NamedParameterMap   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNamedParameters() 0 4 1
A offsetSet() 0 19 3
A filterNamedParameters() 0 17 3
1
<?php
2
/**
3
 * This file is part of the ramsey/collection library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://benramsey.com/projects/ramsey-collection/ Documentation
11
 * @link https://packagist.org/packages/ramsey/collection Packagist
12
 * @link https://github.com/ramsey/collection GitHub
13
 */
14
15
namespace Ramsey\Collection\Map;
16
17
use Ramsey\Collection\Tool\TypeTrait;
18
use Ramsey\Collection\Tool\ValueToStringTrait;
19
20
/**
21
 * NamedParameterMap represents a mapping of values to a set of named keys
22
 * that may optionally be typed
23
 */
24
class NamedParameterMap extends AbstractMap
25
{
26
    use TypeTrait;
27
    use ValueToStringTrait;
28
29
    /**
30
     * @var array
31
     */
32
    protected $namedParameters;
33
34
    /**
35
     * Constructs a new NamedParameterMap object
36
     *
37
     * @param array $namedParameters The named parameters supported
38
     * @param array $data
39
     */
40
    public function __construct(array $namedParameters, array $data = [])
41
    {
42
        $this->namedParameters = $this->filterNamedParameters($namedParameters);
43
        parent::__construct($data);
44
    }
45
46
    /**
47
     * Returns named parameters set for this NamedParameterMap
48
     *
49
     * @return array
50
     */
51
    public function getNamedParameters()
52
    {
53
        return $this->namedParameters;
54
    }
55
56
    public function offsetSet($offset, $value)
57
    {
58
        if (!array_key_exists($offset, $this->namedParameters)) {
59
            throw new \InvalidArgumentException(
60
                'Attempting to set value for unconfigured parameter \''
61
                . $offset . '\''
62
            );
63
        }
64
65
        if ($this->checkType($this->namedParameters[$offset], $value) === false) {
66
            throw new \InvalidArgumentException(
67
                'Value for \'' . $offset . '\' must be of type '
68
                . $this->namedParameters[$offset] . '; value is '
69
                . $this->toolValueToString($value)
70
            );
71
        }
72
73
        $this->data[$offset] = $value;
74
    }
75
76
    /**
77
     * Given an array of named parameters, constructs a proper mapping of
78
     * named parameters to types
79
     *
80
     * @param array $namedParameters
81
     * @return array
82
     */
83
    protected function filterNamedParameters(array $namedParameters)
84
    {
85
        $names = [];
86
        $types = [];
87
88
        foreach ($namedParameters as $key => $value) {
89
            if (is_int($key)) {
90
                $names[] = (string) $value;
91
                $types[] = 'mixed';
92
            } else {
93
                $names[] = (string) $key;
94
                $types[] = (string) $value;
95
            }
96
        }
97
98
        return array_combine($names, $types);
99
    }
100
}
101