1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: siim |
5
|
|
|
* Date: 29.01.19 |
6
|
|
|
* Time: 9:45 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Sf4\Api\Dto\Request; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
12
|
|
|
use Sf4\Api\Dto\Filter\FilterInterface; |
13
|
|
|
use Sf4\Api\Dto\Order\OrderInterface; |
14
|
|
|
use Sf4\Api\Dto\Traits\FilterTrait; |
15
|
|
|
use Sf4\Api\Dto\Traits\OrdersTrait; |
16
|
|
|
use Sf4\Api\Utils\Traits\ArrayCollectionToArrayTrait; |
17
|
|
|
|
18
|
|
|
abstract class AbstractRequestListDto extends AbstractRequestDto implements RequestListDtoInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
use FilterTrait; |
22
|
|
|
use OrdersTrait; |
23
|
|
|
use ArrayCollectionToArrayTrait; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->orders = new ArrayCollection(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $data |
32
|
|
|
* @throws \ReflectionException |
33
|
|
|
*/ |
34
|
|
|
public function populate(array $data): void |
35
|
|
|
{ |
36
|
|
|
$this->populateFilter($data); |
37
|
|
|
$this->populateOrder($data); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $data |
42
|
|
|
* @throws \ReflectionException |
43
|
|
|
*/ |
44
|
|
|
protected function populateFilter(array $data) |
45
|
|
|
{ |
46
|
|
|
$filterData = $this->getFieldData($data, static::FIELD_FILTER); |
47
|
|
|
$filter = $this->getFilter(); |
48
|
|
|
if ($filterData && $filter) { |
49
|
|
|
$filter->populate($filterData); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $data |
55
|
|
|
* @param string $field |
56
|
|
|
* @return array|null |
57
|
|
|
*/ |
58
|
|
|
protected function getFieldData(array $data, string $field): ?array |
59
|
|
|
{ |
60
|
|
|
if (false === isset($data[$field])) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$data = $this->objectToArray($data[$field]); |
65
|
|
|
|
66
|
|
|
if (is_array($data)) { |
|
|
|
|
67
|
|
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return FilterInterface|null |
75
|
|
|
*/ |
76
|
|
|
public function getFilter(): ?FilterInterface |
77
|
|
|
{ |
78
|
|
|
if (!$this->filter) { |
79
|
|
|
$class = $this->getFilterClass(); |
80
|
|
|
$this->filter = new $class(); |
81
|
|
|
} |
82
|
|
|
return $this->filter; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
abstract protected function getFilterClass(): string; |
86
|
|
|
|
87
|
|
|
protected function populateOrder(array $data) |
88
|
|
|
{ |
89
|
|
|
$sortData = $this->getFieldData($data, static::FIELD_SORT); |
90
|
|
|
if ($sortData) { |
91
|
|
|
$orderClass = $this->getOrderClass(); |
92
|
|
|
foreach ($sortData as $orderData) { |
93
|
|
|
$orderData = $this->objectToArray($orderData); |
94
|
|
|
/** @var OrderInterface $order */ |
95
|
|
|
$order = new $orderClass(); |
96
|
|
|
$order->populate($orderData); |
97
|
|
|
$this->addOrder($order); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
abstract protected function getOrderClass(): string; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function toArray(): array |
108
|
|
|
{ |
109
|
|
|
$filterData = []; |
110
|
|
|
if ($this->getFilter()) { |
111
|
|
|
$filterData = $this->getFilter()->toArray(); |
112
|
|
|
} |
113
|
|
|
$sortData = $this->ordersToArray(); |
114
|
|
|
return [ |
115
|
|
|
static::FIELD_FILTER => $filterData, |
116
|
|
|
static::FIELD_SORT => $sortData |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|