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\Populator\Populator; |
17
|
|
|
|
18
|
|
|
abstract class AbstractRequestListDto extends AbstractRequestDto implements RequestListDtoInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
use FilterTrait; |
22
|
|
|
use OrdersTrait; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->orders = new ArrayCollection(); |
27
|
|
|
parent::__construct(); |
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
|
|
|
if($filterData) { |
48
|
|
|
$this->getFilter()->populate($filterData); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function populateOrder(array $data) |
53
|
|
|
{ |
54
|
|
|
$sortData = $this->getFieldData($data, static::FIELD_SORT); |
55
|
|
|
if($sortData) { |
56
|
|
|
$orderClass = $this->getOrderClass(); |
57
|
|
|
foreach($sortData as $orderData) { |
58
|
|
|
$orderData = $this->unpopulateObject($orderData); |
59
|
|
|
/** @var OrderInterface $order */ |
60
|
|
|
$order = new $orderClass(); |
61
|
|
|
$order->populate($orderData); |
|
|
|
|
62
|
|
|
$this->addOrder($order); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $data |
69
|
|
|
* @param string $field |
70
|
|
|
* @return array|null |
71
|
|
|
*/ |
72
|
|
|
protected function getFieldData(array $data, string $field): ?array |
73
|
|
|
{ |
74
|
|
|
if(false === isset($data[$field])) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$data = $this->unpopulateObject($data[$field]); |
79
|
|
|
|
80
|
|
|
if(is_array($data)) { |
81
|
|
|
return $data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $object |
89
|
|
|
* @return array|null |
90
|
|
|
*/ |
91
|
|
|
protected function unpopulateObject($object): ?array |
92
|
|
|
{ |
93
|
|
|
$response = null; |
94
|
|
|
if(true === is_object($object)) { |
95
|
|
|
$populator = new Populator(); |
96
|
|
|
$response = $populator->unpopulate($object); |
97
|
|
|
} else if(is_array($object)) { |
98
|
|
|
$response = $object; |
99
|
|
|
} else if(is_scalar($object)) { |
100
|
|
|
$response = [$object]; |
101
|
|
|
} |
102
|
|
|
return $response; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function toArray(): array |
109
|
|
|
{ |
110
|
|
|
$filterData = []; |
111
|
|
|
if($this->getFilter()) { |
112
|
|
|
$filterData = $this->getFilter()->toArray(); |
113
|
|
|
} |
114
|
|
|
$sortData = []; |
115
|
|
|
/** @var OrderInterface $order */ |
116
|
|
|
foreach($this->getOrders() as $order) { |
117
|
|
|
$sortData[] = $order->toArray(); |
118
|
|
|
} |
119
|
|
|
return [ |
120
|
|
|
static::FIELD_FILTER => $filterData, |
121
|
|
|
static::FIELD_SORT => $sortData |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected abstract function getFilterClass(): string; |
126
|
|
|
|
127
|
|
|
protected abstract function getOrderClass(): string; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return FilterInterface|null |
131
|
|
|
*/ |
132
|
|
|
public function getFilter(): ?FilterInterface |
133
|
|
|
{ |
134
|
|
|
if(!$this->filter) { |
135
|
|
|
$class = $this->getFilterClass(); |
136
|
|
|
$this->filter = new $class(); |
137
|
|
|
} |
138
|
|
|
return $this->filter; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|