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\Common\Reflection\ClassIsTrait; |
22
|
|
|
use Limoncello\Flute\Contracts\Api\CrudInterface; |
23
|
|
|
use Limoncello\Flute\Contracts\FactoryInterface; |
24
|
|
|
use Limoncello\Flute\Contracts\Validation\ErrorCodes; |
25
|
|
|
use Limoncello\Flute\L10n\Messages; |
26
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
27
|
|
|
use Limoncello\Validation\Rules\ExecuteRule; |
28
|
|
|
use Psr\Container\ContainerExceptionInterface; |
29
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
30
|
|
|
use function assert; |
31
|
|
|
use function count; |
32
|
|
|
use function is_array; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @package Limoncello\Flute |
36
|
|
|
*/ |
37
|
|
|
final class AreReadableViaApiRule extends ExecuteRule |
38
|
|
|
{ |
39
|
|
|
use ClassIsTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Property key. |
43
|
33 |
|
*/ |
44
|
|
|
const PROPERTY_API_CLASS = self::PROPERTY_LAST + 1; |
45
|
33 |
|
|
46
|
33 |
|
/** |
47
|
33 |
|
* @param string $apiClass |
48
|
|
|
*/ |
49
|
|
|
public function __construct(string $apiClass) |
50
|
33 |
|
{ |
51
|
33 |
|
assert(static::classImplements($apiClass, CrudInterface::class)); |
52
|
|
|
|
53
|
|
|
parent::__construct([ |
54
|
|
|
static::PROPERTY_API_CLASS => $apiClass, |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param mixed $values |
60
|
|
|
* @param ContextInterface $context |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
* |
64
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
65
|
|
|
* |
66
|
1 |
|
* @throws ContainerExceptionInterface |
67
|
|
|
* @throws NotFoundExceptionInterface |
68
|
1 |
|
*/ |
69
|
|
|
public static function execute($values, ContextInterface $context): array |
70
|
|
|
{ |
71
|
1 |
|
assert(is_array($values)); |
72
|
|
|
|
73
|
1 |
|
// let's consider an empty index list as `readable` |
74
|
1 |
|
$result = true; |
75
|
|
|
|
76
|
|
|
if (empty($values) === false) { |
77
|
1 |
|
$apiClass = $context->getProperties()->getProperty(static::PROPERTY_API_CLASS); |
78
|
|
|
|
79
|
|
|
/** @var FactoryInterface $apiFactory */ |
80
|
1 |
|
$apiFactory = $context->getContainer()->get(FactoryInterface::class); |
81
|
|
|
|
82
|
1 |
|
/** @var CrudInterface $api */ |
83
|
|
|
$api = $apiFactory->createApi($apiClass); |
84
|
1 |
|
|
85
|
|
|
$readIndexes = $api->withIndexesFilter($values)->indexIdentities(); |
86
|
|
|
|
87
|
1 |
|
$result = count($readIndexes) === count($values); |
88
|
1 |
|
} |
89
|
1 |
|
|
90
|
1 |
|
return $result === true ? |
91
|
1 |
|
static::createSuccessReply($values) : |
92
|
1 |
|
static::createErrorReply( |
93
|
1 |
|
$context, |
94
|
1 |
|
$values, |
95
|
|
|
ErrorCodes::EXIST_IN_DATABASE_MULTIPLE, |
96
|
|
|
Messages::EXIST_IN_DATABASE_MULTIPLE, |
97
|
|
|
[] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|