|
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
|
|
|
use function is_scalar; |
|
29
|
|
|
use function key; |
|
30
|
|
|
use function reset; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @package Limoncello\Flute |
|
34
|
|
|
*/ |
|
35
|
|
|
final class ToOneRelationshipTypeCheckerRule extends ExecuteRule |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Property key. |
|
39
|
41 |
|
*/ |
|
40
|
|
|
const PROPERTY_RESOURCE_TYPE = self::PROPERTY_LAST + 1; |
|
41
|
41 |
|
|
|
42
|
41 |
|
/** |
|
43
|
|
|
* @param string $type |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(string $type) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct([ |
|
48
|
|
|
static::PROPERTY_RESOURCE_TYPE => $type, |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param mixed $value |
|
54
|
9 |
|
* @param ContextInterface $context |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
* |
|
58
|
9 |
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
59
|
2 |
|
*/ |
|
60
|
|
|
public static function execute($value, ContextInterface $context): array |
|
61
|
|
|
{ |
|
62
|
7 |
|
// parser guarantees that input will be either null or an [$type => $id] where type and id are scalars |
|
63
|
7 |
|
|
|
64
|
7 |
|
if ($value === null) { |
|
65
|
7 |
|
return static::createSuccessReply($value); |
|
66
|
7 |
|
} |
|
67
|
7 |
|
|
|
68
|
6 |
|
assert(is_array($value) === true && count($value) === 1); |
|
69
|
1 |
|
$index = reset($value); |
|
70
|
1 |
|
$type = key($value); |
|
71
|
1 |
|
assert(is_scalar($index) === true && is_scalar($type) === true); |
|
72
|
1 |
|
$expectedType = $context->getProperties()->getProperty(static::PROPERTY_RESOURCE_TYPE); |
|
73
|
1 |
|
$reply = $type === $expectedType ? |
|
74
|
7 |
|
static::createSuccessReply($index) : |
|
75
|
|
|
static::createErrorReply( |
|
76
|
|
|
$context, |
|
77
|
7 |
|
$type, |
|
78
|
|
|
ErrorCodes::INVALID_RELATIONSHIP_TYPE, |
|
79
|
|
|
Messages::INVALID_RELATIONSHIP_TYPE, |
|
80
|
|
|
[] |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
return $reply; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|