ToManyRelationshipTypeCheckerRule::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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