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

AsObject::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8.7414

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.2
ccs 3
cts 9
cp 0.3333
cc 4
eloc 8
nc 3
nop 1
crap 8.7414
1
<?php
2
/**
3
 * File was created 30.09.2015 07:52
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
12
/**
13
 * @Annotation
14
 * @Annotation\Target("PROPERTY")
15
 *
16
 * @author Karsten J. Gerber <[email protected]>
17
 */
18
class AsObject extends PropertyMappingMarkerBase
19
{
20
    /**
21
     * @param ValidationContext $context
22
     *
23
     * @throws SlumberException
24
     */
25 6
    public function validate(ValidationContext $context)
26
    {
27 6
        if (empty($this->value)) {
28
            throw $this->createValidationException(
29
                $context,
30
                'you must provide a class as value. Example @Slumber\AsObject( SomeClass::class )'
31
            );
32
        }
33
34
        // NOTICE: we also accept interfaces here, as the interface can define a Slumber\Polymorphic
35 6
        if (! class_exists($this->value) && ! interface_exists($this->value)) {
36
            throw $this->createValidationException(
37
                $context,
38
                "you provided the non-existing class '$this->value'"
39
            );
40
        }
41 6
    }
42
43
    /**
44
     * @return bool
45
     */
46 3
    public function keepNullValuesInCollections()
47
    {
48 3
        return false;
49
    }
50
}
51