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 Doctrine\DBAL\Connection; |
22
|
|
|
use Limoncello\Flute\Contracts\Validation\ErrorCodes; |
23
|
|
|
use Limoncello\Flute\L10n\Messages; |
24
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
25
|
|
|
use Limoncello\Validation\Rules\ExecuteRule; |
26
|
|
|
use Psr\Container\ContainerExceptionInterface; |
27
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Limoncello\Flute |
31
|
|
|
*/ |
32
|
|
|
final class ExistInDbTableMultipleWithDoctrineRule extends ExecuteRule |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Property key. |
36
|
|
|
*/ |
37
|
|
|
const PROPERTY_TABLE_NAME = self::PROPERTY_LAST + 1; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Property key. |
41
|
|
|
*/ |
42
|
|
|
const PROPERTY_PRIMARY_NAME = self::PROPERTY_TABLE_NAME + 1; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $tableName |
46
|
|
|
* @param string $primaryName |
47
|
|
|
*/ |
48
|
30 |
|
public function __construct(string $tableName, string $primaryName) |
49
|
|
|
{ |
50
|
30 |
|
parent::__construct([ |
51
|
30 |
|
static::PROPERTY_TABLE_NAME => $tableName, |
52
|
30 |
|
static::PROPERTY_PRIMARY_NAME => $primaryName, |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param mixed $values |
58
|
|
|
* @param ContextInterface $context |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
* |
62
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
63
|
|
|
* |
64
|
|
|
* @throws ContainerExceptionInterface |
65
|
|
|
* @throws NotFoundExceptionInterface |
66
|
|
|
*/ |
67
|
4 |
|
public static function execute($values, ContextInterface $context): array |
68
|
|
|
{ |
69
|
|
|
// let's consider an empty index list as `exists` |
70
|
4 |
|
$result = is_array($values); |
71
|
|
|
|
72
|
4 |
|
if ($result === true && empty($values) === false) { |
73
|
4 |
|
$tableName = $context->getProperties()->getProperty(static::PROPERTY_TABLE_NAME); |
74
|
4 |
|
$primaryName = $context->getProperties()->getProperty(static::PROPERTY_PRIMARY_NAME); |
75
|
|
|
|
76
|
|
|
/** @var Connection $connection */ |
77
|
4 |
|
$connection = $context->getContainer()->get(Connection::class); |
78
|
4 |
|
$builder = $connection->createQueryBuilder(); |
79
|
4 |
|
$placeholders = []; |
80
|
4 |
|
foreach ($values as $value) { |
81
|
4 |
|
$placeholders[] = $builder->createPositionalParameter($value); |
82
|
|
|
} |
83
|
|
|
$statement = $builder |
84
|
4 |
|
->select('count(*)') |
85
|
4 |
|
->from($tableName) |
86
|
4 |
|
->where($builder->expr()->in($primaryName, $placeholders)) |
87
|
4 |
|
->execute(); |
88
|
|
|
|
89
|
4 |
|
$count = (int)$statement->fetchColumn(); |
90
|
4 |
|
$result = $count === count($values); |
91
|
|
|
} |
92
|
|
|
|
93
|
4 |
|
$reply = $result === true ? |
94
|
4 |
|
static::createSuccessReply($values) : |
95
|
|
|
static::createErrorReply( |
96
|
|
|
$context, |
97
|
|
|
$values, |
98
|
|
|
ErrorCodes::EXIST_IN_DATABASE_MULTIPLE, |
99
|
|
|
Messages::EXIST_IN_DATABASE_MULTIPLE, |
100
|
4 |
|
[] |
|
|
|
|
101
|
|
|
); |
102
|
|
|
|
103
|
4 |
|
return $reply; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.