Passed
Branch master (0f6c49)
by Karsten
01:41
created

AsEnum   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 37
c 0
b 0
f 0
rs 10
ccs 0
cts 16
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 20 4
A keepNullValuesInCollections() 0 3 1
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