Completed
Pull Request — master (#51)
by Konrad
03:32
created

ItemsResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusElasticsearchPlugin\Controller\Response;
6
7
use BitBag\SyliusElasticsearchPlugin\Controller\Response\DTO\Item;
8
9
final class ItemsResponse
10
{
11
    /** @var array|Item[] */
12
    private $items;
13
14
    private function __construct(array $itemsList)
15
    {
16
        $this->items = $itemsList;
17
    }
18
19
    public static function createEmpty(): self
20
    {
21
        return new self([]);
22
    }
23
24
    public function addItem(DTO\Item $item): void
25
    {
26
        $this->items[] = $item;
27
    }
28
29
    public function all(): \Traversable
30
    {
31
        foreach ($this->items as $item) {
32
            yield $item->toArray();
33
        }
34
    }
35
36
    public function toArray(): array
37
    {
38
        return ['items' => iterator_to_array($this->all())];
39
    }
40
}
41