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

ErrorBag   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A throws() 0 4 1
A count() 0 4 1
B errors() 0 13 5
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