| Total Complexity | 41 |
| Total Lines | 177 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ArrayDataProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ArrayDataProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ArrayDataProvider extends DataProvider |
||
| 11 | { |
||
| 12 | protected $data; |
||
| 13 | |||
| 14 | public function __construct(array $data) |
||
| 15 | { |
||
| 16 | $this->data = $data; |
||
| 17 | } |
||
| 18 | |||
| 19 | public function getItems(): array |
||
| 20 | { |
||
| 21 | return array_map(function ($row) { |
||
| 22 | $item = new DataGridItem(); |
||
| 23 | $item->setRow($row); |
||
| 24 | return $item; |
||
| 25 | }, $this->data); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function getTotalCount(): int |
||
| 29 | { |
||
| 30 | return count($this->data); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function setSort(array $sort): DataProviderInterface |
||
| 34 | { |
||
| 35 | if (!empty($this->data)) { |
||
| 36 | foreach ($sort as $attribute => $order) { |
||
| 37 | usort($this->data, $this->buildCompare($attribute, $order)); |
||
| 38 | } |
||
| 39 | |||
| 40 | } |
||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function addEqualFilter(string $attribute, $value): DataProviderInterface |
||
| 45 | { |
||
| 46 | $this->data = array_filter($this->data, function ($row) use ($attribute, $value) { |
||
| 47 | if (!array_key_exists($attribute, $row)) { |
||
| 48 | return false; |
||
| 49 | } |
||
| 50 | return $row[$attribute] == $value; |
||
| 51 | }); |
||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function addLikeFilter(string $attribute, $value): DataProviderInterface |
||
| 56 | { |
||
| 57 | $this->data = array_filter($this->data, function ($row) use ($attribute, $value) { |
||
| 58 | if (!array_key_exists($attribute, $row)) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | return mb_strpos(mb_strtolower($row[$attribute]), mb_strtolower($value)) !== false; |
||
| 62 | }); |
||
| 63 | return $this; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface |
||
| 67 | { |
||
| 68 | $this->data = array_filter($this->data, function ($row) use ($attribute, $value, $callback) { |
||
| 69 | return call_user_func_array($callback, [$row, $attribute, $value]); |
||
| 70 | }); |
||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | protected function buildCompare($attribute, $order) |
||
| 75 | { |
||
| 76 | return function ($a, $b) use ($attribute, $order) { |
||
| 77 | if (!array_key_exists($attribute, $a) || !array_key_exists($attribute, $b)) { |
||
| 78 | return 0; |
||
| 79 | } |
||
| 80 | $attrValueA = $a[$attribute]; |
||
| 81 | $attrValueB = $b[$attribute]; |
||
| 82 | if ($attrValueA == $attrValueB) { |
||
| 83 | return 0; |
||
| 84 | } |
||
| 85 | if (($type1 = gettype($attrValueA)) != ($type2 = gettype($attrValueB))) { |
||
|
|
|||
| 86 | return 0; |
||
| 87 | } |
||
| 88 | if ($type1 == 'string') { |
||
| 89 | return $order == 'ASC' ? strcmp($attrValueA, $attrValueB) : -strcmp($attrValueA, $attrValueB); |
||
| 90 | } |
||
| 91 | return $attrValueA < $attrValueB ? ($order == 'ASC' ? -1 : 1) : ($order == 'ASC' ? 1 : -1); |
||
| 92 | }; |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function equalDate($attribute, $value): void |
||
| 96 | { |
||
| 97 | $date = new DateTime($value); |
||
| 98 | $this->data = array_filter($this->data, function ($row) use ($attribute, $date) { |
||
| 99 | if (!array_key_exists($attribute, $row)) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | $attrValue = $row[$attribute]; |
||
| 103 | if ($attrValue instanceof DateTime) { |
||
| 104 | return date('d.m.Y', $attrValue->getTimestamp()) == date('d.m.Y', $date->getTimestamp()); |
||
| 105 | } |
||
| 106 | if (is_string($attrValue)) { |
||
| 107 | $attrDate = new DateTime($attrValue); |
||
| 108 | return date('d.m.Y', $attrDate->getTimestamp()) == date('d.m.Y', $date->getTimestamp()); |
||
| 109 | } |
||
| 110 | return false; |
||
| 111 | }); |
||
| 112 | } |
||
| 113 | |||
| 114 | protected function ltDate($attribute, $value): void |
||
| 130 | }); |
||
| 131 | } |
||
| 132 | |||
| 133 | protected function lteDate($attribute, $value): void |
||
| 134 | { |
||
| 135 | $date = (new DateTime($value))->modify('+1 day'); |
||
| 136 | $this->data = array_filter($this->data, function ($row) use ($attribute, $date) { |
||
| 137 | if (!array_key_exists($attribute, $row)) { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | $attrValue = $row[$attribute]; |
||
| 141 | if ($attrValue instanceof DateTime) { |
||
| 142 | return $attrValue < $date; |
||
| 143 | } |
||
| 144 | if (is_string($attrValue)) { |
||
| 145 | $attrDate = new DateTime($attrValue); |
||
| 146 | return $attrDate < $date; |
||
| 147 | } |
||
| 148 | return false; |
||
| 149 | }); |
||
| 150 | } |
||
| 151 | |||
| 152 | protected function gtDate($attribute, $value): void |
||
| 153 | { |
||
| 154 | $date = (new DateTime($value))->modify('+1 day'); |
||
| 155 | $this->data = array_filter($this->data, function ($row) use ($attribute, $date) { |
||
| 156 | if (!array_key_exists($attribute, $row)) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | $attrValue = $row[$attribute]; |
||
| 160 | if ($attrValue instanceof DateTime) { |
||
| 161 | return $attrValue >= $date; |
||
| 162 | } |
||
| 163 | if (is_string($attrValue)) { |
||
| 164 | $attrDate = new DateTime($attrValue); |
||
| 165 | return $attrDate >= $date; |
||
| 166 | } |
||
| 167 | return false; |
||
| 168 | }); |
||
| 169 | } |
||
| 170 | |||
| 171 | protected function gteDate($attribute, $value): void |
||
| 187 | }); |
||
| 188 | } |
||
| 189 | } |
||
| 190 |