1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\DataPersister; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Chained data persisters. |
18
|
|
|
* |
19
|
|
|
* @author Baptiste Meyer <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class ChainDataPersister implements ContextAwareDataPersisterInterface |
22
|
|
|
{ |
23
|
|
|
/** @internal */ |
24
|
|
|
public $persisters; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param DataPersisterInterface[] $persisters |
28
|
|
|
*/ |
29
|
|
|
public function __construct(/* iterable */ $persisters) |
30
|
|
|
{ |
31
|
|
|
$this->persisters = $persisters; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function supports($data, array $context = []): bool |
38
|
|
|
{ |
39
|
|
|
foreach ($this->persisters as $persister) { |
40
|
|
|
if ($persister->supports($data, $context)) { |
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function persist($data, array $context = []) |
52
|
|
|
{ |
53
|
|
|
foreach ($this->persisters as $persister) { |
54
|
|
|
if ($persister->supports($data, $context)) { |
|
|
|
|
55
|
|
|
return $persister->persist($data, $context) ?? $data; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function remove($data, array $context = []) |
64
|
|
|
{ |
65
|
|
|
foreach ($this->persisters as $persister) { |
66
|
|
|
if ($persister->supports($data, $context)) { |
|
|
|
|
67
|
|
|
$persister->remove($data, $context); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function removeElementFromCollection($data, array $context = []) |
78
|
|
|
{ |
79
|
|
|
foreach ($this->persisters as $persister) { |
80
|
|
|
if ($persister->supports($data, $context)) { |
|
|
|
|
81
|
|
|
$persister->removeElementFromCollection($data, $context); |
|
|
|
|
82
|
|
|
|
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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. Please note the @ignore annotation hint above.