InteractsWithData.php 11 KB

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