|
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 is_int; |
|
32
|
|
|
use function is_string; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @package Limoncello\Flute |
|
36
|
|
|
*/ |
|
37
|
|
|
final class IsReadableViaApiRule 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 $value |
|
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($value, ContextInterface $context): array |
|
70
|
1 |
|
{ |
|
71
|
|
|
assert(is_int($value) || is_string($value)); |
|
72
|
|
|
|
|
73
|
1 |
|
$apiClass = $context->getProperties()->getProperty(static::PROPERTY_API_CLASS); |
|
74
|
|
|
|
|
75
|
|
|
/** @var FactoryInterface $apiFactory */ |
|
76
|
1 |
|
$apiFactory = $context->getContainer()->get(FactoryInterface::class); |
|
77
|
|
|
|
|
78
|
1 |
|
/** @var CrudInterface $api */ |
|
79
|
1 |
|
$api = $apiFactory->createApi($apiClass); |
|
80
|
|
|
|
|
81
|
1 |
|
$data = $api->withIndexFilter((string)$value)->indexIdentities(); |
|
82
|
1 |
|
$result = !empty($data); |
|
83
|
1 |
|
|
|
84
|
1 |
|
return $result === true ? |
|
85
|
1 |
|
static::createSuccessReply($value) : |
|
86
|
1 |
|
static::createErrorReply( |
|
87
|
1 |
|
$context, |
|
88
|
1 |
|
$value, |
|
89
|
|
|
ErrorCodes::EXIST_IN_DATABASE_SINGLE, |
|
90
|
|
|
Messages::EXIST_IN_DATABASE_SINGLE, |
|
91
|
|
|
[] |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|