Completed
Push — master ( f70b57...5f6191 )
by Changwan
06:48
created

ErrorBag::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Validator\Throwable;
3
4
use Countable;
5
use Wandu\Validator\Contracts\ErrorThrowable;
6
7
class ErrorBag implements ErrorThrowable, Countable
8
{
9
    /** @var array */
10
    public $errors = [];
11
12
    /**
13
     * @param string $type
14
     * @param array $keys
15
     */
16 6
    public function throws(string $type, array $keys = [])
17
    {
18 6
        array_push($this->errors, [$type, $keys]);
19 6
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 6
    public function count()
25
    {
26 6
        return count($this->errors);
27
    }
28
29
    /**
30
     * @return array
31
     */
32
    public function errors(): array
33
    {
34
        return array_map(function ($error) {
35 6
            $target = array_reduce($error[1] ?? [], function ($carry, $param) {
36 5
                if ($carry === null) return $param;
37 3
                if (is_numeric($param) &&is_int($param + 0)) {
38 3
                    return $carry . '[' . $param . ']';
39
                }
40 3
                return $carry . '.' . $param;
41 6
            });
42
            return $error[0] . ($target ? "@{$target}" : "");
43
        }, $this->errors);
44
    }
45
}
46