|
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
|
|
|
if ($filterData) { |
|
48
|
|
|
$this->getFilter()->populate($filterData); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $data |
|
54
|
|
|
* @param string $field |
|
55
|
|
|
* @return array|null |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getFieldData(array $data, string $field): ?array |
|
58
|
|
|
{ |
|
59
|
|
|
if (false === isset($data[$field])) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$data = $this->objectToArray($data[$field]); |
|
64
|
|
|
|
|
65
|
|
|
if (is_array($data)) { |
|
|
|
|
|
|
66
|
|
|
return $data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return FilterInterface|null |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getFilter(): ?FilterInterface |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$this->filter) { |
|
78
|
|
|
$class = $this->getFilterClass(); |
|
79
|
|
|
$this->filter = new $class(); |
|
80
|
|
|
} |
|
81
|
|
|
return $this->filter; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
abstract protected function getFilterClass(): string; |
|
85
|
|
|
|
|
86
|
|
|
protected function populateOrder(array $data) |
|
87
|
|
|
{ |
|
88
|
|
|
$sortData = $this->getFieldData($data, static::FIELD_SORT); |
|
89
|
|
|
if ($sortData) { |
|
90
|
|
|
$orderClass = $this->getOrderClass(); |
|
91
|
|
|
foreach ($sortData as $orderData) { |
|
92
|
|
|
$orderData = $this->objectToArray($orderData); |
|
93
|
|
|
/** @var OrderInterface $order */ |
|
94
|
|
|
$order = new $orderClass(); |
|
95
|
|
|
$order->populate($orderData); |
|
96
|
|
|
$this->addOrder($order); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
abstract protected function getOrderClass(): string; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function toArray(): array |
|
107
|
|
|
{ |
|
108
|
|
|
$filterData = []; |
|
109
|
|
|
if ($this->getFilter()) { |
|
110
|
|
|
$filterData = $this->getFilter()->toArray(); |
|
111
|
|
|
} |
|
112
|
|
|
$sortData = $this->ordersToArray(); |
|
113
|
|
|
return [ |
|
114
|
|
|
static::FIELD_FILTER => $filterData, |
|
115
|
|
|
static::FIELD_SORT => $sortData |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|