Completed
Push — master ( ed059a...0f6c49 )
by Karsten
02:43
created

AsEnum::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 9.2
ccs 0
cts 13
cp 0
cc 4
eloc 12
nc 4
nop 1
crap 20
1
<?php
2
/**
3
 * File was created 30.09.2015 07:57
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Annotation\Slumber;
7
8
use Doctrine\Common\Annotations\Annotation;
9
use PeekAndPoke\Component\Slumber\Core\Exception\SlumberException;
10
use PeekAndPoke\Component\Slumber\Core\Validation\ValidationContext;
11
use PeekAndPoke\Types\Enumerated;
12
13
/**
14
 * @Annotation
15
 * @Annotation\Target("PROPERTY")
16
 *
17
 * @author Karsten J. Gerber <[email protected]>
18
 */
19
class AsEnum extends PropertyMappingMarkerBase
20
{
21
    /**
22
     * @param ValidationContext  $context
23
     *
24
     * @throws SlumberException
25
     */
26
    public function validate(ValidationContext $context)
27
    {
28
        if (empty($this->value)) {
29
            throw $this->createValidationException(
30
                $context,
31
                'you must provide a class as value. Example @Slumber\AsObject( MyEnum::class )'
32
            );
33
        }
34
35
        if (!class_exists($this->value)) {
36
            throw $this->createValidationException(
37
                $context,
38
                "you provided the non-existing class '$this->value'"
39
            );
40
        }
41
42
        if (!is_a($this->value, Enumerated::class, true)) {
43
            throw $this->createValidationException(
44
                $context,
45
                "you provided the class '$this->value' which does not extend " . Enumerated::class
46
            );
47
        }
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function keepNullValuesInCollections()
54
    {
55
        return false;
56
    }
57
}
58