Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| RichtextDoc | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| review | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Validation; |
| 6 | |
| 7 | use Celema\Sire\Contract\Validator; |
| 8 | use Celema\Sire\Extra; |
| 9 | use Celema\Sire\Result; |
| 10 | use Celema\Sire\Review; |
| 11 | use Celema\Sire\Shape; |
| 12 | use Cosray\Richtext\Validator as DocValidator; |
| 13 | use Override; |
| 14 | |
| 15 | /** |
| 16 | * Sire adapter running the writer-strict richtext document validation |
| 17 | * on a per-locale value of a richtext field or block. |
| 18 | */ |
| 19 | final class RichtextDoc implements Validator |
| 20 | { |
| 21 | private readonly Shape $shape; |
| 22 | private readonly DocValidator $validator; |
| 23 | |
| 24 | /** |
| 25 | * @param array<string, string> $classes |
| 26 | * @param array<string, string> $styles |
| 27 | */ |
| 28 | public function __construct(array $classes = [], array $styles = []) |
| 29 | { |
| 30 | $this->validator = new DocValidator($classes, $styles); |
| 31 | $this->shape = new Shape() |
| 32 | ->rules(Validators::registry()) |
| 33 | ->extra(Extra::Allow); |
| 34 | $this->shape->review($this->review(...)); |
| 35 | } |
| 36 | |
| 37 | #[Override] |
| 38 | public function validate(array $data): Result |
| 39 | { |
| 40 | return $this->shape->validate($data); |
| 41 | } |
| 42 | |
| 43 | private function review(Review $review): void |
| 44 | { |
| 45 | foreach ($this->validator->validate($review->values()) as $error) { |
| 46 | $review->addError('', $error, 'richtext'); |
| 47 | } |
| 48 | } |
| 49 | } |