|
@@ -0,0 +1,44 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Providers\Macros;
|
|
|
+
|
|
|
+use Illuminate\Support\Collection;
|
|
|
+use Illuminate\Support\ServiceProvider;
|
|
|
+
|
|
|
+class CollectionProvider extends ServiceProvider
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * Register services.
|
|
|
+ */
|
|
|
+ public function register(): void
|
|
|
+ {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Bootstrap services.
|
|
|
+ */
|
|
|
+ public function boot(): void
|
|
|
+ {
|
|
|
+ /**
|
|
|
+ * Recursively convert array to collection
|
|
|
+ */
|
|
|
+ Collection::macro('recursive', function () {
|
|
|
+ $this->whenNotEmpty($recursive = function ($collection) use (&$recursive) {
|
|
|
+ if (is_array($collection)) {
|
|
|
+ return $recursive(new static($collection));
|
|
|
+ } else if ($collection instanceof Collection) {
|
|
|
+ $collection->transform(function ($value, $key) use ($collection, $recursive) {
|
|
|
+ return $collection->{$key} = $recursive($value);
|
|
|
+ });
|
|
|
+ } else if (is_object($collection)) {
|
|
|
+ foreach ($collection as $k => &$v) {
|
|
|
+ $collection->{$k} = $recursive($v);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $collection;
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|