Completed
Push — master ( c822e5...a5c9dc )
by Joao
04:27
created

ResponseBag::getCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ByJG\RestServer;
4
5
use ByJG\AnyDataset\Model\ObjectHandler;
6
use ByJG\RestServer\Exception\HttpResponseException;
7
use ByJG\Util\XmlUtil;
8
use DOMNode;
9
10
class ResponseBag
11
{
12
13
    protected $collection;
14
15
    public function add($object)
16
    {
17
        if (!is_object($object) && !is_array($object)) {
18
            throw new HttpResponseException('You can add only object');
19
        }
20
        $this->collection[] = $object;
21
    }
22
23
    /**
24
     *
25
     * @param DOMNode $current
26
     * @return \DOMDocument XML Node
27
     */
28
    public function process(DOMNode $current = null, $annotationPrefix = 'object')
29
    {
30
        $xmlDoc = null;
31
        if (is_null($current)) {
32
            $xmlDoc = XmlUtil::CreateXmlDocument();
33
            $current = XmlUtil::CreateChild($xmlDoc, "root");
34
        }
35
36
        foreach ((array) $this->collection as $object) {
37
            if ($object instanceof ResponseBag) {
38
                $object->process($current);
39
            } else {
40
                $objHandler = new ObjectHandler($current, $object, $annotationPrefix);
41
                $objHandler->CreateObjectFromModel();
42
            }
43
        }
44
45
        return $xmlDoc;
46
    }
47
48
    public function getCollection()
49
    {
50
        return $this->collection;
51
    }
52
}
53