Completed
Pull Request — master (#212)
by Alexandru
43:58 queued 01:14
created

ExpressionAttributeNames   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 21
dl 0
loc 69
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 8 2
A placeholders() 0 3 1
A all() 0 3 1
A isNested() 0 3 3
A placeholder() 0 8 2
A get() 0 3 1
A reset() 0 6 1
1
<?php
2
3
namespace Rennokki\DynamoDb\Parsers;
4
5
class ExpressionAttributeNames
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $mapping;
11
12
    /**
13
     * @var array
14
     */
15
    protected $nested;
16
17
    /**
18
     * @var string
19
     */
20
    protected $prefix;
21
22 14
    public function __construct($prefix = '#')
23
    {
24 14
        $this->reset();
25 14
        $this->prefix = $prefix;
26 14
    }
27
28 6
    public function set($name)
29
    {
30 6
        if ($this->isNested($name)) {
31 2
            $this->nested[] = $name;
32
33 2
            return;
34
        }
35 5
        $this->mapping["{$this->prefix}{$name}"] = $name;
36 5
    }
37
38 1
    public function get($placeholder)
39
    {
40 1
        return $this->mapping[$placeholder];
41
    }
42
43 5
    public function placeholder($name)
44
    {
45 5
        $placeholder = "{$this->prefix}{$name}";
46 5
        if (isset($this->mapping[$placeholder])) {
47 4
            return $placeholder;
48
        }
49
50 1
        return $name;
51
    }
52
53 7
    public function all()
54
    {
55 7
        return $this->mapping;
56
    }
57
58 1
    public function placeholders()
59
    {
60 1
        return array_merge(array_keys($this->mapping), $this->nested);
61
    }
62
63 14
    public function reset()
64
    {
65 14
        $this->mapping = [];
66 14
        $this->nested = [];
67
68 14
        return $this;
69
    }
70
71 6
    private function isNested($name)
72
    {
73 6
        return strpos($name, '.') !== false || (strpos($name, '[') !== false && strpos($name, ']') !== false);
74
    }
75
}
76