SlumberAnnotation::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 3
cts 3
cp 1
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * File was created 08.10.2015 17:51
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Annotation;
7
8
use PeekAndPoke\Component\Slumber\Core\Exception\SlumberException;
9
use PeekAndPoke\Component\Slumber\Core\Validation\ValidationContext;
10
11
/**
12
 * @author Karsten J. Gerber <[email protected]>
13
 */
14
abstract class SlumberAnnotation implements SlumberMarker
15
{
16
    /**
17
     * Value property. Common among all derived classes.
18
     *
19
     * @var string
20
     */
21
    public $value;
22
23
    /**
24
     * @param array $data Key-value for properties to be defined in this class.
25
     */
26 377
    public function __construct(array $data = [])
27
    {
28 377
        foreach ($data as $key => $value) {
29 166
            $this->$key = $value;
30
        }
31 377
    }
32
33
    /**
34
     * Error handler for unknown property accessor in Annotation class.
35
     *
36
     * @param string $name Unknown property name.
37
     *
38
     * @throws \BadMethodCallException
39
     */
40
    public function __get($name)
41
    {
42
        throw new \BadMethodCallException(
43
            sprintf("Unknown property '%s' on annotation '%s'.", $name, \get_class($this))
44
        );
45
    }
46
47
    /**
48
     * Error handler for unknown property mutator in Annotation class.
49
     *
50
     * @param string $name  Unknown property name.
51
     * @param mixed  $value Property value.
52
     *
53
     * @throws \BadMethodCallException
54
     */
55
    public function __set($name, $value)
56
    {
57
        throw new \BadMethodCallException(
58
            sprintf("Unknown property '%s' on annotation '%s'.", $name, \get_class($this))
59
        );
60
    }
61
62
    /**
63
     * Error handler for unknown property mutator in Annotation class.
64
     *
65
     * @param string $name Unknown property name.
66
     *
67
     * @throws \BadMethodCallException
68
     */
69
    public function __isset($name)
70
    {
71
        throw new \BadMethodCallException(
72
            sprintf("Unknown property '%s' on annotation '%s'.", $name, \get_class($this))
73
        );
74
    }
75
76
    /**
77
     * Initialize the annotation and validate the given parameters
78
     *
79
     * @param ValidationContext $context
80
     */
81 36
    public function validate(ValidationContext $context)
82
    {
83
        // noop
84 36
    }
85
86
    /**
87
     * @param ValidationContext $context
88
     * @param string            $msg
89
     *
90
     * @return string
91
     */
92
    protected function buildValidationMessage(ValidationContext $context, $msg)
93
    {
94
        return 'For ' . $context->getAnnotationLocation() . ' the validation of @' . static::class .
95
               ' failed, due to: ' . $msg . ' ';
96
    }
97
98
    /**
99
     * @param ValidationContext $context
100
     * @param string            $msg
101
     *
102
     * @return SlumberException
103
     */
104
    protected function createValidationException(ValidationContext $context, $msg)
105
    {
106
        return new SlumberException($this->buildValidationMessage($context, $msg));
107
    }
108
}
109