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\Request\RequestListDtoInterface; |
15
|
|
|
use Sf4\Api\Dto\Traits\FilterTrait; |
16
|
|
|
use Sf4\Api\Dto\Traits\ItemsTrait; |
17
|
|
|
use Sf4\Api\Dto\Traits\OrdersTrait; |
18
|
|
|
use Sf4\Api\Dto\Traits\PaginationTrait; |
19
|
|
|
use Sf4\Api\Utils\Traits\ArrayCollectionToArrayTrait; |
20
|
|
|
|
21
|
|
|
abstract class AbstractResponseListDto extends AbstractResponseDto |
22
|
|
|
{ |
23
|
|
|
use ItemsTrait; |
24
|
|
|
use PaginationTrait; |
25
|
|
|
use FilterTrait; |
26
|
|
|
use OrdersTrait; |
27
|
|
|
use ArrayCollectionToArrayTrait; |
28
|
|
|
|
29
|
|
|
public const ITEMS = 'items'; |
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->items = new ArrayCollection(); |
34
|
|
|
$this->orders = new ArrayCollection(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function toArray(): array |
41
|
|
|
{ |
42
|
|
|
$data = parent::toArray(); |
43
|
|
|
$data[static::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
|
|
|
return $this->arrayCollectionToArray($this->items); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getFilterData(): array |
55
|
|
|
{ |
56
|
|
|
$data = []; |
57
|
|
|
if ($this->getFilter() instanceof FilterInterface) { |
|
|
|
|
58
|
|
|
$data = $this->getFilter()->toArray(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getSortData(): array |
65
|
|
|
{ |
66
|
|
|
return $this->ordersToArray(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $data |
71
|
|
|
* @throws \ReflectionException |
72
|
|
|
*/ |
73
|
|
|
public function populate(array $data): void |
74
|
|
|
{ |
75
|
|
|
$listItemClass = $this->getListItemClass(); |
76
|
|
|
|
77
|
|
|
if (array_key_exists(static::ITEMS, $data)) { |
78
|
|
|
$data = $data[static::ITEMS]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($data as $item) { |
82
|
|
|
if (is_array($item)) { |
83
|
|
|
$dto = new $listItemClass(); |
84
|
|
|
if ($dto instanceof DtoInterface) { |
85
|
|
|
$dto->populate($item); |
86
|
|
|
$this->addItem($dto); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->setCount($this->items->count()); |
92
|
|
|
if ($this->getTotal() <= 0) { |
93
|
|
|
$this->setTotal($this->getCount()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->setTotalPages(ceil($this->getTotal() / $this->getItemsPerPage())); |
|
|
|
|
97
|
|
|
|
98
|
|
|
if ($this->getTotalPages() > $this->getCurrentPage()) { |
99
|
|
|
$this->setNextPage($this->getCurrentPage() + 1); |
100
|
|
|
} |
101
|
|
|
if ($this->getCurrentPage() > 1) { |
102
|
|
|
$this->setPreviousPage($this->getCurrentPage() - 1); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
abstract public function getListItemClass(): string; |
107
|
|
|
} |
108
|
|
|
|