Builder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 11
dl 0
loc 56
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A toJson() 0 3 2
A offers() 0 3 2
A toXml() 0 14 2
1
<?php
2
3
namespace TPG\Pcflib;
4
5
use TPG\Pcflib\Contracts\Arrayable;
6
use TPG\Pcflib\Contracts\Jsonable;
7
8
class Builder implements Arrayable, Jsonable
9
{
10
    /**
11
     * @var OfferCollection
12
     */
13
    protected $offers;
14
15
    /**
16
     * @return OfferCollection
17
     */
18
    public function offers(): OfferCollection
19
    {
20
        return $this->offers ?: $this->offers = new OfferCollection();
21
    }
22
23
    /**
24
     * Output an array
25
     *
26
     * @return array
27
     */
28
    public function toArray(): array
29
    {
30
        return $this->offers()->toArray();
31
    }
32
33
    /**
34
     * Return a JSON encoded string.
35
     *
36
     * @param bool $pretty
37
     * @return string
38
     */
39
    public function toJson($pretty = false): string
40
    {
41
        return json_encode($this->toArray(), JSON_NUMERIC_CHECK + ($pretty ? JSON_PRETTY_PRINT : 0));
42
    }
43
44
    /**
45
     * Generate an XML string
46
     * @param string|null $filename
47
     * @return string
48
     * @throws Exceptions\MissingRequiredAttribute
49
     */
50
    public function toXml(string $filename = null)
51
    {
52
        $document = new \DOMDocument();
53
54
        $document->encoding = 'UTF-8';
55
56
        $xml = $this->offers->toXml($document);
57
58
        if ($filename) {
59
60
            $xml->save($filename);
61
        }
62
63
        return $xml->saveXML();
64
    }
65
}