Issues (16)

src/Parameters/CompoundParameterCollection.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace TheIconic\Tracking\GoogleAnalytics\Parameters;
4
5
use TheIconic\Tracking\GoogleAnalytics\Traits\Indexable;
6
use IteratorAggregate;
7
8
/**
9
 * Class CompoundParameterCollection
10
 *
11
 * Contains a set of compound parameters. The name of the collection is prepend before the compound parameter
12
 * names. The name can be indexed by the placeholder as specified in the Indexable trait.
13
 *
14
 * @package TheIconic\Tracking\GoogleAnalytics\Parameters
15
 */
16
abstract class CompoundParameterCollection implements IteratorAggregate
17
{
18
    use Indexable;
19
20
    /**
21
     * Name for the collection of compound parameters. Its prepend to the payload names
22
     * of the compound parameter.
23
     *
24
     * @var string
25
     */
26
    protected $name = '';
27
28
    /**
29
     * Holds all the compound parameters that belong to the collection.
30
     *
31
     * @var CompoundParameter[]
32
     */
33
    protected $items = [];
34
35
    /**
36
     * Indexes the name in case it has a placeholder.
37
     *
38
     * @param string $index
39
     */
40
    public function __construct($index = '')
41
    {
42
        $this->name = $this->addIndex($this->name, $index);
0 ignored issues
show
$index of type string is incompatible with the type integer expected by parameter $index of TheIconic\Tracking\Googl...rCollection::addIndex(). ( Ignorable by Annotation )

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

42
        $this->name = $this->addIndex($this->name, /** @scrutinizer ignore-type */ $index);
Loading history...
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function add(CompoundParameter $compoundParameter)
49
    {
50
        $this->items[] = $compoundParameter;
51
    }
52
53
    /**
54
     * @internal
55
     * @inheritDoc
56
     */
57
    public function getParametersArray()
58
    {
59
        $parameters = [];
60
61
        foreach ($this as $number => $compoundParameter) {
62
            $currentParameters = [];
63
64
            foreach ($compoundParameter->getParameters() as $name => $value) {
65
                $currentParameters[$this->name . ($number + 1) . $name] = $value;
66
            }
67
68
            $parameters = array_merge($parameters, $currentParameters);
69
        }
70
71
        return $parameters;
72
    }
73
74
    /**
75
     * Gets the human readable items for the parameter.
76
     *
77
     * @internal
78
     * @return array
79
     */
80
    public function getReadableItems()
81
    {
82
        $readablesItems = [];
83
84
        foreach ($this->items as $key => $item) {
85
            array_push($readablesItems, $item->getReadableParameters());
86
        }
87
88
        return $readablesItems;
89
    }
90
91
    /**
92
     * @internal
93
     * @inheritDoc
94
     */
95
    public function getIterator()
96
    {
97
        return new \ArrayIterator($this->items);
98
    }
99
}
100