1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\ObjectAgent; |
6
|
|
|
|
7
|
|
|
final class Capabilities |
8
|
|
|
{ |
9
|
|
|
private $supportedComparators; |
10
|
|
|
private $setParent; |
11
|
|
|
private $queryCount; |
12
|
|
|
private $queryJoin; |
13
|
|
|
private $querySelect; |
14
|
|
|
|
15
|
|
|
private function __construct() |
16
|
|
|
{ |
17
|
|
|
// this class cannot be instantiated with "new". |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function create(array $capabilities): Capabilities |
21
|
|
|
{ |
22
|
|
|
$defaults = [ |
23
|
|
|
'supported_comparators' => [], |
24
|
|
|
'can_set_parent' => false, |
25
|
|
|
'can_query_count' => false, |
26
|
|
|
'can_query_join' => false, |
27
|
|
|
'can_query_select' => false, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
View Code Duplication |
if ($diff = array_diff(array_keys($capabilities), array_keys($defaults))) { |
|
|
|
|
31
|
|
|
throw new \InvalidArgumentException(sprintf( |
32
|
|
|
'Unknown capabilities: "%s". Valid capabilities: "%s"', |
33
|
|
|
implode('", "', $diff), implode('", "', array_keys($defaults)) |
34
|
|
|
)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$capabilities = array_merge($defaults, $capabilities); |
38
|
|
|
|
39
|
|
|
$instance = new self(); |
40
|
|
|
$instance->supportedComparators = (array) $capabilities['supported_comparators']; |
41
|
|
|
$instance->setParent = (bool) $capabilities['can_set_parent']; |
42
|
|
|
$instance->queryJoin = (bool) $capabilities['can_query_join']; |
43
|
|
|
$instance->queryCount = (bool) $capabilities['can_query_count']; |
44
|
|
|
$instance->querySelect = (bool) $capabilities['can_query_select']; |
45
|
|
|
|
46
|
|
|
return $instance; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getSupportedComparators(): array |
50
|
|
|
{ |
51
|
|
|
return $this->supportedComparators; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function canSetParent(): bool |
55
|
|
|
{ |
56
|
|
|
return $this->setParent; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function canQueryCount(): bool |
60
|
|
|
{ |
61
|
|
|
return $this->queryCount; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function canQueryJoin(): bool |
65
|
|
|
{ |
66
|
|
|
return $this->queryJoin; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function canQuerySelect(): bool |
70
|
|
|
{ |
71
|
|
|
return $this->querySelect; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.