| Total Complexity | 40 |
| Total Lines | 180 |
| 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) |
||
| 17 | } |
||
| 18 | |||
| 19 | public function getItems(): array |
||
| 20 | { |
||
| 21 | if ($this->getPager()->isEnabled()){ |
||
| 22 | $this->data = array_slice($this->data, $this->getPager()->getFirst(), $this->getPager()->getLimit()); |
||
| 23 | } |
||
| 24 | return array_map(function ($row) { |
||
| 25 | $item = new DataGridItem(); |
||
| 26 | $item->setRow($row); |
||
| 27 | return $item; |
||
| 28 | }, $this->data); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function getTotalCount(): int |
||
| 32 | { |
||
| 33 | return count($this->data); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function setSort(array $sort): DataProviderInterface |
||
| 37 | { |
||
| 38 | if (!empty($this->data)) { |
||
| 39 | foreach ($sort as $attribute => $order) { |
||
| 40 | usort($this->data, $this->buildCompare($attribute, $order)); |
||
| 41 | } |
||
| 42 | |||
| 43 | } |
||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function addEqualFilter(string $attribute, $value): DataProviderInterface |
||
| 48 | { |
||
| 49 | $this->data = array_filter($this->data, function ($row) use ($attribute, $value) { |
||
| 50 | if (!array_key_exists($attribute, $row)) { |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | return $row[$attribute] == $value; |
||
| 54 | }); |
||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function addLikeFilter(string $attribute, $value): DataProviderInterface |
||
| 59 | { |
||
| 60 | $this->data = array_filter($this->data, function ($row) use ($attribute, $value) { |
||
| 61 | if (!array_key_exists($attribute, $row)) { |
||
| 62 | return false; |
||
| 63 | } |
||
| 64 | return mb_strpos(mb_strtolower($row[$attribute]), mb_strtolower($value)) !== false; |
||
| 65 | }); |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface |
||
| 70 | { |
||
| 71 | $this->data = array_filter($this->data, function ($row) use ($attribute, $value, $callback) { |
||
| 72 | return call_user_func_array($callback, [$row, $attribute, $value]); |
||
| 73 | }); |
||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | |||
| 77 | protected function buildCompare($attribute, $order) |
||
| 78 | { |
||
| 79 | return function ($a, $b) use ($attribute, $order) { |
||
| 80 | if (!array_key_exists($attribute, $a) || !array_key_exists($attribute, $b)) { |
||
| 81 | return 0; |
||
| 82 | } |
||
| 83 | $attrValueA = $a[$attribute]; |
||
| 84 | $attrValueB = $b[$attribute]; |
||
| 85 | if ($attrValueA == $attrValueB) { |
||
| 86 | return 0; |
||
| 87 | } |
||
| 88 | if (($type1 = gettype($attrValueA)) != gettype($attrValueB)) { |
||
| 89 | return 0; |
||
| 90 | } |
||
| 91 | if ($type1 == 'string') { |
||
| 92 | return $order == 'ASC' ? strcmp($attrValueA, $attrValueB) : -strcmp($attrValueA, $attrValueB); |
||
| 93 | } |
||
| 94 | return $order == 'ASC' ? $attrValueA <=> $attrValueB : $attrValueB <=> $attrValueA; |
||
| 95 | }; |
||
| 96 | } |
||
| 97 | |||
| 98 | protected function equalDate($attribute, $value): void |
||
| 99 | { |
||
| 100 | $date = new DateTime($value); |
||
| 101 | $this->data = array_filter($this->data, function ($row) use ($attribute, $date) { |
||
| 102 | if (!array_key_exists($attribute, $row)) { |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | $attrValue = $row[$attribute]; |
||
| 106 | if ($attrValue instanceof DateTime) { |
||
| 107 | return date('d.m.Y', $attrValue->getTimestamp()) == date('d.m.Y', $date->getTimestamp()); |
||
| 108 | } |
||
| 109 | if (is_string($attrValue)) { |
||
| 110 | $attrDate = new DateTime($attrValue); |
||
| 111 | return date('d.m.Y', $attrDate->getTimestamp()) == date('d.m.Y', $date->getTimestamp()); |
||
| 112 | } |
||
| 113 | return false; |
||
| 114 | }); |
||
| 115 | } |
||
| 116 | |||
| 117 | protected function ltDate($attribute, $value): void |
||
| 133 | }); |
||
| 134 | } |
||
| 135 | |||
| 136 | protected function lteDate($attribute, $value): void |
||
| 137 | { |
||
| 138 | $date = (new DateTime($value))->modify('+1 day'); |
||
| 139 | $this->data = array_filter($this->data, function ($row) use ($attribute, $date) { |
||
| 140 | if (!array_key_exists($attribute, $row)) { |
||
| 141 | return false; |
||
| 142 | } |
||
| 143 | $attrValue = $row[$attribute]; |
||
| 144 | if ($attrValue instanceof DateTime) { |
||
| 145 | return $attrValue < $date; |
||
| 146 | } |
||
| 147 | if (is_string($attrValue)) { |
||
| 148 | $attrDate = new DateTime($attrValue); |
||
| 149 | return $attrDate < $date; |
||
| 150 | } |
||
| 151 | return false; |
||
| 152 | }); |
||
| 153 | } |
||
| 154 | |||
| 155 | protected function gtDate($attribute, $value): void |
||
| 156 | { |
||
| 157 | $date = (new DateTime($value))->modify('+1 day'); |
||
| 158 | $this->data = array_filter($this->data, function ($row) use ($attribute, $date) { |
||
| 159 | if (!array_key_exists($attribute, $row)) { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | $attrValue = $row[$attribute]; |
||
| 163 | if ($attrValue instanceof DateTime) { |
||
| 164 | return $attrValue >= $date; |
||
| 165 | } |
||
| 166 | if (is_string($attrValue)) { |
||
| 167 | $attrDate = new DateTime($attrValue); |
||
| 168 | return $attrDate >= $date; |
||
| 169 | } |
||
| 170 | return false; |
||
| 171 | }); |
||
| 172 | } |
||
| 173 | |||
| 174 | protected function gteDate($attribute, $value): void |
||
| 190 | }); |
||
| 191 | } |
||
| 192 | } |
||
| 193 |