InteractsWithData.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace Illuminate\Support\Traits;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Facades\Date;
  6. use Illuminate\Support\Str;
  7. use stdClass;
  8. use function Illuminate\Support\enum_value;
  9. trait InteractsWithData
  10. {
  11. /**
  12. * Retrieve all data from the instance.
  13. *
  14. * @param mixed $keys
  15. * @return array
  16. */
  17. abstract public function all($keys = null);
  18. /**
  19. * Retrieve data from the instance.
  20. *
  21. * @param string|null $key
  22. * @param mixed $default
  23. * @return mixed
  24. */
  25. abstract protected function data($key = null, $default = null);
  26. /**
  27. * Determine if the data contains a given key.
  28. *
  29. * @param string|array $key
  30. * @return bool
  31. */
  32. public function exists($key)
  33. {
  34. return $this->has($key);
  35. }
  36. /**
  37. * Determine if the data contains a given key.
  38. *
  39. * @param string|array $key
  40. * @return bool
  41. */
  42. public function has($key)
  43. {
  44. $keys = is_array($key) ? $key : func_get_args();
  45. $data = $this->all();
  46. foreach ($keys as $value) {
  47. if (! Arr::has($data, $value)) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. /**
  54. * Determine if the instance contains any of the given keys.
  55. *
  56. * @param string|array $keys
  57. * @return bool
  58. */
  59. public function hasAny($keys)
  60. {
  61. $keys = is_array($keys) ? $keys : func_get_args();
  62. $data = $this->all();
  63. return Arr::hasAny($data, $keys);
  64. }
  65. /**
  66. * Apply the callback if the instance contains the given key.
  67. *
  68. * @param string $key
  69. * @param callable $callback
  70. * @param callable|null $default
  71. * @return $this|mixed
  72. */
  73. public function whenHas($key, callable $callback, ?callable $default = null)
  74. {
  75. if ($this->has($key)) {
  76. return $callback(data_get($this->all(), $key)) ?: $this;
  77. }
  78. if ($default) {
  79. return $default();
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Determine if the instance contains a non-empty value for the given key.
  85. *
  86. * @param string|array $key
  87. * @return bool
  88. */
  89. public function filled($key)
  90. {
  91. $keys = is_array($key) ? $key : func_get_args();
  92. foreach ($keys as $value) {
  93. if ($this->isEmptyString($value)) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. /**
  100. * Determine if the instance contains an empty value for the given key.
  101. *
  102. * @param string|array $key
  103. * @return bool
  104. */
  105. public function isNotFilled($key)
  106. {
  107. $keys = is_array($key) ? $key : func_get_args();
  108. foreach ($keys as $value) {
  109. if (! $this->isEmptyString($value)) {
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. /**
  116. * Determine if the instance contains a non-empty value for any of the given keys.
  117. *
  118. * @param string|array $keys
  119. * @return bool
  120. */
  121. public function anyFilled($keys)
  122. {
  123. $keys = is_array($keys) ? $keys : func_get_args();
  124. foreach ($keys as $key) {
  125. if ($this->filled($key)) {
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. /**
  132. * Apply the callback if the instance contains a non-empty value for the given key.
  133. *
  134. * @param string $key
  135. * @param callable $callback
  136. * @param callable|null $default
  137. * @return $this|mixed
  138. */
  139. public function whenFilled($key, callable $callback, ?callable $default = null)
  140. {
  141. if ($this->filled($key)) {
  142. return $callback(data_get($this->all(), $key)) ?: $this;
  143. }
  144. if ($default) {
  145. return $default();
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Determine if the instance is missing a given key.
  151. *
  152. * @param string|array $key
  153. * @return bool
  154. */
  155. public function missing($key)
  156. {
  157. $keys = is_array($key) ? $key : func_get_args();
  158. return ! $this->has($keys);
  159. }
  160. /**
  161. * Apply the callback if the instance is missing the given key.
  162. *
  163. * @param string $key
  164. * @param callable $callback
  165. * @param callable|null $default
  166. * @return $this|mixed
  167. */
  168. public function whenMissing($key, callable $callback, ?callable $default = null)
  169. {
  170. if ($this->missing($key)) {
  171. return $callback(data_get($this->all(), $key)) ?: $this;
  172. }
  173. if ($default) {
  174. return $default();
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Determine if the given key is an empty string for "filled".
  180. *
  181. * @param string $key
  182. * @return bool
  183. */
  184. protected function isEmptyString($key)
  185. {
  186. $value = $this->data($key);
  187. return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
  188. }
  189. /**
  190. * Retrieve data from the instance as a Stringable instance.
  191. *
  192. * @param string $key
  193. * @param mixed $default
  194. * @return \Illuminate\Support\Stringable
  195. */
  196. public function str($key, $default = null)
  197. {
  198. return $this->string($key, $default);
  199. }
  200. /**
  201. * Retrieve data from the instance as a Stringable instance.
  202. *
  203. * @param string $key
  204. * @param mixed $default
  205. * @return \Illuminate\Support\Stringable
  206. */
  207. public function string($key, $default = null)
  208. {
  209. return Str::of($this->data($key, $default));
  210. }
  211. /**
  212. * Retrieve data as a boolean value.
  213. *
  214. * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
  215. *
  216. * @param string|null $key
  217. * @param bool $default
  218. * @return bool
  219. */
  220. public function boolean($key = null, $default = false)
  221. {
  222. return filter_var($this->data($key, $default), FILTER_VALIDATE_BOOLEAN);
  223. }
  224. /**
  225. * Retrieve data as an integer value.
  226. *
  227. * @param string $key
  228. * @param int $default
  229. * @return int
  230. */
  231. public function integer($key, $default = 0)
  232. {
  233. return intval($this->data($key, $default));
  234. }
  235. /**
  236. * Retrieve data as a float value.
  237. *
  238. * @param string $key
  239. * @param float $default
  240. * @return float
  241. */
  242. public function float($key, $default = 0.0)
  243. {
  244. return floatval($this->data($key, $default));
  245. }
  246. /**
  247. * Retrieve data from the instance as a Carbon instance.
  248. *
  249. * @param string $key
  250. * @param string|null $format
  251. * @param \UnitEnum|string|null $tz
  252. * @return \Illuminate\Support\Carbon|null
  253. *
  254. * @throws \Carbon\Exceptions\InvalidFormatException
  255. */
  256. public function date($key, $format = null, $tz = null)
  257. {
  258. $tz = enum_value($tz);
  259. if ($this->isNotFilled($key)) {
  260. return null;
  261. }
  262. if (is_null($format)) {
  263. return Date::parse($this->data($key), $tz);
  264. }
  265. return Date::createFromFormat($format, $this->data($key), $tz);
  266. }
  267. /**
  268. * Retrieve data from the instance as an enum.
  269. *
  270. * @template TEnum of \BackedEnum
  271. *
  272. * @param string $key
  273. * @param class-string<TEnum> $enumClass
  274. * @param TEnum|null $default
  275. * @return TEnum|null
  276. */
  277. public function enum($key, $enumClass, $default = null)
  278. {
  279. if ($this->isNotFilled($key) || ! $this->isBackedEnum($enumClass)) {
  280. return value($default);
  281. }
  282. return $enumClass::tryFrom($this->data($key)) ?: value($default);
  283. }
  284. /**
  285. * Retrieve data from the instance as an array of enums.
  286. *
  287. * @template TEnum of \BackedEnum
  288. *
  289. * @param string $key
  290. * @param class-string<TEnum> $enumClass
  291. * @return TEnum[]
  292. */
  293. public function enums($key, $enumClass)
  294. {
  295. if ($this->isNotFilled($key) || ! $this->isBackedEnum($enumClass)) {
  296. return [];
  297. }
  298. return $this->collect($key)
  299. ->map(fn ($value) => $enumClass::tryFrom($value))
  300. ->filter()
  301. ->all();
  302. }
  303. /**
  304. * Determine if the given enum class is backed.
  305. *
  306. * @param class-string $enumClass
  307. * @return bool
  308. */
  309. protected function isBackedEnum($enumClass)
  310. {
  311. return enum_exists($enumClass) && method_exists($enumClass, 'tryFrom');
  312. }
  313. /**
  314. * Retrieve data from the instance as an array.
  315. *
  316. * @param array|string|null $key
  317. * @return array
  318. */
  319. public function array($key = null)
  320. {
  321. return (array) (is_array($key) ? $this->only($key) : $this->data($key));
  322. }
  323. /**
  324. * Retrieve data from the instance as a collection.
  325. *
  326. * @param array|string|null $key
  327. * @return \Illuminate\Support\Collection
  328. */
  329. public function collect($key = null)
  330. {
  331. return new Collection(is_array($key) ? $this->only($key) : $this->data($key));
  332. }
  333. /**
  334. * Get a subset containing the provided keys with values from the instance data.
  335. *
  336. * @param mixed $keys
  337. * @return array
  338. */
  339. public function only($keys)
  340. {
  341. $results = [];
  342. $data = $this->all();
  343. $placeholder = new stdClass;
  344. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  345. $value = data_get($data, $key, $placeholder);
  346. if ($value !== $placeholder) {
  347. Arr::set($results, $key, $value);
  348. }
  349. }
  350. return $results;
  351. }
  352. /**
  353. * Get all of the data except for a specified array of items.
  354. *
  355. * @param mixed $keys
  356. * @return array
  357. */
  358. public function except($keys)
  359. {
  360. $keys = is_array($keys) ? $keys : func_get_args();
  361. $results = $this->all();
  362. Arr::forget($results, $keys);
  363. return $results;
  364. }
  365. }