This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Albert221\Validation; | ||
|
||
use Countable; | ||
use IteratorAggregate; | ||
|
||
class Verdicts implements Countable, IteratorAggregate | ||
{ | ||
private $verdicts; | ||
|
||
public function __construct(array $verdicts) | ||
{ | ||
$this->verdicts = $verdicts; | ||
} | ||
|
||
public function passing(): Verdicts | ||
{ | ||
return $this->filter(function (Verdict $verdict) { | ||
return $verdict->passes(); | ||
}); | ||
} | ||
|
||
public function failing(): Verdicts | ||
{ | ||
return $this->filter(function (Verdict $verdict) { | ||
return !$verdict->passes(); | ||
}); | ||
} | ||
|
||
public function forField(string $fieldName): Verdicts | ||
{ | ||
return $this->filter(function (Verdict $verdict) use ($fieldName) { | ||
return $verdict->getField()->getName() === $fieldName; | ||
}); | ||
} | ||
|
||
public function map(callable $function): Verdicts | ||
{ | ||
return new static(array_map($function, $this->verdicts)); | ||
} | ||
|
||
public function filter(callable $function): Verdicts | ||
{ | ||
return new static(array_filter($this->verdicts, $function)); | ||
} | ||
|
||
public function reduce(callable $function, $initial = null) | ||
{ | ||
return new static(array_reduce($this->verdicts, $function, $initial)); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->verdicts; | ||
} | ||
|
||
public function /* Countable */ count(): int | ||
{ | ||
return count($this->verdicts); | ||
} | ||
|
||
public function /* IteratorAggregate */ getIterator(): \Traversable | ||
{ | ||
return new \ArrayIterator($this->verdicts); | ||
} | ||
} |