Skip to content

Commit 49080d4

Browse files
committed
CS & Update README.md
1 parent 391920d commit 49080d4

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,64 @@ fact([1, 2, 3])->none(fn($v) => $v > 10); // Passes
7777
fact([1, 2, 3])->none(fn($v) => $v > 2); // Fails
7878
```
7979

80+
### Boolean assertions
81+
```php
82+
fact(true)->true(); // Passes
83+
fact(1)->true(); // Fails due to strict comparison
84+
85+
fact(false)->notTrue(); // Passes
86+
fact(true)->notTrue(); // Fails
87+
88+
fact(false)->false(); // Passes
89+
fact(0)->false(); // Fails due to strict comparison
90+
91+
fact(true)->notFalse(); // Passes
92+
fact(false)->notFalse(); // Fails
93+
```
94+
95+
### Comparison and equality assertions
96+
```php
97+
fact(42)->is(42); // Passes
98+
fact(42)->is('42'); // Fails due to type difference
99+
100+
fact(42)->equals(42); // Passes
101+
fact(42)->equals('42'); // Passes due to loose comparison
102+
103+
fact(42)->not(43); // Passes
104+
fact(42)->not(42); // Fails
105+
```
106+
107+
### Null assertions
108+
```php
109+
fact(null)->null(); // Passes
110+
fact('')->null(); // Fails
111+
112+
fact(42)->notNull(); // Passes
113+
fact(null)->notNull(); // Fails
114+
```
115+
116+
### Numeric assertions
117+
```php
118+
fact(5)->isLowerThan(10); // Passes
119+
fact(10)->isLowerThan(5); // Fails
120+
121+
fact(10)->isGreaterThan(5); // Passes
122+
fact(5)->isGreaterThan(10); // Fails
123+
124+
fact(5)->isPositive(); // Passes
125+
fact(-3)->isPositive(); // Fails
126+
127+
fact(-3)->isNegative(); // Passes
128+
fact(5)->isNegative(); // Fails
129+
130+
fact(0)->isZero(); // Passes
131+
fact(0.0)->isZero(); // Passes
132+
fact(1)->isZero(); // Fails
133+
134+
fact(5)->isBetween(1, 10); // Passes
135+
fact(15)->isBetween(1, 10); // Fails
136+
```
137+
80138
### String assertions
81139
```php
82140
fact('abc123')->matchesRegularExpression('/^[a-z]+\d+$/'); // Passes

src/Traits/ArrayAssertions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,23 @@ public function isNotEmptyArray(string $message = ''): self
259259
public function every(callable $callback, string $message = ''): self
260260
{
261261
$array = $this->variable;
262+
262263
if (!is_array($array)) {
263264
Assert::assertTrue(false, $message ?: 'Variable is not an array.');
264265
}
266+
265267
if (empty($array)) {
266268
Assert::assertTrue(false, $message ?: 'Array is empty, cannot evaluate condition on elements.');
267269
}
270+
268271
foreach ($array as $key => $value) {
269272
if (!$callback($value, $key)) {
270273
Assert::assertTrue(false, $message ?: 'Not all elements satisfy the condition.');
271274
}
272275
}
276+
273277
Assert::assertTrue(true, $message);
278+
274279
return $this;
275280
}
276281

@@ -292,18 +297,23 @@ public function every(callable $callback, string $message = ''): self
292297
public function some(callable $callback, string $message = ''): self
293298
{
294299
$array = $this->variable;
300+
295301
if (!is_array($array)) {
296302
Assert::assertTrue(false, $message ?: 'Variable is not an array.');
297303
}
304+
298305
if (empty($array)) {
299306
Assert::assertTrue(false, $message ?: 'Array is empty, cannot evaluate condition on elements.');
300307
}
308+
301309
foreach ($array as $key => $value) {
302310
if ($callback($value, $key)) {
303311
Assert::assertTrue(true, $message);
312+
304313
return $this;
305314
}
306315
}
316+
307317
Assert::assertTrue(false, $message ?: 'No elements satisfy the condition.');
308318
}
309319

@@ -325,18 +335,23 @@ public function some(callable $callback, string $message = ''): self
325335
public function none(callable $callback, string $message = ''): self
326336
{
327337
$array = $this->variable;
338+
328339
if (!is_array($array)) {
329340
Assert::assertTrue(false, $message ?: 'Variable is not an array.');
330341
}
342+
331343
if (empty($array)) {
332344
Assert::assertTrue(false, $message ?: 'Array is empty, cannot evaluate condition on elements.');
333345
}
346+
334347
foreach ($array as $key => $value) {
335348
if ($callback($value, $key)) {
336349
Assert::assertTrue(false, $message ?: 'At least one element satisfies the condition.');
337350
}
338351
}
352+
339353
Assert::assertTrue(true, $message);
354+
340355
return $this;
341356
}
342357
}

0 commit comments

Comments
 (0)