Completed
Push — master ( 0ca994...bcdd07 )
by Neomerx
05:23
created

ToOneRelationshipTypeChecker::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Limoncello\Flute\Validation\Rules;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Flute\Contracts\Validation\ContextInterface;
20
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
21
use Limoncello\Validation\Blocks\ProcedureBlock;
22
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
23
use Limoncello\Validation\Execution\BlockReplies;
24
use Limoncello\Validation\Rules\BaseRule;
25
26
/**
27
 * @package Limoncello\Flute
28
 */
29
final class ToOneRelationshipTypeChecker extends BaseRule
30
{
31
    /**
32
     * Property key.
33
     */
34
    const PROPERTY_RESOURCE_TYPE = self::PROPERTY_LAST + 1;
35
36
    /**
37
     * @var string
38
     */
39
    private $type;
40
41
    /**
42
     * @param string $type
43
     */
44
    public function __construct(string $type)
45
    {
46
        $this->type = $type;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function toBlock(): ExecutionBlockInterface
53
    {
54
        return (new ProcedureBlock([self::class, 'execute']))
55
            ->setProperties($this->getStandardProperties() + [self::PROPERTY_RESOURCE_TYPE => $this->getType()]);
56
    }
57
58
    /**
59
     * @param mixed            $value
60
     * @param ContextInterface $context
61
     *
62
     * @return array
63
     *
64
     * @SuppressWarnings(PHPMD.StaticAccess)
65
     */
66
    public static function execute($value, ContextInterface $context): array
67
    {
68
        // parser guarantees that input will be either null or an [$type => $id] where type and id are scalars
69
70
        if ($value === null) {
71
            return BlockReplies::createSuccessReply($value);
72
        }
73
74
        assert(is_array($value) === true && count($value) === 1);
75
        $index = reset($value);
76
        $type  = key($value);
77
        assert(is_scalar($index) === true && is_scalar($type) === true);
78
        $expectedType = $context->getProperties()->getProperty(self::PROPERTY_RESOURCE_TYPE);
79
        $reply = $type === $expectedType ?
80
            BlockReplies::createSuccessReply($index) :
81
            BlockReplies::createErrorReply($context, $type, ErrorCodes::INVALID_RELATIONSHIP_TYPE);
82
83
        return $reply;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getType(): string
90
    {
91
        return $this->type;
92
    }
93
}
94