Completed
Pull Request — master (#212)
by Alexandru
45:00 queued 03:00
created

ExpressionAttributeNames::placeholder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 2
            return;
33
        }
34 5
        $this->mapping["{$this->prefix}{$name}"] = $name;
35 5
    }
36
37 1
    public function get($placeholder)
38
    {
39 1
        return $this->mapping[$placeholder];
40
    }
41
42 5
    public function placeholder($name)
43
    {
44 5
        $placeholder = "{$this->prefix}{$name}";
45 5
        if (isset($this->mapping[$placeholder])) {
46 4
            return $placeholder;
47
        }
48 1
        return $name;
49
    }
50
51 7
    public function all()
52
    {
53 7
        return $this->mapping;
54
    }
55
56 1
    public function placeholders()
57
    {
58 1
        return array_merge(array_keys($this->mapping), $this->nested);
59
    }
60
61 14
    public function reset()
62
    {
63 14
        $this->mapping = [];
64 14
        $this->nested = [];
65
66 14
        return $this;
67
    }
68
69 6
    private function isNested($name)
70
    {
71 6
        return strpos($name, '.') !== false || (strpos($name, '[') !== false && strpos($name, ']') !== false);
72
    }
73
}
74