Validator::getErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace R\Hive\Concrete\Data;
4
5
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
6
use R\Hive\Concrete\Exceptions\ValidatorRulesNotSuppliedException;
7
use R\Hive\Contracts\Data\MutatorInterface;
8
use R\Hive\Contracts\Data\ValidatorInterface;
9
10
class Validator implements ValidatorInterface
11
{
12
    protected $errors = null;
13
    protected $factory = null;
14
    protected $update = false;
15
16
    public function __construct(ValidationFactory $factory)
17
    {
18
        $this->factory = $factory;
19
    }
20
21
    public function getCreateRules()
22
    {
23
        throw new ValidatorRulesNotSuppliedException($this, 'create');
24
    }
25
26
    public function getErrors()
27
    {
28
        return $this->errors;
29
    }
30
31
    public function getUpdateRules()
32
    {
33
        throw new ValidatorRulesNotSuppliedException($this, 'update');
34
    }
35
36
    public function hasErrors()
37
    {
38
        return $this->errors !== null;
39
    }
40
41
    public function markAsUpdate()
42
    {
43
        $this->update = true;
44
45
        return $this;
46
    }
47
48
    public function validate(MutatorInterface $mutator)
49
    {
50
        $validator = $this->update
51
        ? $this->factory->make($mutator->all(), $this->getUpdateRules())
52
        : $this->factory->make($mutator->all(), $this->getCreateRules());
53
54
        if ($validator->fails()) {
55
            $this->errors = $validator->errors();
0 ignored issues
show
Bug introduced by
The method errors() does not seem to exist on object<Illuminate\Contracts\Validation\Validator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        }
57
58
        // Reset the update flag back to it's default value.
59
        $this->update = false;
60
61
        return $this;
62
    }
63
}
64