@@ -240,4 +240,103 @@ public function isNotEmptyArray(string $message = ''): self
240240
241241 return $ this ;
242242 }
243+
244+ /**
245+ * Asserts that every element in the array satisfies the given callback.
246+ *
247+ * This method checks if all elements pass the condition defined by the callback.
248+ * The callback receives the value and optionally the key.
249+ *
250+ * Example usage:
251+ * fact([2, 4, 6])->every(fn($v) => $v % 2 === 0); // Passes
252+ * fact([1, 2, 3])->every(fn($v) => $v > 5); // Fails
253+ *
254+ * @param callable $callback The function to test each element (receives value and key).
255+ * @param string $message Optional custom error message.
256+ *
257+ * @return self Enables fluent chaining of assertion methods.
258+ */
259+ public function every (callable $ callback , string $ message = '' ): self
260+ {
261+ $ array = $ this ->variable ;
262+ if (!is_array ($ array )) {
263+ Assert::assertTrue (false , $ message ?: 'Variable is not an array. ' );
264+ }
265+ if (empty ($ array )) {
266+ Assert::assertTrue (false , $ message ?: 'Array is empty, cannot evaluate condition on elements. ' );
267+ }
268+ foreach ($ array as $ key => $ value ) {
269+ if (!$ callback ($ value , $ key )) {
270+ Assert::assertTrue (false , $ message ?: 'Not all elements satisfy the condition. ' );
271+ }
272+ }
273+ Assert::assertTrue (true , $ message );
274+ return $ this ;
275+ }
276+
277+ /**
278+ * Asserts that at least one element in the array satisfies the given callback.
279+ *
280+ * This method checks if any element passes the condition defined by the callback.
281+ * The callback receives the value and optionally the key.
282+ *
283+ * Example usage:
284+ * fact([1, 2, 3])->some(fn($v) => $v > 2); // Passes
285+ * fact([1, 2, 3])->some(fn($v) => $v > 10); // Fails
286+ *
287+ * @param callable $callback The function to test each element (receives value and key).
288+ * @param string $message Optional custom error message.
289+ *
290+ * @return self Enables fluent chaining of assertion methods.
291+ */
292+ public function some (callable $ callback , string $ message = '' ): self
293+ {
294+ $ array = $ this ->variable ;
295+ if (!is_array ($ array )) {
296+ Assert::assertTrue (false , $ message ?: 'Variable is not an array. ' );
297+ }
298+ if (empty ($ array )) {
299+ Assert::assertTrue (false , $ message ?: 'Array is empty, cannot evaluate condition on elements. ' );
300+ }
301+ foreach ($ array as $ key => $ value ) {
302+ if ($ callback ($ value , $ key )) {
303+ Assert::assertTrue (true , $ message );
304+ return $ this ;
305+ }
306+ }
307+ Assert::assertTrue (false , $ message ?: 'No elements satisfy the condition. ' );
308+ }
309+
310+ /**
311+ * Asserts that no elements in the array satisfy the given callback.
312+ *
313+ * This method checks if none of the elements pass the condition defined by the callback.
314+ * The callback receives the value and optionally the key.
315+ *
316+ * Example usage:
317+ * fact([1, 2, 3])->none(fn($v) => $v > 10); // Passes
318+ * fact([1, 2, 3])->none(fn($v) => $v > 2); // Fails
319+ *
320+ * @param callable $callback The function to test each element (receives value and key).
321+ * @param string $message Optional custom error message.
322+ *
323+ * @return self Enables fluent chaining of assertion methods.
324+ */
325+ public function none (callable $ callback , string $ message = '' ): self
326+ {
327+ $ array = $ this ->variable ;
328+ if (!is_array ($ array )) {
329+ Assert::assertTrue (false , $ message ?: 'Variable is not an array. ' );
330+ }
331+ if (empty ($ array )) {
332+ Assert::assertTrue (false , $ message ?: 'Array is empty, cannot evaluate condition on elements. ' );
333+ }
334+ foreach ($ array as $ key => $ value ) {
335+ if ($ callback ($ value , $ key )) {
336+ Assert::assertTrue (false , $ message ?: 'At least one element satisfies the condition. ' );
337+ }
338+ }
339+ Assert::assertTrue (true , $ message );
340+ return $ this ;
341+ }
243342}
0 commit comments