| 1 | <?php |
||
| 6 | class Sanitizer |
||
| 7 | { |
||
| 8 | /** @var \Wandu\Validator\Validator */ |
||
| 9 | protected $validator; |
||
| 10 | |||
| 11 | /** @var string */ |
||
| 12 | protected $T; |
||
| 13 | |||
| 14 | /** @var \ReflectionClass */ |
||
| 15 | private $refl; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param string|\Wandu\Validator\Contracts\RuleInterface|\Wandu\Validator\Contracts\TesterInterface|\Wandu\Validator\Validator $rule |
||
| 19 | * @param string $T |
||
| 20 | */ |
||
| 21 | 1 | public function __construct($rule, string $T = null) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * @param mixed $data |
||
| 33 | * @return mixed|{$this->T} |
||
|
|
|||
| 34 | */ |
||
| 35 | 1 | public function sanitize($data) |
|
| 36 | { |
||
| 37 | 1 | $this->validator->assert($data); |
|
| 38 | 1 | if (!$this->T || !class_exists($this->T)) return $data; |
|
| 39 | 1 | $refl = $this->getReflectionClass(); |
|
| 40 | 1 | $instance = $refl->newInstanceWithoutConstructor(); |
|
| 41 | 1 | foreach ($refl->getProperties() as $reflectionProperty) { |
|
| 42 | 1 | $propertyName = $reflectionProperty->getName(); |
|
| 43 | 1 | if (isset($data[$propertyName])) { |
|
| 44 | 1 | $reflectionProperty->setAccessible(true); |
|
| 45 | 1 | $reflectionProperty->setValue($instance, $data[$propertyName]); |
|
| 46 | } |
||
| 47 | } |
||
| 48 | 1 | return $instance; |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return \ReflectionClass |
||
| 53 | */ |
||
| 54 | 1 | protected function getReflectionClass(): ReflectionClass |
|
| 61 | } |
||
| 62 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.