1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Seo\Entity; |
4
|
|
|
|
5
|
|
|
use Bankiru\Seo\ConditionInterface; |
6
|
|
|
use Bankiru\Seo\DestinationInterface; |
7
|
|
|
use Bankiru\Seo\SourceInterface; |
8
|
|
|
use Bankiru\Seo\TargetDefinitionInterface; |
9
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
10
|
|
|
use Doctrine\Common\Collections\Collection; |
11
|
|
|
|
12
|
|
|
class TargetDefinition implements TargetDefinitionInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Collection|ConditionInterface[] |
16
|
|
|
*/ |
17
|
|
|
protected $conditions; |
18
|
|
|
/** @var string */ |
19
|
|
|
private $route; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TargetDefinition constructor. |
23
|
|
|
* |
24
|
|
|
* @param string $route |
25
|
|
|
*/ |
26
|
7 |
|
public function __construct($route) |
27
|
|
|
{ |
28
|
7 |
|
$this->route = $route; |
29
|
7 |
|
$this->conditions = new ArrayCollection(); |
30
|
7 |
|
} |
31
|
|
|
|
32
|
|
|
/** {@inheritdoc} */ |
33
|
7 |
|
public function match(DestinationInterface $destination) |
34
|
|
|
{ |
35
|
7 |
|
if ($destination->getRoute() !== $this->getRoute()) { |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
7 |
|
$conditions = $this->conditions->toArray(); |
40
|
7 |
|
foreach ($destination as $code => $item) { |
41
|
5 |
|
if (!array_key_exists($code, $conditions)) { |
42
|
1 |
|
$conditions[$code] = new PermissiveCondition($code, $this); |
43
|
1 |
|
} |
44
|
7 |
|
} |
45
|
|
|
|
46
|
7 |
|
$result = 0; |
47
|
7 |
|
foreach ($conditions as $code => $condition) { |
48
|
5 |
|
$payload = $destination->resolve($code); |
49
|
5 |
|
$score = $condition->match($payload); |
50
|
5 |
|
if ($score === null) { |
51
|
3 |
|
return null; |
52
|
|
|
} |
53
|
3 |
|
$result += $score; |
54
|
5 |
|
} |
55
|
|
|
|
56
|
4 |
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get conditions indexed by code |
61
|
|
|
* |
62
|
|
|
* @return ConditionInterface[] |
63
|
|
|
*/ |
64
|
|
|
public function getConditions() |
65
|
|
|
{ |
66
|
|
|
return $this->conditions->toArray(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
70
|
|
|
public function setCondition($code, ConditionInterface $condition) |
71
|
|
|
{ |
72
|
|
|
if ($condition instanceof PermissiveCondition) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->conditions->set($code, $condition); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** {@inheritdoc} */ |
80
|
1 |
|
public function getCondition($code) |
81
|
|
|
{ |
82
|
1 |
|
if ($this->conditions->containsKey($code)) { |
83
|
|
|
return $this->conditions->get($code); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return new PermissiveCondition($code, $this); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param SourceInterface[] $sources |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
1 |
|
public function count(array $sources) |
95
|
|
|
{ |
96
|
1 |
|
$count = 1; |
97
|
1 |
|
$uniqueSources = []; |
98
|
1 |
|
foreach ($sources as $code => $source) { |
99
|
1 |
|
$source->withCondition($this->getCondition($code)); |
100
|
1 |
|
$uniqueSources[spl_object_hash($source)] = $source; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
foreach ($uniqueSources as $source) { |
104
|
1 |
|
$count *= $source->count(); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
return $count; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** {@inheritdoc} */ |
111
|
11 |
|
public function getRoute() |
112
|
|
|
{ |
113
|
11 |
|
return $this->route; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|