Completed
Push — master ( 0d5c20...3309bb )
by Changwan
09:19
created

ErrorBag::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Validator;
3
4
use Countable;
5
6
class ErrorBag implements Countable
7
{
8
    /** @var string[] */
9
    private $prefixes = [];
10
11
    /** @var array */
12
    public $errors = [];
13
14
    /**
15
     * @param string $prefix
16 3
     */
17
    public function pushPrefix(string $prefix)
18 3
    {
19 3
        array_push($this->prefixes, $prefix);
20
    }
21 3
    
22
    public function popPrefix()
23 3
    {
24 3
        array_pop($this->prefixes);
25
    }
26
27
    /**
28
     * @param string $type
29
     * @param array $target
30
     */
31
    public function store(string $type, array $target = [])
32 2
    {
33 2
        array_push($this->errors, [
34 2
            $type,
35
            array_merge($this->prefixes, $target)
36 2
        ]);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41 3
     */
42
    public function count()
43 3
    {
44
        return count($this->errors);
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function errors(): array
51 2
    {
52 2
        return array_map(function ($error) {
53 2
            $target = "";
54 2
            if (count($error[1])) {
55
                $target = implode(".", $error[1]);
56 2
            }
57 2
            return $error[0] . ($target ? "@{$target}" : "");
58
        }, $this->errors);
59
    }
60
}
61