Issues (2)

src/OfferCollection.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace TPG\Pcflib;
4
5
use TPG\Pcflib\Contracts\Arrayable;
6
use TPG\Pcflib\Contracts\Jsonable;
7
use TPG\Pcflib\Traits\ArrayAccessible;
8
9
/**
10
 * Class OfferCollection
11
 * @package TPG\Pcflib
12
 */
13
class OfferCollection implements \Countable, Arrayable, \ArrayAccess, Jsonable
14
{
15
    use ArrayAccessible;
16
17
    /**
18
     * @var Offer[]
19
     */
20
    protected $items = [];
21
22
    /**
23
     * OfferCollection constructor.
24
     * @param array $offers
25
     */
26
    public function __construct(array $offers = [])
27
    {
28
        $this->items = $offers;
29
    }
30
31
    /**
32
     * Add an offer to the collection
33
     *
34
     * @param mixed $offers
35
     * @return OfferCollection
36
     */
37
    public function add($offers): OfferCollection
38
    {
39
        if (is_array($offers)) {
40
            $this->items = array_merge($this->items, $offers);
41
            return $this;
42
        }
43
        $this->items[] = $offers;
44
        return $this;
45
    }
46
47
    /**
48
     * Count the number of offers
49
     *
50
     * @return int
51
     */
52
    public function count()
53
    {
54
        return count($this->items);
55
    }
56
57
    /**
58
     * Find an offer by the Shop SKU
59
     *
60
     * @param $sku
61
     * @return Offer|null
62
     */
63
    public function find($sku): ?Offer
64
    {
65
        $offers = array_filter($this->items, function (Offer $offer) use ($sku) {
66
            return $offer->toArray()['ShopSKU'] === (string)$sku;
67
        });
68
69
        if (count($offers) >= 1) {
70
            $offer = $offers[0];
71
            $offer->parent($this);
72
            return $offer;
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * Clone an Offer object
80
     *
81
     * @param $sku
82
     * @return Offer|null
83
     */
84
    public function clone($sku): ?Offer
85
    {
86
        $offer = $this->find($sku);
87
        if ($offer) {
88
            return (new Offer())->fill($offer->toArray());
89
        }
90
    }
91
92
    /**
93
     * Remove an offer from the collection
94
     *
95
     * @param $sku
96
     */
97
    public function delete($sku)
98
    {
99
        $offer = $this->find($sku);
100
        $index = array_search($offer, $this->items);
101
        unset($this->items[$index]);
102
        $this->items = array_values($this->items);
103
    }
104
105
    /**
106
     * Clear the collection
107
     *
108
     * @return $this
109
     */
110
    public function purge()
111
    {
112
        $this->items = [];
113
        return $this;
114
    }
115
116
    /**
117
     * Output an array
118
     *
119
     * @return array
120
     */
121
    public function toArray(): array
122
    {
123
        return [
124
            'Offers' => array_map(function (Offer $offer) {
125
                return $offer->toArray();
126
            }, $this->items)
127
        ];
128
    }
129
130
    /**
131
     * Return a JSON encoded string.
132
     *
133
     * @param bool $pretty
134
     * @return string
135
     */
136
    public function toJson($pretty = false): string
137
    {
138
        return json_encode($this->toArray(), JSON_NUMERIC_CHECK + ($pretty ? JSON_PRETTY_PRINT : 0));
139
    }
140
141
    /**
142
     * Generate XML from the offer collection
143
     *
144
     * @param \DOMDocument $document
145
     * @return \DOMDocument
146
     * @throws Exceptions\MissingRequiredAttribute
147
     */
148
    public function toXml(\DOMDocument $document)
149
    {
150
        $element = $document->createElement('Offers');
151
        foreach ($this->items as $offer) {
152
            $offerElenent = $element->appendChild(new \DOMElement('Offer'));
0 ignored issues
show
The call to DOMElement::__construct() has too few arguments starting with value. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
            $offerElenent = $element->appendChild(/** @scrutinizer ignore-call */ new \DOMElement('Offer'));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
153
            $document->appendChild($element);
154
            $offer->toXmlNode($offerElenent);
155
        }
156
        return $document;
157
    }
158
}
159