Completed
Push — master ( b3758e...d24b37 )
by Sébastien
04:19
created

ReportParams   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 125
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A addParams() 0 4 2
A withMergedParams() 0 5 1
A toArray() 0 3 1
A offsetExists() 0 5 1
A put() 0 3 1
A __construct() 0 13 3
A offsetUnset() 0 5 2
A checkStringOffset() 0 14 4
A offsetSet() 0 4 1
A offsetGet() 0 5 1
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 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 16
    public function __construct(iterable $params = [])
35
    {
36 16
        $this->params = new ArrayObject();
37
        try {
38 16
            foreach ($params as $key => $value) {
39 13
                $current_key = $key;
40 16
                $this->offsetSet($key, $value);
41
            }
42 1
        } catch (InvalidArgumentException $e) {
43 1
            throw new InvalidArgumentException(sprintf(
44 1
                'Cannot construct ReportParams from provided $params, all keys must be non-empty strings (key: %s)',
45 1
                $current_key ?? ''
46 1
            ), $e->getCode(), $e);
47
        }
48 15
    }
49
50 9
    public function addParams(iterable $params): void
51
    {
52 9
        foreach ($params as $key => $value) {
53 9
            $this->offsetSet($key, $value);
54
        }
55 9
    }
56
57 10
    public function withMergedParams(ReportParams $params): ReportParams
58
    {
59 10
        $newParams = array_merge($this->toArray(), $params->toArray());
60
61 10
        return new self($newParams);
62
    }
63
64
    /**
65
     * @param string $param report parameter name ($P{} in jasper)
66
     * @param mixed  $value
67
     */
68 1
    public function put(string $param, $value): void
69
    {
70 1
        $this->params->offsetSet($param, $value);
71 1
    }
72
73 5
    public function getIterator()
74
    {
75 5
        return new ArrayIterator($this->params);
76
    }
77
78
    /**
79
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
80
     */
81 1
    public function offsetExists($offset): bool
82
    {
83 1
        $this->checkStringOffset($offset);
84
85 1
        return $this->params->offsetExists($offset);
86
    }
87
88
    /**
89
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
90
     */
91 4
    public function offsetGet($offset)
92
    {
93 4
        $this->checkStringOffset($offset);
94
95 4
        return $this->params->offsetGet($offset);
96
    }
97
98
    /**
99
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
100
     */
101 15
    public function offsetSet($offset, $value): void
102
    {
103 15
        $this->checkStringOffset($offset);
104 14
        $this->params->offsetSet($offset, $value);
105 14
    }
106
107
    /**
108
     * @throws \Soluble\Jasper\Exception\InvalidArgumentException
109
     */
110 1
    public function offsetUnset($offset): void
111
    {
112 1
        $this->checkStringOffset($offset);
113 1
        if ($this->offsetExists($offset)) {
114 1
            $this->params->offsetUnset($offset);
115
        }
116 1
    }
117
118
    /**
119
     * @return mixed[]
120
     */
121 11
    public function toArray(): array
122
    {
123 11
        return (array) $this->params;
124
    }
125
126
    /**
127
     * @param string|mixed $offset
128
     *
129
     * @throws Exception\InvalidArgumentException
130
     */
131 16
    private function checkStringOffset($offset): void
132
    {
133 16
        if (!is_string($offset)) {
134 2
            throw new Exception\InvalidArgumentException(
135 2
                sprintf(
136 2
                    'Report parameters must be a string (type: %s%s)',
137 2
                    gettype($offset),
138 2
                    is_scalar($offset) ? ', value:' . (string) $offset : ''
139
                )
140
            );
141 15
        } elseif (trim($offset) === '') {
142 1
            throw new Exception\InvalidArgumentException(
143 1
                sprintf(
144 1
                    'Report parameters must be a non-empty string.'
145
                )
146
            );
147
        }
148 15
    }
149
}
150