1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: siim |
5
|
|
|
* Date: 29.01.19 |
6
|
|
|
* Time: 9:36 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Sf4\Api\Dto\Response; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
12
|
|
|
use Sf4\Api\Dto\DtoInterface; |
13
|
|
|
use Sf4\Api\Dto\Filter\FilterInterface; |
14
|
|
|
use Sf4\Api\Dto\Order\OrderInterface; |
15
|
|
|
use Sf4\Api\Dto\Request\RequestListDtoInterface; |
16
|
|
|
use Sf4\Api\Dto\Traits\FilterTrait; |
17
|
|
|
use Sf4\Api\Dto\Traits\ItemsTrait; |
18
|
|
|
use Sf4\Api\Dto\Traits\OrdersTrait; |
19
|
|
|
use Sf4\Api\Dto\Traits\PaginationTrait; |
20
|
|
|
|
21
|
|
|
abstract class AbstractResponseListDto extends AbstractResponseDto |
22
|
|
|
{ |
23
|
|
|
use ItemsTrait; |
24
|
|
|
use PaginationTrait; |
25
|
|
|
use FilterTrait; |
26
|
|
|
use OrdersTrait; |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->items = new ArrayCollection(); |
31
|
|
|
$this->orders = new ArrayCollection(); |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public abstract function getListItemClass(): string; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function toArray(): array |
41
|
|
|
{ |
42
|
|
|
$data = parent::toArray(); |
43
|
|
|
$data['items'] = $this->getItemsData(); |
44
|
|
|
$data[RequestListDtoInterface::FIELD_FILTER] = $this->getFilterData(); |
45
|
|
|
$data[RequestListDtoInterface::FIELD_SORT] = $this->getSortData(); |
46
|
|
|
return $data; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getItemsData(): array |
50
|
|
|
{ |
51
|
|
|
$data = []; |
52
|
|
|
/** @var DtoInterface $item */ |
53
|
|
|
foreach($this->items as $item) { |
54
|
|
|
$data[] = $item->toArray(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getFilterData(): array |
61
|
|
|
{ |
62
|
|
|
$data = []; |
63
|
|
|
if($this->getFilter() instanceof FilterInterface) { |
|
|
|
|
64
|
|
|
$data = $this->getFilter()->toArray(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getSortData(): array |
71
|
|
|
{ |
72
|
|
|
$data = []; |
73
|
|
|
/** @var OrderInterface $order */ |
74
|
|
|
foreach($this->getOrders() as $order) { |
75
|
|
|
$data[] = $order->toArray(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $data; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $data |
83
|
|
|
* @throws \ReflectionException |
84
|
|
|
*/ |
85
|
|
|
public function populate(array $data): void |
86
|
|
|
{ |
87
|
|
|
$listItemClass = $this->getListItemClass(); |
88
|
|
|
foreach($data as $item) { |
89
|
|
|
if(is_array($item)) { |
90
|
|
|
$dto = new $listItemClass(); |
91
|
|
|
if($dto instanceof DtoInterface) { |
92
|
|
|
$dto->populate($item); |
93
|
|
|
$this->addItem($dto); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->setCount($this->items->count()); |
99
|
|
|
if($this->getTotal() <= 0) { |
100
|
|
|
$this->setTotal($this->getCount()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->setTotalPages(ceil($this->getTotal() / $this->getItemsPerPage())); |
|
|
|
|
104
|
|
|
|
105
|
|
|
if($this->getTotalPages() > $this->getCurrentPage()) { |
106
|
|
|
$this->setNextPage($this->getCurrentPage() + 1); |
107
|
|
|
} |
108
|
|
|
if($this->getCurrentPage() > 1) { |
109
|
|
|
$this->setPreviousPage($this->getCurrentPage() - 1); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|