wrapped = (object)$wrapped; } public function __get($name) { if ($this->wrapped) { if (isset($this->wrapped->$name)) { $x = $this->wrapped->$name; if (is_array($x) || is_object($x)) return objBag($x); return $x; } } return null; } public function __set($name, $value) { if ($this->wrapped) { $this->wrapped->$name = $value; } } public function __unset($name) { if ($this->wrapped) { unset($this->wrapped->$name); } } public function __isset($name) { return isset($this->wrapped->$name); } public function get($name, $def = null) { if (!isset($this->$name)) return $def; return $this->$name; } public function has($name) { return isset($this->$name) && $this->$name !== null; } public function unpack() { return $this->wrapped; } public function toArray() { return(array)$this->wrapped; } public function all() { return $this->toArray(); } /** * Specify data which should be serialized to JSON * * @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @return mixed data which can be serialized by json_encode, * which is a value of any type other than a resource. * @since 5.4.0 */ public function jsonSerialize() { return $this->wrapped; } public function offsetExists($offset) { return isset($this->$offset); } public function offsetGet($offset) { return $this->$offset; } public function offsetSet($offset, $value) { $this->$offset = $value; } public function offsetUnset($offset) { unset($this->$offset); } } function objBag($obj) { return new \objBag($obj); } /** * Helper to check if model exists. * * Related to https://github.com/laravel/framework/issues/13988 * * @param \Illuminate\Database\Eloquent\Model|null $model * @return bool */ function model_exists(Illuminate\Database\Eloquent\Model $model = null) { return $model !== null && $model->exists; } /** * Get a data array or value from the config data array * * @param string $key key in dot notation * @param null $default default value if not defined * @return mixed */ function data($key, $default = null) { return config("data.$key", $default); } /** * Wrap the argument in an array, if not an array already. * Null becomes an empty array, array stays an array. * Collection is converted to an array. * * @param mixed|mixed[]|null $oneOrMany * @return array */ function arr($oneOrMany) { if (is_null($oneOrMany)) { return []; } if (is_array($oneOrMany)) { return $oneOrMany; } else if ($oneOrMany instanceof \Illuminate\Support\Collection) { return $oneOrMany->toArray(); } else { return [$oneOrMany]; } } /** * Clamp value to given bounds * * @param float $value input * @param float $min lower bound - included * @param float $max upper bound - included * @return float result */ function clamp($value, $min, $max) { return Utils::clamp($value, $min, $max); } function objCollect($value = null) { return new ObjCollection($value); } function mb_ucfirst($str) { return Str::ucfirst($str); } // "pipe sort" is like normal sort, but returns the sorted array /** "pipe sort" */ function sorted($arr) { sort($arr); return $arr; } /** natural "pipe sort" */ function natsorted($arr, $cs=false) { return collect($arr)->natSort($cs)->all(); } /** natural "pipe sort" by prop or retriever */ function natsorted_by($arr, $prop, $cs=false) { return collect($arr)->natSortBy($prop, $cs)->all(); } /** Preg match that returns the array */ function preg_matched($pattern, $subject) { $ar = []; preg_match($pattern, $subject, $ar); return $ar; } /** * Format a string using the {} syntax * * @param string $format * @param array ...$replacements * @return string */ function fmt($format, ...$replacements) { return Str::format($format, ...$replacements); } /** * Sleep milliseconds (complementary to sleep & usleep) * * @param int $millis ms to sleep */ function msleep($millis) { usleep(1000 * $millis); } /** * Sleep with a log message * * @param int $millis ms to sleep */ function log_msleep($millis) { Log::debug("Sleeping $millis ms"); usleep(1000 * $millis); } /** * Sleep with a log message * * @param int $secs ms to sleep */ function log_sleep($secs) { Log::debug("Sleeping $secs s"); sleep($secs); } #endregion #region Profiling /** * Start profiling * * @return float start time (token) */ function prof_start() { return Profiler::start(); } /** * End profiling * * @param float $token start time (from profiler_start()) * @return float time elapsed */ function prof_end($token) { return Profiler::end($token); } #endregion /** * Simple (but not exhaustive) transliteration for natural sort etc. * * @param $string * @return string */ function simple_translit($string) { static $table = [ 'Á' => 'A', 'á' => 'a', 'Č' => 'C', 'č' => 'c', 'Ď' => 'D', 'ď' => 'd', 'É' => 'E', 'é' => 'e', 'Ě' => 'E', 'ě' => 'e', 'Í' => 'I', 'í' => 'i', 'Ň' => 'N', 'ň' => 'n', 'Ó' => 'O', 'ó' => 'o', 'Ř' => 'R', 'ř' => 'r', 'Š' => 'S', 'š' => 's', 'Ť' => 'T', 'ť' => 't', 'Ú' => 'U', 'ú' => 'u', 'Ů' => 'U', 'ů' => 'u', 'Ý' => 'Y', 'ý' => 'y', 'Ž' => 'Z', 'ž' => 'z', 'ß' => 'ss', 'А' => 'a', 'а' => 'a', 'Б' => 'b', 'б' => 'b', 'В' => 'v', 'в' => 'v', 'Г' => 'g', 'г' => 'g', 'Д' => 'd', 'д' => 'd', 'Е' => 'e', 'е' => 'e', 'Ё' => 'e', 'ё' => 'e', 'Ж' => 'z', 'ж' => 'z', 'З' => 'z', 'з' => 'z', 'И' => 'i', 'и' => 'i', 'Й' => 'j', 'й' => 'j', 'К' => 'k', 'к' => 'k', 'Л' => 'l', 'л' => 'l', 'М' => 'm', 'м' => 'm', 'Н' => 'n', 'н' => 'n', 'О' => 'o', 'о' => 'o', 'П' => 'p', 'п' => 'p', 'Р' => 'r', 'р' => 'r', 'С' => 's', 'с' => 's', 'Т' => 't', 'т' => 't', 'У' => 'u', 'у' => 'u', 'Ф' => 'f', 'ф' => 'f', 'Х' => 'x', 'х' => 'x', 'Ц' => 'c', 'ц' => 'c', 'Ч' => 'ch', 'ч' => 'ch', 'Ш' => 'sh', 'ш' => 'sh', 'Щ' => 'shch', 'щ' => 'shch', 'Ы' => 'y', 'ы' => 'y', 'Э' => 'e', 'э' => 'e', 'Ю' => 'ju', 'ю' => 'ju', 'Я' => 'ja', 'я' => 'ja', ]; return strtr($string, $table); }