@@ -17,84 +17,53 @@ The following versions of PHP are supported: `7.4`, `8.0`, `8.1`.
1717$ composer require brenoroosevelt/array-functions
1818```
1919
20- ## Usage
21-
22- ### index_of
2320``` php
24- /**
25- * Searches the iterable for a given element and returns the first corresponding key (index) if successful
26- *
27- * @param iterable $haystack The iterable collection
28- * @param mixed $element The searched element
29- * @param bool $strict If the third parameter strict is set to true then will also check the types of the needle
30- * @return false|int|string the key for needle if it is found in the array, false otherwise.
31- */
32- function index_of(iterable $haystack, $element, bool $strict = true)
33- ```
21+ // Add - Set
22+ function add(array & $set, ...$elements): int
23+ function set(array & $set, $element, $key = null): void
3424
35- ### contains
36- ``` php
37- /**
38- * Checks if ALL elements exists in a collection
39- * The element index (key) is irrelevant for this operation
40- *
41- * @param iterable $items The collection
42- * @param mixed ...$elements The searched elements
43- * @return bool True if ALL elements were found in the collection, false otherwise
44- */
25+ // Count
26+ function all(iterable $items, callable $callback, bool $empty_is_valid = true, int $mode = CALLBACK_USE_VALUE): bool
27+ function at_least(int $n, iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
28+ function at_most(int $n, iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
29+ function exactly(int $n, iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
30+ function none(iterable $items, callable $callback, int $mode = 0): bool
31+ function some(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
4532function contains(iterable $items, ...$elements): bool
46- ```
4733
48- ### add
49- ``` php
50- /**
51- * Adds elements to a collection if they don't exist yet (set behavior).
52- * The element index (key) is irrelevant for this operation
53- *
54- * @param array $set The collection
55- * @param mixed ...$elements Elements to be added
56- * @return int The number of items added to the collection
57- */
58- function add(array & $set, ...$elements): int
59- ```
34+ // Filter - Extract
35+ function accept(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
36+ function reject(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
37+ function only(array $items, ...$keys): array
38+ function except(array $items, ...$keys): array
39+ function first(iterable $items, callable $callback, $default = null, int $mode = CALLBACK_USE_VALUE)
6040
61- ### set
62- ``` php
63- /**
64- * Adds or replace an element to a collection using an optional key/index.
65- * If the given key already exists in the collection, the corresponding value will be replaced by the element
66- *
67- * @example $set = ['a' => 1, 2, 3, 4]
68- * set($set, 2, 'a') $set will contain ['a' => 2, 2, 3, 4]
69- *
70- * @example $set = ['a' => 1, 2, 3, 4]
71- * set($set, 1) $set will contain ['a' => 1, 2, 3, 4, 1]
72- *
73- * @param array $set The collection
74- * @param mixed $element The element to be added
75- * @param string|int $key The key/index of element
76- * @return void
77- */
78- function set(array & $set, $element, $key = null): void
79- ```
41+ // Key - Index
42+ function has(array $haystack, $key, ...$keys): bool
43+ function index_of(iterable $haystack, $element, bool $strict = true)
8044
81- ### remove
82- ``` php
83- /**
84- * Remove all provided elements from the collection
85- * The element index (key) is irrelevant for this operation
86- * This function uses strict comparison to find and remove elements
87- *
88- * @example $set = [1, 1, 2, 3, 4]
89- * remove($set, 1, 3) will return 3 (int) and $set will contain [2, 4]
90- *
91- * @param array $set The collection
92- * @param mixed ...$elements Elements to be removed
93- * @return int The number of removed elements
94- */
45+ // Remove - Pull
46+ function pull(array & $set, $key, $default = null)
9547function remove(array & $set, ...$elements): int
96- ```
48+ function remove_key(array & $set, $key, ...$keys): int
49+
50+ // Miscellaneous
51+ function map(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
52+ function paginate(array $items, int $page, int $per_page, bool $preserve_keys = true): array
53+ function sum(iterable $items, callable $callback, int $mode = 0)
9754
55+ // Dot Notation
56+ function flatten(array $items, ?string $pathSeparator = null): array
57+ function expand(array $items, string $separator = '.'): array
58+ function set_path(array & $haystack, string $path, $value, string $separator = '.'): void
59+ function get_path(array $haystack, string $path, $default = null, string $separator = '.')
60+ function unset_path(array & $haystack, string $path, string $separator = '.')
61+ function has_path(array $haystack, string $path, string $separator = '.'): bool
62+
63+ // Pipeline
64+ function pipe($payload, callable ...$stages)
65+ function with(& $value, callable ...$jobs)
66+ ```
9867## License
9968
10069This project is licensed under the terms of the MIT license. See the [ LICENSE] ( LICENSE.md ) file for license rights and limitations.
0 commit comments