Mediadevs /
Validator
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Mediadevs\Validator\Helpers; |
||
| 4 | |||
| 5 | use ReflectionClass; |
||
| 6 | use ReflectionException; |
||
| 7 | use Mediadevs\Validator\Filters\FilterInterface; |
||
| 8 | |||
| 9 | class ClassParser |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The identifier for this filter. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private $identifier; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The aliases for this filter. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private $aliases = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The message / responses for this filter. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $message = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Parsing the filter to collect it's properties and configuration. |
||
| 34 | * |
||
| 35 | * @param string $namespace |
||
| 36 | * |
||
| 37 | * @throws ReflectionException |
||
| 38 | * |
||
| 39 | * @return self |
||
| 40 | */ |
||
| 41 | public function parse(string $namespace): self |
||
| 42 | { |
||
| 43 | // Collecting the classname and instantiating the class |
||
| 44 | $filename = $this->getFileName($namespace); |
||
| 45 | $this->loadFile($filename); |
||
| 46 | |||
| 47 | // Creating a workable instance of the class with empty arrays so the constructor won't fail. |
||
| 48 | $filter = (new $namespace(array(), array())); |
||
| 49 | |||
| 50 | // Validating whether all the properties exist. |
||
| 51 | $hasIdentifier = $this->hasProperty($filter, 'identifier'); |
||
| 52 | $hasAliases = $this->hasProperty($filter, 'aliases'); |
||
| 53 | $hasMessage = $this->hasProperty($filter, 'message'); |
||
| 54 | |||
| 55 | // Validating whether the properties exist. |
||
| 56 | if ($hasIdentifier && $hasAliases) { |
||
| 57 | $this->identifier = $this->getProperty($filter, 'identifier'); |
||
| 58 | $this->aliases = $this->getProperty($filter, 'aliases'); |
||
| 59 | |||
| 60 | // This only comes in action when it is a custom filter. |
||
| 61 | if ($hasMessage) { |
||
| 62 | $this->message = $this->getProperty($filter, 'message'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Collecting the filename from the filter using a reflection class. |
||
| 71 | * |
||
| 72 | * @param string $namespace |
||
| 73 | * |
||
| 74 | * @throws ReflectionException |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | private function getFileName(string $namespace): string |
||
| 79 | { |
||
| 80 | return (new ReflectionClass($namespace))->getFileName(); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Requiring the file in case it is a custom filter, else the files will be automatically loaded by composer. |
||
| 85 | * |
||
| 86 | * @param string $filename |
||
| 87 | * |
||
| 88 | * @return mixed |
||
| 89 | */ |
||
| 90 | private function loadFile(string $filename) |
||
| 91 | { |
||
| 92 | return require_once $filename; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Whether the filter has the given property or not. |
||
| 97 | * |
||
| 98 | * @param FilterInterface $filter |
||
| 99 | * @param string $property |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | private function hasProperty(FilterInterface $filter, string $property): bool |
||
| 104 | { |
||
| 105 | return (bool) property_exists($filter, $property) ? true : false; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Collecting the property from the filter. |
||
| 110 | * |
||
| 111 | * @param FilterInterface $filter |
||
| 112 | * @param string $property |
||
| 113 | * |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | private function getProperty(FilterInterface $filter, string $property) |
||
| 117 | { |
||
| 118 | return $filter->getProperty($property); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Collecting the identifier from this filter. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getIdentifier(): string |
||
| 127 | { |
||
| 128 | return $this->identifier; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Collecting the aliases from this filter. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public function getAliases(): array |
||
| 137 | { |
||
| 138 | return $this->aliases; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Collecting the message from this filter. |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function getMessage(): array |
||
| 147 | { |
||
| 148 | return $this->message; |
||
| 149 | } |
||
| 150 | } |
||
| 151 |