Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| FilterVar | |
100.00% |
31 / 31 |
|
100.00% |
10 / 10 |
14 | |
100.00% |
1 / 1 |
| getLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLabel | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| setFilter | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| getFilter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addOption | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addFlag | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFlags | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOptionsFlagsArray | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| filter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | declare(strict_types=1); |
| 7 | |
| 8 | namespace pvc\filtervar; |
| 9 | |
| 10 | |
| 11 | use pvc\filtervar\err\InvalidFilterException; |
| 12 | use pvc\filtervar\err\InvalidLabelException; |
| 13 | use pvc\interfaces\filtervar\FilterVarInterface; |
| 14 | |
| 15 | /** |
| 16 | * Class FilterVar |
| 17 | */ |
| 18 | class FilterVar implements FilterVarInterface |
| 19 | { |
| 20 | /** |
| 21 | * @var int |
| 22 | */ |
| 23 | protected int $filter; |
| 24 | |
| 25 | /** |
| 26 | * @var array <string, mixed> |
| 27 | */ |
| 28 | protected array $options = []; |
| 29 | |
| 30 | /** |
| 31 | * @var array<int, int> |
| 32 | */ |
| 33 | protected array $flags = []; |
| 34 | |
| 35 | /** |
| 36 | * @var string |
| 37 | * describes what the thing is that is being validated. For example, for a url, set the label to 'url' |
| 38 | */ |
| 39 | protected string $label; |
| 40 | |
| 41 | /** |
| 42 | * getLabel |
| 43 | * @return string |
| 44 | */ |
| 45 | public function getLabel(): string |
| 46 | { |
| 47 | return $this->label; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * setLabel |
| 52 | * @param string $label |
| 53 | */ |
| 54 | public function setLabel(string $label): void |
| 55 | { |
| 56 | if (empty($label)) { |
| 57 | throw new InvalidLabelException(); |
| 58 | } |
| 59 | $this->label = $label; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * setFilter |
| 64 | * @param int $filter |
| 65 | * @throws InvalidFilterException |
| 66 | */ |
| 67 | public function setFilter(int $filter): void |
| 68 | { |
| 69 | /** |
| 70 | * keys are the constant values, values are string name of each constant |
| 71 | */ |
| 72 | $filtervarConstants = array_flip(get_defined_constants(true)['filter']); |
| 73 | |
| 74 | /** |
| 75 | * callback will be used to remove constants which are flags and options and leave only those constants |
| 76 | * which are legitimate filters. |
| 77 | * |
| 78 | * @param string $constant |
| 79 | * @return bool |
| 80 | */ |
| 81 | $callback = function(string $constant): bool { |
| 82 | $filterPrefixes = ['FILTER_VALIDATE', 'FILTER_SANITIZE', 'FILTER_CALLBACK']; |
| 83 | /** |
| 84 | * luckily, we can take advantage of the fact that the prefix strings each have the same string length! |
| 85 | */ |
| 86 | $constantPrefix = substr($constant, 0, 15); |
| 87 | return in_array($constantPrefix, $filterPrefixes); |
| 88 | }; |
| 89 | $filters = array_filter($filtervarConstants, $callback); |
| 90 | |
| 91 | /** |
| 92 | * array keys are the constant values so if $filter is a key in the array, then it's a legit filter |
| 93 | */ |
| 94 | if (!array_key_exists($filter, $filters)) { |
| 95 | throw new InvalidFilterException(); |
| 96 | } |
| 97 | |
| 98 | $this->filter = $filter; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * getFilter |
| 103 | * @return int |
| 104 | */ |
| 105 | public function getFilter(): int |
| 106 | { |
| 107 | return $this->filter; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * addOption |
| 112 | * @param string $filterVarOption |
| 113 | * @param mixed $value |
| 114 | */ |
| 115 | public function addOption(string $filterVarOption, mixed $value): void |
| 116 | { |
| 117 | $this->options[$filterVarOption] = $value; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * getOptions |
| 122 | * @return array<string, mixed> |
| 123 | */ |
| 124 | public function getOptions(): array |
| 125 | { |
| 126 | return $this->options; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * addFlag |
| 131 | * @param int $filterFlag |
| 132 | */ |
| 133 | public function addFlag(int $filterFlag): void |
| 134 | { |
| 135 | $this->flags[] = $filterFlag; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * getFlags |
| 140 | * @return int[] |
| 141 | */ |
| 142 | public function getFlags(): array |
| 143 | { |
| 144 | return $this->flags; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * getOptionsFlagsArray |
| 149 | * @return array<string, mixed> |
| 150 | */ |
| 151 | public function getOptionsFlagsArray(): array |
| 152 | { |
| 153 | $optionsFlagsArray = []; |
| 154 | |
| 155 | if ($this->getOptions()) { |
| 156 | $optionsFlagsArray['options'] = $this->getOptions(); |
| 157 | } |
| 158 | |
| 159 | if ($this->getFlags()) { |
| 160 | $callback = function(int $carry, int $item): int { |
| 161 | $carry |= $item; |
| 162 | return $carry; |
| 163 | }; |
| 164 | $optionsFlagsArray['flags'] = array_reduce($this->getFlags(), $callback, 0); |
| 165 | } |
| 166 | |
| 167 | return $optionsFlagsArray; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @param string $value |
| 172 | * |
| 173 | * @return mixed |
| 174 | */ |
| 175 | public function filter(string $value): mixed |
| 176 | { |
| 177 | return filter_var($value, $this->getFilter(), |
| 178 | $this->getOptionsFlagsArray()); |
| 179 | } |
| 180 | } |