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

ExpressionAttributeNames::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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