1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Rule Component. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Component\Rule\Applicator; |
16
|
|
|
|
17
|
|
|
use SWP\Component\Rule\Model\RuleInterface; |
18
|
|
|
use SWP\Component\Rule\Model\RuleSubjectInterface; |
19
|
|
|
|
20
|
|
|
final class RuleApplicatorChain implements RuleApplicatorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RuleApplicatorInterface[] array |
24
|
|
|
*/ |
25
|
|
|
private $ruleApplicators = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* RuleApplicatorChain constructor. |
29
|
|
|
* |
30
|
|
|
* @param array $ruleApplicators |
31
|
|
|
*/ |
32
|
11 |
|
public function __construct(array $ruleApplicators = []) |
33
|
|
|
{ |
34
|
11 |
|
$this->ruleApplicators = $ruleApplicators; |
35
|
11 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param RuleApplicatorInterface $ruleApplicator |
39
|
|
|
*/ |
40
|
11 |
|
public function addApplicator(RuleApplicatorInterface $ruleApplicator) |
41
|
|
|
{ |
42
|
11 |
|
$this->ruleApplicators[] = $ruleApplicator; |
43
|
11 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2 |
|
public function apply(RuleInterface $rule, RuleSubjectInterface $subject) |
49
|
|
|
{ |
50
|
2 |
|
foreach ($this->ruleApplicators as $ruleApplicator) { |
51
|
2 |
|
if ($ruleApplicator->isSupported($subject)) { |
52
|
2 |
|
$ruleApplicator->apply($rule, $subject); |
53
|
|
|
} |
54
|
|
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function isSupported(RuleSubjectInterface $subject) |
61
|
|
|
{ |
62
|
|
|
foreach ($this->ruleApplicators as $ruleApplicator) { |
63
|
|
|
if ($ruleApplicator->isSupported($subject)) { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|