1<?php
2
3namespace FINDOLOGIC\Export\XML;
4
5use FINDOLOGIC\Export\Helpers\XMLHelper;
6
7class ItemsExceedCountValueException extends \RuntimeException
8{
9    public function __construct()
10    {
11        $message = 'The number of items must not exceed the count value';
12        parent::__construct($message);
13    }
14}
15
16class Page
17{
18    private $items;
19    private $start;
20    private $count;
21    private $total;
22
23    public function __construct($start, $count, $total)
24    {
25        $this->start = $start;
26        $this->count = $count;
27        $this->total = $total;
28        $this->items = [];
29    }
30
31    public function addItem(XMLItem $item)
32    {
33        array_push($this->items, $item);
34    }
35
36    public function setAllItems(array $items)
37    {
38        $this->items = $items;
39    }
40
41    /**
42     * @SuppressWarnings(PHPMD.StaticAccess)
43     */
44    public function getXml()
45    {
46        if (count($this->items) > $this->count) {
47            throw new ItemsExceedCountValueException();
48        }
49
50        $document = new \DOMDocument('1.0', 'utf-8');
51        $root = XMLHelper::createElement($document, 'findologic', ['version' => '1.0']);
52        $document->appendCHild($root);
53
54        $items = XMLHelper::createElement($document, 'items', [
55            'start' => $this->start,
56            'count' => $this->count,
57            'total' => $this->total
58        ]);
59        $root->appendChild($items);
60
61        /** @var XMLItem $item */
62        foreach ($this->items as $item) {
63            $itemDom = $item->getDomSubtree($document);
64            $items->appendChild($itemDom);
65        }
66
67        return $document;
68    }
69}
70