• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NIT-Administrative-Systems / dynamic-forms / 13506324421

24 Feb 2025 07:35PM UTC coverage: 59.275% (-34.7%) from 94.003%
13506324421

Pull #480

github

nie7321
Update ComponentRegistryTest.php
Pull Request #480: Laravel 12

818 of 1380 relevant lines covered (59.28%)

45.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/JSONLogic/LodashFunctions/Collection.php
1
<?php
2

3
namespace Northwestern\SysDev\DynamicForms\JSONLogic\LodashFunctions;
4

5
use Closure;
6
use stdClass;
7

8
trait Collection
9
{
10
    public static function includes(string | object | array $collection, mixed $value, int $fromIndex = 0): bool
11
    {
12
        if (is_string($collection)) {
×
13
            return str_contains(substr($collection, $fromIndex), $value);
×
14
        }
15
        if (is_object($collection)) {
×
16
            foreach ($collection as $key => $item) {
×
17
                if ($value === $item) {
×
18
                    return true;
×
19
                }
20
            }
21

22
            return false;
×
23
        }
24
        if (is_array($collection)) {
×
25
            return in_array($value, array_slice($collection, $fromIndex));
×
26
        }
27

28
        return false;
×
29
    }
30

31
    public static function orderBy(?iterable $collection, array $iteratee, array $orders): array
32
    {
33
        $temp = \_\orderBy($collection, $iteratee, $orders);
×
34
        $ret = [];
×
35
        foreach ($temp as $temp2) {
×
36
            $ret[] = $temp2['value'];
×
37
        }
38

39
        return $ret;
×
40
    }
41

42
    /**
43
     * Gets the first element of an array. Passing n returns the first n elements.
44
     *
45
     * @usage __::first([1, 2, 3]);
46
     *        >> 1
47
     *
48
     * @param array    $array of values
49
     * @param int|null $take  number of values to return
50
     *
51
     * @return mixed
52
     *
53
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L48
54
     * @license MIT
55
     */
56
    public static function first(array $array, $take = null)
57
    {
58
        if (! $take) {
×
59
            return array_shift($array);
×
60
        }
61

62
        return array_splice($array, 0, $take);
×
63
    }
64

65
    /**
66
     * Check if value is an empty array or object. We consider any non enumerable as empty.
67
     *
68
     * @usage __::isEmpty([]);
69
     *        >> true
70
     *
71
     * @param mixed $value The value to check for emptiness.
72
     *
73
     * @return bool
74
     *
75
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L743
76
     * @license MIT
77
     */
78
    public static function isEmpty(mixed $value): bool
79
    {
80
        return (! is_array($value) && ! is_object($value)) || count((array) $value) === 0;
×
81
    }
82

83
    /**
84
     * Returns the minimum value from the collection. If passed an iterator, min will return min value returned by the
85
     * iterator.
86
     *
87
     * @usage __::min([1, 2, 3]);
88
     *        >> 1
89
     *
90
     * @param array $array array of values
91
     *
92
     * @return mixed
93
     *
94
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L170
95
     * @license MIT
96
     */
97
    public static function min(array $array = []): mixed
98
    {
99
        return min($array);
×
100
    }
101

102
    /**
103
     * Returns an array having only keys present in the given path list. Values for missing keys values will be filled
104
     * with provided default value.
105
     *
106
     * @usage __::pick(['a' => 1, 'b' => ['c' => 3, 'd' => 4]], ['a', 'b.d']);
107
     *        >> ['a' => 1, 'b' => ['d' => 4]]
108
     *
109
     * @param object|array $collection The collection to iterate over.
110
     * @param array        $paths      array paths to pick
111
     * @param null         $default
112
     *
113
     * @return array|object
114
     *
115
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L842
116
     * @license MIT
117
     */
118
    public static function pick(object|array $collection = [], array $paths = [], $default = null)
119
    {
120
        return self::reduce($paths, function ($results, $path) use ($collection, $default) {
×
121
            return self::set($results, $path, self::get($collection, $path, $default));
×
122
        }, self::isObjectStrict($collection) ? new stdClass() : []);
×
123
    }
124

125
    /**
126
     * Reduces $collection to a value which is the $accumulator result of running each
127
     * element in $collection thru $iterateFn, where each successive invocation is supplied
128
     * the return value of the previous.
129
     *
130
     * If $accumulator is not given, the first element of $collection is used as the
131
     * initial value.
132
     *
133
     * The $iterateFn is invoked with four arguments:
134
     * ($accumulator, $value, $index|$key, $collection).
135
     *
136
     * @usage __::reduce([1, 2], function ($sum, $number) {
137
     *                return $sum + $number;
138
     *            }, 0);
139
     *        >> 3
140
     *
141
     *        $a = [
142
     *            ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
143
     *            ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'],
144
     *            ['state' => 'IN', 'city' => 'Plainfield', 'object' => 'Basketball'],
145
     *            ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
146
     *            ['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'],
147
     *        ];
148
     *        $iterateFn = function ($accumulator, $value) {
149
     *            if (isset($accumulator[$value['city']]))
150
     *                $accumulator[$value['city']]++;
151
     *            else
152
     *                $accumulator[$value['city']] = 1;
153
     *            return $accumulator;
154
     *        };
155
     *        __::reduce($c, $iterateFn, []);
156
     *        >> [
157
     *            'Indianapolis' => 2,
158
     *            'Plainfield' => 1,
159
     *            'San Diego' => 1,
160
     *            'Mountain View' => 1,
161
     *         ]
162
     *
163
     *        $object = new \stdClass();
164
     *        $object->a = 1;
165
     *        $object->b = 2;
166
     *        $object->c = 1;
167
     *        __::reduce($object, function ($result, $value, $key) {
168
     *            if (!isset($result[$value]))
169
     *                $result[$value] = [];
170
     *            $result[$value][] = $key;
171
     *            return $result;
172
     *        }, [])
173
     *        >> [
174
     *             '1' => ['a', 'c'],
175
     *             '2' => ['b']
176
     *         ]
177
     *
178
     * @param array               $collection The collection to iterate over.
179
     * @param Closure             $iterateFn  The function invoked per iteration.
180
     * @param null|array          $accumulator
181
     *
182
     * @return array|mixed|null (*): Returns the accumulated value.
183
     *
184
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L908
185
     * @license MIT
186
     */
187
    public static function reduce($collection, Closure $iterateFn, null|array $accumulator = null)
188
    {
189
        if ($accumulator === null) {
×
190
            $accumulator = array_shift($collection);
×
191
        }
192

193
        self::doForEach(
×
194
            $collection,
×
195
            function ($value, $key, $collection) use (&$accumulator, $iterateFn) {
×
196
                $accumulator = $iterateFn($accumulator, $value, $key, $collection);
×
197
            }
×
198
        );
×
199

200
        return $accumulator;
×
201
    }
202

203
    /**
204
     * Iterate over elements of the collection and invokes iterate for each element.
205
     *
206
     * The iterate is invoked with three arguments: (value, index|key, collection).
207
     * Iterate functions may exit iteration early by explicitly returning false.
208
     *
209
     * @usage __::doForEach([1, 2, 3], function ($value) { print_r($value) });
210
     *        >> (Side effect: print 1, 2, 3)
211
     *
212
     * @param array|object $collection The collection to iterate over.
213
     * @param \Closure     $iterateFn  The function to call for each value
214
     *
215
     * @return bool
216
     *
217
     * @license MIT
218
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L369
219
     */
220
    public static function doForEach(array|object $collection, Closure $iterateFn)
221
    {
222
        foreach ($collection as $key => $value) {
×
223
            if ($iterateFn($value, $key, $collection) === false) {
×
224
                break;
×
225
            }
226
        }
227

228
        return true;
×
229
    }
230

231
    /**
232
     * Return a new collection with the item set at index to given value.
233
     * Index can be a path of nested indexes.
234
     *
235
     * If a portion of path doesn't exist, it's created. Arrays are created for missing
236
     * index in an array; objects are created for missing property in an object.
237
     *
238
     * @usage __::set(['foo' => ['bar' => 'ter']], 'foo.baz.ber', 'fer');
239
     *        >> '['foo' => ['bar' => 'ter', 'baz' => ['ber' => 'fer']]]'
240
     *
241
     * @param array|object|null $collection collection of values
242
     * @param string|int|null   $path       key or index
243
     * @param mixed             $value      the value to set at position $key
244
     *
245
     * @throws \Exception if the path consists of a non collection and strict is set to false
246
     *
247
     * @return array|object the new collection with the item set
248
     *
249
     * @license MIT
250
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L409
251
     */
252
    public static function set(array|object|null $collection, string|int|null $path, mixed $value = null)
253
    {
254
        if ($path === null) {
×
255
            return $collection;
×
256
        }
257
        $portions = self::split($path, '.', 2);
×
258
        $key = $portions[0];
×
259
        if (count($portions) === 1) {
×
260
            return self::universalSet($collection, $key, $value);
×
261
        }
262
        // Here we manage the case where the portion of the path points to nothing,
263
        // or to a value that does not match the type of the source collection
264
        // (e.g. the path portion 'foo.bar' points to an integer value, while we
265
        // want to set a string at 'foo.bar.fun'. We first set an object or array
266
        //  - following the current collection type - to 'for.bar' before setting
267
        // 'foo.bar.fun' to the specified value).
268
        if (! self::has($collection, $key)
×
269
            || (self::isObjectStrict($collection) && ! self::isObjectStrict(self::get($collection, $key)))
×
270
            || (self::isArray($collection) && ! self::isArray(self::get($collection, $key)))
×
271
        ) {
272
            $collection = self::universalSet($collection, $key, self::isObjectStrict($collection) ? new stdClass : []);
×
273
        }
274

275
        return self::universalSet($collection, $key, self::set(self::get($collection, $key), $portions[1], $value));
×
276
    }
277

278
    /**
279
     * Return true if $collection contains the requested $key.
280
     *
281
     * In constraint to isset(), __::has() returns true if the key exists but is null.
282
     *
283
     * @usage __::has(['foo' => ['bar' => 'num'], 'foz' => 'baz'], 'foo.bar');
284
     *        >> true
285
     *
286
     *        __::hasKeys((object) ['foo' => 'bar', 'foz' => 'baz'], 'bar');
287
     *        >> false
288
     *
289
     * @param null|array|object $collection of key values pairs
290
     * @param string            $path       Path to look for.
291
     *
292
     * @return bool
293
     */
294
    public static function has(null|array|object $collection, string|array $path): bool
295
    {
296
        if (is_array($path)) {
×
297
            $path = implode('.', $path);
×
298
        }
299

300
        $portions = self::split($path, '.', 2);
×
301
        $key = $portions[0];
×
302

303
        if (count($portions) === 1) {
×
304
            return array_key_exists($key, (array) $collection);
×
305
        }
306

307
        return self::has(self::get($collection, $key), $portions[1]);
×
308
    }
309

310
    /**
311
     * @param mixed $collection
312
     * @param mixed $key
313
     * @param mixed $value
314
     *
315
     * @return mixed
316
     *
317
     * @license MIT
318
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L442
319
     */
320
    public static function universalSet(mixed $collection, mixed $key, mixed $value): mixed
321
    {
322
        $set_object = function ($object, $key, $value) {
323
            $newObject = clone $object;
×
324
            $newObject->$key = $value;
×
325

326
            return $newObject;
×
327
        };
328
        $set_array = function ($array, $key, $value) {
329
            $array[$key] = $value;
×
330

331
            return $array;
×
332
        };
333
        $setter = self::isObjectStrict($collection) ? $set_object : $set_array;
×
334

335
        return call_user_func_array($setter, [$collection, $key, $value]);
×
336
    }
337

338
    /**
339
     * Get item of an array by index, accepting nested index.
340
     *
341
     * @usage __::get(['foo' => ['bar' => 'ter']], 'foo.bar');
342
     *        >> 'ter'
343
     *
344
     * @param array|object $collection array of values
345
     * @param null|string  $key        key or index
346
     * @param mixed        $default    default value to return if index not exist
347
     *
348
     * @return mixed
349
     *
350
     * @license MIT
351
     * @url https://github.com/me-io/php-lodash/blob/2.0.0/src/Traits/Collections.php#L69
352
     */
353
    public static function get(array|object $collection = [], null|string $key = null, mixed $default = null): mixed
354
    {
355
        if (self::isNull($key)) {
×
356
            return $collection;
×
357
        }
358

359
        if (! self::isObjectStrict($collection) && isset($collection[$key])) {
×
360
            return $collection[$key];
×
361
        }
362

363
        foreach (explode('.', $key) as $segment) {
×
364
            if (self::isObjectStrict($collection)) {
×
365
                if (! isset($collection->{$segment})) {
×
366
                    return $default instanceof Closure ? $default() : $default;
×
367
                } else {
368
                    $collection = $collection->{$segment};
×
369
                }
370
            } else {
371
                if (! isset($collection[$segment])) {
×
372
                    return $default instanceof Closure ? $default() : $default;
×
373
                } else {
374
                    $collection = $collection[$segment];
×
375
                }
376
            }
377
        }
378

379
        return $collection;
×
380
    }
381
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc