|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: siim |
|
5
|
|
|
* Date: 25.01.19 |
|
6
|
|
|
* Time: 10:01 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Sf4\Api\Dto; |
|
10
|
|
|
|
|
11
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
12
|
|
|
use Sf4\Api\Dto\Traits\PaginationTrait; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractListDto extends AbstractDto |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
use PaginationTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ArrayCollection $items */ |
|
20
|
|
|
protected $items; |
|
21
|
|
|
|
|
22
|
|
|
/** @var int $total */ |
|
23
|
|
|
protected $total = 0; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int $count */ |
|
26
|
|
|
protected $count = 0; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array $filter */ |
|
29
|
|
|
protected $filter = []; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->items = new ArrayCollection(); |
|
34
|
|
|
parent::__construct(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public abstract function getListItemClass(): string; |
|
38
|
|
|
|
|
39
|
|
|
public function addItem(DtoInterface $dto) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->items->add($dto); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function toArray(): array |
|
45
|
|
|
{ |
|
46
|
|
|
$data = parent::toArray(); |
|
47
|
|
|
$data['items'] = []; |
|
48
|
|
|
foreach($this->items as $item) { |
|
49
|
|
|
if($item instanceof DtoInterface) { |
|
50
|
|
|
$data['items'] = $item->toArray(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return $data; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function populate(array $data): void |
|
57
|
|
|
{ |
|
58
|
|
|
$listItemClass = $this->getListItemClass(); |
|
59
|
|
|
foreach($data as $item) { |
|
60
|
|
|
if(is_array($item)) { |
|
61
|
|
|
$dto = new $listItemClass(); |
|
62
|
|
|
if($dto instanceof DtoInterface) { |
|
63
|
|
|
$dto->populate($item); |
|
64
|
|
|
$this->addItem($dto); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->setCount($this->items->count()); |
|
70
|
|
|
if($this->getTotal() <= 0) { |
|
71
|
|
|
$this->setTotal($this->getCount()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->setTotalPages(ceil($this->getTotal() / $this->getItemsPerPage())); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if($this->getTotalPages() > $this->getCurrentPage()) { |
|
77
|
|
|
$this->setNextPage($this->getCurrentPage() + 1); |
|
78
|
|
|
} |
|
79
|
|
|
if($this->getCurrentPage() > 1) { |
|
80
|
|
|
$this->setPreviousPage($this->getCurrentPage() - 1); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return int |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getTotal(): int |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->total; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param int $total |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setTotal(int $total): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->total = $total; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return int |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getCount(): int |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->count; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param int $count |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setCount(int $count): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->count = $count; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getFilter(): array |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->filter; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param array $filter |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setFilter(array $filter): void |
|
128
|
|
|
{ |
|
129
|
|
|
$this->filter = $filter; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|