Completed
Push — master ( 7e748b...ca1854 )
by Karsten
02:03
created

AsCollection::validateCollectionParam()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 6
cts 12
cp 0.5
rs 9.0534
cc 4
eloc 11
nc 4
nop 1
crap 6
1
<?php
2
/**
3
 * File was created 30.09.2015 10:49
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Annotation\Slumber;
7
8
use PeekAndPoke\Component\Collections\Collection;
9
use PeekAndPoke\Component\Slumber\Annotation\PropertyMappingMarker;
10
use PeekAndPoke\Component\Slumber\Core\Exception\SlumberException;
11
use PeekAndPoke\Component\Slumber\Core\Validation\ValidationContext;
12
13
/**
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
abstract class AsCollection extends PropertyMappingMarkerBase
17
{
18
    /**
19
     * @var string The class of the collection to use
20
     */
21
    public $collection;
22
23
    /**
24
     * @return string
25
     */
26 2
    public function getCollection()
27
    {
28 2
        return $this->collection;
29
    }
30
31
    /**
32
     * @param string $collection
33
     *
34
     * @return $this
35
     */
36 2
    public function setCollection($collection)
37
    {
38 2
        $this->collection = $collection;
39
40 2
        return $this;
41
    }
42
43
    /**
44
     * @param ValidationContext $context
45
     *
46
     * @throws SlumberException
47
     */
48 8
    public function validate(ValidationContext $context)
49
    {
50 8
        $this->validateNestedMapperParam($context);
51
52 8
        $this->validateCollectionParam($context);
53 8
    }
54
55 8
    public function validateNestedMapperParam(ValidationContext $context)
56
    {
57 8
        if ($this->value instanceof PropertyMappingMarker) {
58
59 8
            $this->value->validate($context);
60
61
        } else {
62
            throw $this->createValidationException(
63
                $context,
64
                'You must provide a PropertyMappingMarker as value. ' .
65
                'Example: @Slumber\AsList( @Slumber\AsObject( SomeClass::class ) ) or ' .
66
                '@Slumber\AsList( @Slumber\AsString() )'
67
            );
68
        }
69 8
    }
70
71 8
    public function validateCollectionParam(ValidationContext $context)
72
    {
73 8
        if (! empty ($this->collection)) {
74
75 4
            if (! class_exists($this->collection)) {
76
                throw $this->createValidationException(
77
                    $context,
78
                    "The collection class '{$this->collection}' does not exist'"
79
                );
80
            }
81
82 4
            $expectedType = Collection::class;
83
84 4
            if (! is_a($this->collection, $expectedType, true)) {
85
                throw $this->createValidationException(
86
                    $context,
87
                    "The collection class '{$this->collection}' must be instance of '{$expectedType}'"
88
                );
89
            }
90
        }
91 8
    }
92
}
93