| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\Service; |
| 4 |
|
| 5 |
use App\Billingo\Error\BillingoError; |
| 6 |
|
| 7 |
class SelfTest |
| 8 |
{ |
| 9 |
|
| 10 |
private ?array $errors = null; |
| 11 |
|
| 12 |
public function __construct(private readonly string $name, private readonly array $controlTree) |
| 13 |
{ |
| 14 |
} |
| 15 |
|
| 16 |
public function getName(): string |
| 17 |
{ |
| 18 |
return $this->name; |
| 19 |
} |
| 20 |
|
| 21 |
public function getControlTree(): array |
| 22 |
{ |
| 23 |
return $this->controlTree; |
| 24 |
} |
| 25 |
|
| 26 |
public function getErrors(): ?array |
| 27 |
{ |
| 28 |
if ($this->errors) { |
| 29 |
|
| 30 |
return $this->errors; |
| 31 |
} |
| 32 |
|
| 33 |
$this->errors = $this->findErrors($this->controlTree); |
| 34 |
|
| 35 |
return $this->errors; |
| 36 |
} |
| 37 |
|
| 38 |
private function findErrors(array $data): ?array |
| 39 |
{ |
| 40 |
$errors = []; |
| 41 |
|
| 42 |
foreach ($data as $class => $status) { |
| 43 |
|
| 44 |
if (is_array($status)) { |
| 45 |
$localError = $this->findErrors($status); |
| 46 |
|
| 47 |
if (!empty($localError)) { |
| 48 |
|
| 49 |
$errors[$class] = $localError; |
| 50 |
} |
| 51 |
} else { |
| 52 |
if ($status instanceof BillingoError) { |
| 53 |
$errors[$class] = $status; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return empty($errors) ? null : $errors; |
| 59 |
} |
| 60 |
} |
| 61 |
|