BaseMetaData::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace SeoHelper\MetaData;
4
5
use InvalidArgumentException;
6
7
class BaseMetaData
8
{
9
    private $data = [];
10
11
    private $aliases = [];
12
13 27
    public function __call($name, $arguments)
14
    {
15 27
        if (substr($name, 0, 3) === 'get') {
16 15
            $propertyName = $this->createPropertyName(str_replace('get', '', $name));
17 15
            return $this->get($propertyName);
18 27 View Code Duplication
        } elseif (substr($name, 0, 3) == 'set') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19 15
            $propertyName = $this->createPropertyName(str_replace('set', '', $name));
20 15
            return $this->set($propertyName, $arguments[0]);
21 24
        } elseif (substr($name, 0, 3) == 'add') {
22 18
            $propertyName = $this->createPropertyName(str_replace('add', '', $name));
23 18
            return $this->add($propertyName, $arguments[0]);
24 6 View Code Duplication
        } elseif (substr($name, 0, 5) == 'reset') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25 3
            $propertyName = $this->createPropertyName(str_replace('reset', '', $name));
26 3
            return $this->reset($propertyName, isset($arguments[0]) ? $arguments[0] : null);
27
        }
28 3
        throw new InvalidArgumentException("Method '$name' not found");
29
    }
30
31
    /**
32
     * @param string $key
33
     * @param string|array $value
34
     * @return BaseMetaData
35
     */
36 18
    final public function set($key, $value)
37
    {
38 18
        if (!is_array($value)) {
39 18
            $value = [$value];
40 6
        }
41 18
        foreach ($this->findAliases($key) as $alias) {
42 18
            $this->data[$alias] = $value;
43 6
        }
44 18
        return $this;
45
    }
46
47
    /**
48
     * @param string $key
49
     * @param string|array|null $newValue
50
     * @return BaseMetaData
51
     */
52 3
    final public function reset($key, $newValue = null)
53
    {
54 3
        if ($newValue !== null) {
55 3
            return $this->set($key, $newValue);
56
        }
57
58 3
        foreach ($this->findAliases($key) as $alias) {
59 3
            unset($this->data[$alias]);
60 1
        }
61 3
        return $this;
62
    }
63
64
    /**
65
     * @param string $key
66
     * @param string|array $value
67
     * @return BaseMetaData
68
     */
69 18
    final public function add($key, $value)
70
    {
71 18
        if (!is_array($value)) {
72 18
            $value = [$value];
73 6
        }
74 18
        foreach ($this->findAliases($key) as $alias) {
75 18
            if (!isset($this->data[$alias])) {
76 18
                $this->data[$alias] = [];
77 6
            }
78 18
            $this->data[$alias] = array_merge($this->data[$alias], $value);
79 6
        }
80 18
        return $this;
81
    }
82
83
    /**
84
     * @param string $key
85
     * @return array|false all data if $key is null or data for $key as array or false if $key is not set in data
86
     */
87 27
    final public function get($key = null)
88
    {
89 27
        if ($key === null) {
90 21
            return $this->data;
91
        }
92 18
        return isset($this->data[$key]) ? $this->data[$key] : false;
93
    }
94
95
    /**
96
     * alias(es) for type
97
     * for example title => [og:title, twitter:title], in this case there is no need to set all types, because they will be set automatically with main type
98
     * @param string $type
99
     * @param string|array $alias
100
     */
101 3
    final public function alias($type, $alias)
102
    {
103 3
        if (!isset($this->aliases[$type])) {
104 3
            $this->aliases[$type] = [];
105 1
        }
106 3
        if (!is_array($alias)) {
107 3
            $this->aliases[$type][] = $alias;
108 3
            return $this;
109
        }
110
111 3
        $this->aliases[$type] = array_merge($this->aliases[$type], $alias);
112 3
        return $this;
113
    }
114
115 24
    private function createPropertyName($camelCase)
116
    {
117 24
        $length = strlen($camelCase);
118 24
        $lowerCamelCase = lcfirst($camelCase);
119 24
        $propertyName = '';
120 24
        for ($i = 0; $i < $length; $i++) {
121 24
            if ($lowerCamelCase[$i] == strtoupper($lowerCamelCase[$i])) {
122 15
                $propertyName .= ':';
123 5
            }
124 24
            $propertyName .= strtolower($lowerCamelCase[$i]);
125 8
        }
126 24
        return $propertyName;
127
    }
128
129 27
    private function findAliases($key)
130
    {
131 27
        $aliases = isset($this->aliases[$key]) ? $this->aliases[$key] : [];
132 27
        return array_merge([$key], $aliases);
133
    }
134
}
135