1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Blitz PHP framework. |
||
5 | * |
||
6 | * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace BlitzPHP\Validation; |
||
13 | |||
14 | use BlitzPHP\Contracts\Autoloader\LocatorInterface; |
||
15 | use BlitzPHP\Validation\Rules\AbstractRule; |
||
16 | use Dimtrovich\Validation\Validation as BaseValidation; |
||
17 | |||
18 | class Validation extends BaseValidation |
||
19 | { |
||
20 | public function __construct() |
||
21 | { |
||
22 | parent::__construct(config('app.language')); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
23 | |||
24 | $this->discoverRules(); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * {@inheritDoc} |
||
29 | */ |
||
30 | public function errors(): ErrorBag |
||
31 | { |
||
32 | return ErrorBag::fromBase(parent::errors()); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | * |
||
38 | * @param list<AbstractRule> $rules |
||
0 ignored issues
–
show
The type
BlitzPHP\Validation\list was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
39 | */ |
||
40 | protected function registerRules(array $rules): void |
||
41 | { |
||
42 | foreach ($rules as $key => $value) { |
||
43 | if (is_int($key)) { |
||
44 | $name = $value::name(); |
||
45 | $rule = $value; |
||
46 | } else { |
||
47 | $name = $value; |
||
48 | $rule = $key; |
||
49 | } |
||
50 | |||
51 | $this->addValidator($name, service('container')->get($rule)); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Definie les fichiers qui pourront etre considerer comme regles de validations |
||
57 | * |
||
58 | * @return list<string> Chemins absolus des fichiers |
||
59 | */ |
||
60 | protected function files(LocatorInterface $locator): array |
||
61 | { |
||
62 | $files = array_merge( |
||
63 | $locator->listFiles('Rules/'), // Regles de l'application ou des fournisseurs |
||
64 | $locator->listFiles('Validation/Rules/') // Regles internes du framework |
||
65 | ); |
||
66 | |||
67 | return array_unique($files); |
||
0 ignored issues
–
show
|
|||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Recherche toutes les regles de validation dans le framework et dans le code de l'utilisateur |
||
72 | * et collecte leurs instances pour fonctionner avec eux. |
||
73 | */ |
||
74 | private function discoverRules() |
||
75 | { |
||
76 | $files = $this->files($locator = service('locator')); |
||
77 | $rules = []; |
||
78 | |||
79 | foreach ($files as $file) { |
||
80 | $className = $locator->getClassname($file); |
||
81 | |||
82 | if ($className !== '' && class_exists($className) && is_subclass_of($className, AbstractRule::class)) { |
||
83 | $rules[] = $className; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | $this->registerRules($rules); |
||
88 | } |
||
89 | } |
||
90 |