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

ToManyRelationshipTypeChecker::toBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 ToManyRelationshipTypeChecker 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
     * @SuppressWarnings(PHPMD.ElseExpression)
66
     */
67
    public static function execute($value, ContextInterface $context): array
68
    {
69
        // parser guarantees that input will be an array of [$type => $id] where type and id are scalars
70
71
        // we will check the type of every pair and send further identities only
72
        $indexes          = [];
73
        $foundInvalidType = null;
74
        $expectedType     = $context->getProperties()->getProperty(self::PROPERTY_RESOURCE_TYPE);
75
        foreach ($value as $typeAndId) {
76
            assert(is_array($typeAndId) === true && count($typeAndId) === 1);
77
            $index = reset($typeAndId);
78
            $type  = key($typeAndId);
79
            assert(is_scalar($index) === true && is_scalar($type) === true);
80
            if ($type === $expectedType) {
81
                $indexes[] = $index;
82
            } else {
83
                $foundInvalidType = $type;
84
                break;
85
            }
86
        }
87
88
        $reply = $foundInvalidType === null ?
89
            BlockReplies::createSuccessReply($indexes) :
90
            BlockReplies::createErrorReply($context, $foundInvalidType, ErrorCodes::INVALID_RELATIONSHIP_TYPE);
91
92
        return $reply;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getType(): string
99
    {
100
        return $this->type;
101
    }
102
}
103