AbstractConstraint::initName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Validation\Constraints;
4
5
use Nip\Utility\Traits\NameWorksTrait;
6
7
/**
8
 * Class AbstractConstraint.
9
 *
10
 * @property $errorNames
11
 */
12
abstract class AbstractConstraint implements ConstraintInterface
13
{
14 1
    use NameWorksTrait;
15 1
    use Traits\InitTrait;
16
17
    /**
18
     * @var string
19
     */
20
    protected $name = null;
21
22
    /**
23
     * @return string
24
     */
25 4
    public function getName(): string
26
    {
27 4
        if ($this->name === null) {
28
            $this->initName();
29
        }
30
31 4
        return $this->name;
32
    }
33
34
    /**
35
     * @param string $name
36
     */
37 4
    public function setName(string $name)
38
    {
39 4
        $this->name = $name;
40 4
    }
41
42
    protected function initName()
43
    {
44
        $this->name = $this->generateName();
45
    }
46
47
    /**
48
     * @return string
49
     */
50 4
    protected function generateName()
51
    {
52 4
        return strtolower(str_replace('Constraint', '', $this->getClassFirstName()));
53
    }
54
55
56
    /**
57
     * @param $variables
58
     */
59 1
    protected function applyVariables($variables)
60
    {
61 1
        foreach ($variables as $name => $value) {
62
            $this->{$name} = $value;
63
        }
64 1
    }
65
66
    /**
67
     * @param $code
68
     */
69 1
    public function getErrorMessage($code)
70
    {
71 1
        if (isset(static::$errorNames[$code])) {
72 1
            return static::$errorNames[$code];
73
        }
74 1
    }
75
}
76