← All changes
|
includes/sdk/google/ramsey/collection/src/CollectionInterface.php
+85
-38
1.1.1
→
1.4.1
View file →
| @@ -9,12 +9,17 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Collection; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection; | |
| 14 | 14 | |
| 15 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\CollectionMismatchException; | |
| 16 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidArgumentException; | |
| 17 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidPropertyOrMethod; | |
| 18 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\NoSuchElementException; | |
| 19 | +use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\UnsupportedOperationException; | |
| 15 | 20 | /** |
| 16 | - * A collection represents a group of objects, known as its elements. | |
| 21 | + * A collection represents a group of values, known as its elements. | |
| 17 | 22 | * |
| 18 | 23 | * Some collections allow duplicate elements and others do not. Some are ordered |
| 19 | 24 | * and others unordered. |
| 20 | 25 | * |
| @@ -23,16 +28,8 @@ | ||
| 23 | 28 | */ |
| 24 | 29 | interface CollectionInterface extends ArrayInterface |
| 25 | 30 | { |
| 26 | 31 | /** |
| 27 | - * Ascending sort type. | |
| 28 | - */ | |
| 29 | - public const SORT_ASC = 'asc'; | |
| 30 | - /** | |
| 31 | - * Descending sort type. | |
| 32 | - */ | |
| 33 | - public const SORT_DESC = 'desc'; | |
| 34 | - /** | |
| 35 | 32 | * Ensures that this collection contains the specified element (optional |
| 36 | 33 | * operation). |
| 37 | 34 | * |
| 38 | 35 | * Returns `true` if this collection changed as a result of the call. |
| @@ -53,11 +50,13 @@ | ||
| 53 | 50 | * |
| 54 | 51 | * @param T $element The element to add to the collection. |
| 55 | 52 | * |
| 56 | 53 | * @return bool `true` if this collection changed as a result of the call. |
| 54 | + * | |
| 55 | + * @throws InvalidArgumentException if the collection refuses to add the | |
| 56 | + * $element for any reason other than that it already contains the element. | |
| 57 | 57 | */ |
| 58 | - // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | |
| 59 | - public function add($element): bool; | |
| 58 | + public function add(mixed $element) : bool; | |
| 60 | 59 | /** |
| 61 | 60 | * Returns `true` if this collection contains the specified element. |
| 62 | 61 | * |
| 63 | 62 | * @param T $element The element to check whether the collection contains. |
| @@ -62,14 +61,13 @@ | ||
| 62 | 61 | * |
| 63 | 62 | * @param T $element The element to check whether the collection contains. |
| 64 | 63 | * @param bool $strict Whether to perform a strict type check on the value. |
| 65 | 64 | */ |
| 66 | - // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | |
| 67 | - public function contains($element, bool $strict = \true): bool; | |
| 65 | + public function contains(mixed $element, bool $strict = \true) : bool; | |
| 68 | 66 | /** |
| 69 | 67 | * Returns the type associated with this collection. |
| 70 | 68 | */ |
| 71 | - public function getType(): string; | |
| 69 | + public function getType() : string; | |
| 72 | 70 | /** |
| 73 | 71 | * Removes a single instance of the specified element from this collection, |
| 74 | 72 | * if it is present. |
| 75 | 73 | * |
| @@ -76,43 +74,60 @@ | ||
| 76 | 74 | * @param T $element The element to remove from the collection. |
| 77 | 75 | * |
| 78 | 76 | * @return bool `true` if an element was removed as a result of this call. |
| 79 | 77 | */ |
| 80 | - // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | |
| 81 | - public function remove($element): bool; | |
| 78 | + public function remove(mixed $element) : bool; | |
| 82 | 79 | /** |
| 83 | - * Returns the values from the given property or method. | |
| 80 | + * Returns the values from the given property, method, or array key. | |
| 84 | 81 | * |
| 85 | - * @param string $propertyOrMethod The property or method name to filter by. | |
| 82 | + * @param string $propertyOrMethod The name of the property, method, or | |
| 83 | + * array key to evaluate and return. | |
| 86 | 84 | * |
| 87 | 85 | * @return list<mixed> |
| 86 | + * | |
| 87 | + * @throws InvalidPropertyOrMethod if the $propertyOrMethod does not exist | |
| 88 | + * on the elements in this collection. | |
| 89 | + * @throws UnsupportedOperationException if unable to call column() on this | |
| 90 | + * collection. | |
| 88 | 91 | */ |
| 89 | - public function column(string $propertyOrMethod): array; | |
| 92 | + public function column(string $propertyOrMethod) : array; | |
| 90 | 93 | /** |
| 91 | 94 | * Returns the first item of the collection. |
| 92 | 95 | * |
| 93 | 96 | * @return T |
| 97 | + * | |
| 98 | + * @throws NoSuchElementException if this collection is empty. | |
| 94 | 99 | */ |
| 95 | - public function first(); | |
| 100 | + public function first() : mixed; | |
| 96 | 101 | /** |
| 97 | 102 | * Returns the last item of the collection. |
| 98 | 103 | * |
| 99 | 104 | * @return T |
| 105 | + * | |
| 106 | + * @throws NoSuchElementException if this collection is empty. | |
| 100 | 107 | */ |
| 101 | - public function last(); | |
| 108 | + public function last() : mixed; | |
| 102 | 109 | /** |
| 103 | - * Sort the collection by a property or method with the given sort order. | |
| 110 | + * Sort the collection by a property, method, or array key with the given | |
| 111 | + * sort order. | |
| 104 | 112 | * |
| 113 | + * If $propertyOrMethod is `null`, this will sort by comparing each element. | |
| 114 | + * | |
| 105 | 115 | * This will always leave the original collection untouched and will return |
| 106 | 116 | * a new one. |
| 107 | 117 | * |
| 108 | - * @param string $propertyOrMethod The property or method to sort by. | |
| 109 | - * @param string $order The sort order for the resulting collection (one of | |
| 110 | - * this interface's `SORT_*` constants). | |
| 118 | + * @param string | null $propertyOrMethod The property, method, or array key | |
| 119 | + * to sort by. | |
| 120 | + * @param Sort $order The sort order for the resulting collection. | |
| 111 | 121 | * |
| 112 | 122 | * @return CollectionInterface<T> |
| 123 | + * | |
| 124 | + * @throws InvalidPropertyOrMethod if the $propertyOrMethod does not exist | |
| 125 | + * on the elements in this collection. | |
| 126 | + * @throws UnsupportedOperationException if unable to call sort() on this | |
| 127 | + * collection. | |
| 113 | 128 | */ |
| 114 | - public function sort(string $propertyOrMethod, string $order = self::SORT_ASC): self; | |
| 129 | + public function sort(?string $propertyOrMethod = null, Sort $order = Sort::Ascending) : self; | |
| 115 | 130 | /** |
| 116 | 131 | * Filter out items of the collection which don't match the criteria of |
| 117 | 132 | * given callback. |
| 118 | 133 | * |
| @@ -121,26 +136,32 @@ | ||
| 121 | 136 | * |
| 122 | 137 | * See the {@link http://php.net/manual/en/function.array-filter.php PHP array_filter() documentation} |
| 123 | 138 | * for examples of how the `$callback` parameter works. |
| 124 | 139 | * |
| 125 | - * @param callable(T):bool $callback A callable to use for filtering elements. | |
| 140 | + * @param callable(T): bool $callback A callable to use for filtering elements. | |
| 126 | 141 | * |
| 127 | 142 | * @return CollectionInterface<T> |
| 128 | 143 | */ |
| 129 | - public function filter(callable $callback): self; | |
| 144 | + public function filter(callable $callback) : self; | |
| 130 | 145 | /** |
| 131 | - * Create a new collection where items match the criteria of given callback. | |
| 146 | + * Create a new collection where the result of the given property, method, | |
| 147 | + * or array key of each item in the collection equals the given value. | |
| 132 | 148 | * |
| 133 | 149 | * This will always leave the original collection untouched and will return |
| 134 | 150 | * a new one. |
| 135 | 151 | * |
| 136 | - * @param string $propertyOrMethod The property or method to evaluate. | |
| 152 | + * @param string | null $propertyOrMethod The property, method, or array key | |
| 153 | + * to evaluate. If `null`, the element itself is compared to $value. | |
| 137 | 154 | * @param mixed $value The value to match. |
| 138 | 155 | * |
| 139 | 156 | * @return CollectionInterface<T> |
| 157 | + * | |
| 158 | + * @throws InvalidPropertyOrMethod if the $propertyOrMethod does not exist | |
| 159 | + * on the elements in this collection. | |
| 160 | + * @throws UnsupportedOperationException if unable to call where() on this | |
| 161 | + * collection. | |
| 140 | 162 | */ |
| 141 | - // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | |
| 142 | - public function where(string $propertyOrMethod, $value): self; | |
| 163 | + public function where(?string $propertyOrMethod, mixed $value) : self; | |
| 143 | 164 | /** |
| 144 | 165 | * Apply a given callback method on each item of the collection. |
| 145 | 166 | * |
| 146 | 167 | * This will always leave the original collection untouched. The new |
| @@ -149,9 +170,9 @@ | ||
| 149 | 170 | * |
| 150 | 171 | * See the {@link http://php.net/manual/en/function.array-map.php PHP array_map() documentation} |
| 151 | 172 | * for examples of how the `$callback` parameter works. |
| 152 | 173 | * |
| 153 | - * @param callable(T):TCallbackReturn $callback A callable to apply to each | |
| 174 | + * @param callable(T): TCallbackReturn $callback A callable to apply to each | |
| 154 | 175 | * item of the collection. |
| 155 | 176 | * |
| 156 | 177 | * @return CollectionInterface<TCallbackReturn> |
| 157 | 178 | * |
| @@ -156,10 +177,26 @@ | ||
| 156 | 177 | * @return CollectionInterface<TCallbackReturn> |
| 157 | 178 | * |
| 158 | 179 | * @template TCallbackReturn |
| 159 | 180 | */ |
| 160 | - public function map(callable $callback): self; | |
| 181 | + public function map(callable $callback) : self; | |
| 161 | 182 | /** |
| 183 | + * Apply a given callback method on each item of the collection | |
| 184 | + * to reduce it to a single value. | |
| 185 | + * | |
| 186 | + * See the {@link http://php.net/manual/en/function.array-reduce.php PHP array_reduce() documentation} | |
| 187 | + * for examples of how the `$callback` and `$initial` parameters work. | |
| 188 | + * | |
| 189 | + * @param callable(TCarry, T): TCarry $callback A callable to apply to each | |
| 190 | + * item of the collection to reduce it to a single value. | |
| 191 | + * @param TCarry $initial This is the initial value provided to the callback. | |
| 192 | + * | |
| 193 | + * @return TCarry | |
| 194 | + * | |
| 195 | + * @template TCarry | |
| 196 | + */ | |
| 197 | + public function reduce(callable $callback, mixed $initial) : mixed; | |
| 198 | + /** | |
| 162 | 199 | * Create a new collection with divergent items between current and given |
| 163 | 200 | * collection. |
| 164 | 201 | * |
| 165 | 202 | * @param CollectionInterface<T> $other The collection to check for divergent |
| @@ -165,10 +202,13 @@ | ||
| 165 | 202 | * @param CollectionInterface<T> $other The collection to check for divergent |
| 166 | 203 | * items. |
| 167 | 204 | * |
| 168 | 205 | * @return CollectionInterface<T> |
| 206 | + * | |
| 207 | + * @throws CollectionMismatchException if the compared collections are of | |
| 208 | + * differing types. | |
| 169 | 209 | */ |
| 170 | - public function diff(CollectionInterface $other): self; | |
| 210 | + public function diff(CollectionInterface $other) : self; | |
| 171 | 211 | /** |
| 172 | 212 | * Create a new collection with intersecting item between current and given |
| 173 | 213 | * collection. |
| 174 | 214 | * |
| @@ -175,10 +215,13 @@ | ||
| 175 | 215 | * @param CollectionInterface<T> $other The collection to check for |
| 176 | 216 | * intersecting items. |
| 177 | 217 | * |
| 178 | 218 | * @return CollectionInterface<T> |
| 219 | + * | |
| 220 | + * @throws CollectionMismatchException if the compared collections are of | |
| 221 | + * differing types. | |
| 179 | 222 | */ |
| 180 | - public function intersect(CollectionInterface $other): self; | |
| 223 | + public function intersect(CollectionInterface $other) : self; | |
| 181 | 224 | /** |
| 182 | 225 | * Merge current items and items of given collections into a new one. |
| 183 | 226 | * |
| 184 | 227 | * @param CollectionInterface<T> ...$collections The collections to merge. |
| @@ -183,7 +226,11 @@ | ||
| 183 | 226 | * |
| 184 | 227 | * @param CollectionInterface<T> ...$collections The collections to merge. |
| 185 | 228 | * |
| 186 | 229 | * @return CollectionInterface<T> |
| 230 | + * | |
| 231 | + * @throws CollectionMismatchException if unable to merge any of the given | |
| 232 | + * collections or items within the given collections due to type | |
| 233 | + * mismatch errors. | |
| 187 | 234 | */ |
| 188 | - public function merge(CollectionInterface ...$collections): self; | |
| 235 | + public function merge(CollectionInterface ...$collections) : self; | |
| 189 | 236 | } |