| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- <?php
- namespace Illuminate\Support\Traits;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Date;
- use Illuminate\Support\Str;
- use stdClass;
- use function Illuminate\Support\enum_value;
- trait InteractsWithData
- {
- /**
- * Retrieve all data from the instance.
- *
- * @param mixed $keys
- * @return array
- */
- abstract public function all($keys = null);
- /**
- * Retrieve data from the instance.
- *
- * @param string|null $key
- * @param mixed $default
- * @return mixed
- */
- abstract protected function data($key = null, $default = null);
- /**
- * Determine if the data contains a given key.
- *
- * @param string|array $key
- * @return bool
- */
- public function exists($key)
- {
- return $this->has($key);
- }
- /**
- * Determine if the data contains a given key.
- *
- * @param string|array $key
- * @return bool
- */
- public function has($key)
- {
- $keys = is_array($key) ? $key : func_get_args();
- $data = $this->all();
- foreach ($keys as $value) {
- if (! Arr::has($data, $value)) {
- return false;
- }
- }
- return true;
- }
- /**
- * Determine if the instance contains any of the given keys.
- *
- * @param string|array $keys
- * @return bool
- */
- public function hasAny($keys)
- {
- $keys = is_array($keys) ? $keys : func_get_args();
- $data = $this->all();
- return Arr::hasAny($data, $keys);
- }
- /**
- * Apply the callback if the instance contains the given key.
- *
- * @param string $key
- * @param callable $callback
- * @param callable|null $default
- * @return $this|mixed
- */
- public function whenHas($key, callable $callback, ?callable $default = null)
- {
- if ($this->has($key)) {
- return $callback(data_get($this->all(), $key)) ?: $this;
- }
- if ($default) {
- return $default();
- }
- return $this;
- }
- /**
- * Determine if the instance contains a non-empty value for the given key.
- *
- * @param string|array $key
- * @return bool
- */
- public function filled($key)
- {
- $keys = is_array($key) ? $key : func_get_args();
- foreach ($keys as $value) {
- if ($this->isEmptyString($value)) {
- return false;
- }
- }
- return true;
- }
- /**
- * Determine if the instance contains an empty value for the given key.
- *
- * @param string|array $key
- * @return bool
- */
- public function isNotFilled($key)
- {
- $keys = is_array($key) ? $key : func_get_args();
- foreach ($keys as $value) {
- if (! $this->isEmptyString($value)) {
- return false;
- }
- }
- return true;
- }
- /**
- * Determine if the instance contains a non-empty value for any of the given keys.
- *
- * @param string|array $keys
- * @return bool
- */
- public function anyFilled($keys)
- {
- $keys = is_array($keys) ? $keys : func_get_args();
- foreach ($keys as $key) {
- if ($this->filled($key)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Apply the callback if the instance contains a non-empty value for the given key.
- *
- * @param string $key
- * @param callable $callback
- * @param callable|null $default
- * @return $this|mixed
- */
- public function whenFilled($key, callable $callback, ?callable $default = null)
- {
- if ($this->filled($key)) {
- return $callback(data_get($this->all(), $key)) ?: $this;
- }
- if ($default) {
- return $default();
- }
- return $this;
- }
- /**
- * Determine if the instance is missing a given key.
- *
- * @param string|array $key
- * @return bool
- */
- public function missing($key)
- {
- $keys = is_array($key) ? $key : func_get_args();
- return ! $this->has($keys);
- }
- /**
- * Apply the callback if the instance is missing the given key.
- *
- * @param string $key
- * @param callable $callback
- * @param callable|null $default
- * @return $this|mixed
- */
- public function whenMissing($key, callable $callback, ?callable $default = null)
- {
- if ($this->missing($key)) {
- return $callback(data_get($this->all(), $key)) ?: $this;
- }
- if ($default) {
- return $default();
- }
- return $this;
- }
- /**
- * Determine if the given key is an empty string for "filled".
- *
- * @param string $key
- * @return bool
- */
- protected function isEmptyString($key)
- {
- $value = $this->data($key);
- return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
- }
- /**
- * Retrieve data from the instance as a Stringable instance.
- *
- * @param string $key
- * @param mixed $default
- * @return \Illuminate\Support\Stringable
- */
- public function str($key, $default = null)
- {
- return $this->string($key, $default);
- }
- /**
- * Retrieve data from the instance as a Stringable instance.
- *
- * @param string $key
- * @param mixed $default
- * @return \Illuminate\Support\Stringable
- */
- public function string($key, $default = null)
- {
- return Str::of($this->data($key, $default));
- }
- /**
- * Retrieve data as a boolean value.
- *
- * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
- *
- * @param string|null $key
- * @param bool $default
- * @return bool
- */
- public function boolean($key = null, $default = false)
- {
- return filter_var($this->data($key, $default), FILTER_VALIDATE_BOOLEAN);
- }
- /**
- * Retrieve data as an integer value.
- *
- * @param string $key
- * @param int $default
- * @return int
- */
- public function integer($key, $default = 0)
- {
- return intval($this->data($key, $default));
- }
- /**
- * Retrieve data as a float value.
- *
- * @param string $key
- * @param float $default
- * @return float
- */
- public function float($key, $default = 0.0)
- {
- return floatval($this->data($key, $default));
- }
- /**
- * Retrieve data from the instance as a Carbon instance.
- *
- * @param string $key
- * @param string|null $format
- * @param \UnitEnum|string|null $tz
- * @return \Illuminate\Support\Carbon|null
- *
- * @throws \Carbon\Exceptions\InvalidFormatException
- */
- public function date($key, $format = null, $tz = null)
- {
- $tz = enum_value($tz);
- if ($this->isNotFilled($key)) {
- return null;
- }
- if (is_null($format)) {
- return Date::parse($this->data($key), $tz);
- }
- return Date::createFromFormat($format, $this->data($key), $tz);
- }
- /**
- * Retrieve data from the instance as an enum.
- *
- * @template TEnum of \BackedEnum
- *
- * @param string $key
- * @param class-string<TEnum> $enumClass
- * @param TEnum|null $default
- * @return TEnum|null
- */
- public function enum($key, $enumClass, $default = null)
- {
- if ($this->isNotFilled($key) || ! $this->isBackedEnum($enumClass)) {
- return value($default);
- }
- return $enumClass::tryFrom($this->data($key)) ?: value($default);
- }
- /**
- * Retrieve data from the instance as an array of enums.
- *
- * @template TEnum of \BackedEnum
- *
- * @param string $key
- * @param class-string<TEnum> $enumClass
- * @return TEnum[]
- */
- public function enums($key, $enumClass)
- {
- if ($this->isNotFilled($key) || ! $this->isBackedEnum($enumClass)) {
- return [];
- }
- return $this->collect($key)
- ->map(fn ($value) => $enumClass::tryFrom($value))
- ->filter()
- ->all();
- }
- /**
- * Determine if the given enum class is backed.
- *
- * @param class-string $enumClass
- * @return bool
- */
- protected function isBackedEnum($enumClass)
- {
- return enum_exists($enumClass) && method_exists($enumClass, 'tryFrom');
- }
- /**
- * Retrieve data from the instance as an array.
- *
- * @param array|string|null $key
- * @return array
- */
- public function array($key = null)
- {
- return (array) (is_array($key) ? $this->only($key) : $this->data($key));
- }
- /**
- * Retrieve data from the instance as a collection.
- *
- * @param array|string|null $key
- * @return \Illuminate\Support\Collection
- */
- public function collect($key = null)
- {
- return new Collection(is_array($key) ? $this->only($key) : $this->data($key));
- }
- /**
- * Get a subset containing the provided keys with values from the instance data.
- *
- * @param mixed $keys
- * @return array
- */
- public function only($keys)
- {
- $results = [];
- $data = $this->all();
- $placeholder = new stdClass;
- foreach (is_array($keys) ? $keys : func_get_args() as $key) {
- $value = data_get($data, $key, $placeholder);
- if ($value !== $placeholder) {
- Arr::set($results, $key, $value);
- }
- }
- return $results;
- }
- /**
- * Get all of the data except for a specified array of items.
- *
- * @param mixed $keys
- * @return array
- */
- public function except($keys)
- {
- $keys = is_array($keys) ? $keys : func_get_args();
- $results = $this->all();
- Arr::forget($results, $keys);
- return $results;
- }
- }
|