Completed
Push — master ( 8155ff...9fff48 )
by Changwan
03:00
created

ErrorBag::popPrefix()   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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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
    public $errors = [];
12
13
    /**
14
     * @param string $prefix
15
     */
16 3
    public function pushPrefix(string $prefix)
17
    {
18 3
        array_push($this->prefixes, $prefix);
19 3
    }
20
    
21 3
    public function popPrefix()
22
    {
23 3
        array_pop($this->prefixes);
24 3
    }
25
26
    /**
27
     * @param string $type
28
     * @param array $target
29
     */
30
    public function throw(string $type, array $target = [])
31
    {
32 2
        array_push($this->errors, [
33 2
            $type,
34 2
            array_merge($this->prefixes, $target)
35
        ]);
36 2
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function count()
42
    {
43 3
        return count($this->errors);
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function errors(): array
50
    {
51 2
        return array_map(function ($error) {
52 2
            $target = "";
53 2
            if (count($error[1])) {
54 2
                $target = implode(".", $error[1]);
55
            }
56 2
            return $error[0] . ($target ? "@{$target}" : "");
57 2
        }, $this->errors);
58
    }
59
}
60