ReportParams::addParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Jasper report integration for PHP
7
 *
8
 * @link      https://github.com/belgattitude/soluble-jasper
9
 * @author    Vanvelthem Sébastien
10
 * @copyright Copyright (c) 2017-2019 Vanvelthem Sébastien
11
 * @license   MIT
12
 */
13
14
namespace Soluble\Jasper;
15
16
use ArrayIterator;
17
use ArrayObject;
18
use Soluble\Jasper\Exception\InvalidArgumentException;
19
20
class ReportParams implements \ArrayAccess, \IteratorAggregate
21
{
22
    /**
23
     * @var ArrayObject
24
     */
25
    private $params;
26
27
    /**
28
     * ReportParams constructor.
29
     *
30
     * @param iterable $params Report parameters as array or any traversable type (IteratorAggregate, Iterator, ArrayObject...)
31
     *
32
     * @throws InvalidArgumentException
33
     */
34 22
    public function __construct(iterable $params = [])
35
    {
36 22
        $this->params = new ArrayObject();
37
38
        try {
39 22
            foreach ($params as $key => $value) {
40 19
                $current_key = $key;
41 19
                $this->offsetSet($key, $value);
42
            }
43 1
        } catch (InvalidArgumentException $e) {
44 1
            throw new InvalidArgumentException(sprintf(
45 1
                'Cannot construct ReportParams from provided $params, all keys must be non-empty strings (key: %s)',
46 1
                $current_key ?? ''
47 1
            ), $e->getCode(), $e);
48
        }
49 21
    }
50
51 15
    public function addParams(iterable $params): void
52
    {
53 15
        foreach ($params as $key => $value) {
54 15
            $this->offsetSet($key, $value);
55
        }
56 15
    }
57
58 16
    public function withMergedParams(self $params): self
59
    {
60 16
        return new self(array_merge($this->toArray(), $params->toArray()));
61
    }
62
63
    /**
64
     * @param string $param report parameter name ($P{} in jasper)
65
     * @param mixed  $value
66
     */
67 1
    public function put(string $param, $value): void
68
    {
69 1
        $this->params->offsetSet($param, $value);
70 1
    }
71
72 8
    public function getIterator(): ArrayIterator
73
    {
74 8
        return new ArrayIterator((array) $this->params);
75
    }
76
77
    /**
78
     * @param mixed $offset
79
     *
80
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
81
     */
82 1
    public function offsetExists($offset): bool
83
    {
84 1
        $this->checkStringOffset($offset);
85
86 1
        return $this->params->offsetExists($offset);
87
    }
88
89
    /**
90
     * @param mixed $offset
91
     *
92
     * @return mixed
93
     *
94
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
95
     */
96 4
    public function offsetGet($offset)
97
    {
98 4
        $this->checkStringOffset($offset);
99
100 4
        return $this->params->offsetGet($offset);
101
    }
102
103
    /**
104
     * @param mixed $offset
105
     * @param mixed $value
106
     *
107
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
108
     */
109 21
    public function offsetSet($offset, $value): void
110
    {
111 21
        $this->checkStringOffset($offset);
112 20
        $this->params->offsetSet($offset, $value);
113 20
    }
114
115
    /**
116
     * @param mixed $offset
117
     *
118
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
119
     */
120 1
    public function offsetUnset($offset): void
121
    {
122 1
        $this->checkStringOffset($offset);
123 1
        if ($this->offsetExists($offset)) {
124 1
            $this->params->offsetUnset($offset);
125
        }
126 1
    }
127
128
    /**
129
     * @return mixed[]
130
     */
131 17
    public function toArray(): array
132
    {
133 17
        return (array) $this->params;
134
    }
135
136
    /**
137
     * @param string|mixed $offset
138
     *
139
     * @throws Exception\InvalidArgumentException
140
     */
141 22
    private function checkStringOffset($offset): void
142
    {
143 22
        if (!is_string($offset)) {
144 2
            throw new Exception\InvalidArgumentException(
145 2
                sprintf(
146 2
                    'Report parameters must be a string (type: %s%s)',
147 2
                    gettype($offset),
148 2
                    is_scalar($offset) ? ', value:' . (string) $offset : ''
149
                )
150
            );
151 21
        } elseif (trim($offset) === '') {
152 1
            throw new Exception\InvalidArgumentException(
153 1
                sprintf(
154 1
                    'Report parameters must be a non-empty string.'
155
                )
156
            );
157
        }
158 21
    }
159
}
160