|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/old-town/old-town-workflow |
|
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace OldTown\Workflow\Loader; |
|
7
|
|
|
|
|
8
|
|
|
use DOMElement; |
|
9
|
|
|
use DOMDocument; |
|
10
|
|
|
use OldTown\Workflow\Exception\InvalidDescriptorException; |
|
11
|
|
|
use OldTown\Workflow\Exception\InvalidWriteWorkflowException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ConditionDescriptor |
|
15
|
|
|
* |
|
16
|
|
|
* @package OldTown\Workflow\Loader |
|
17
|
|
|
*/ |
|
18
|
|
View Code Duplication |
class ValidatorDescriptor extends AbstractDescriptor |
|
|
|
|
|
|
19
|
|
|
implements Traits\ArgsInterface, |
|
20
|
|
|
Traits\TypeInterface, |
|
21
|
|
|
Traits\NameInterface, |
|
22
|
|
|
Traits\CustomArgInterface, |
|
23
|
|
|
WriteXmlInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use Traits\ArgsTrait, Traits\TypeTrait, Traits\IdTrait, Traits\NameTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param $element |
|
29
|
|
|
*/ |
|
30
|
15 |
|
public function __construct(DOMElement $element = null) |
|
31
|
|
|
{ |
|
32
|
15 |
|
parent::__construct($element); |
|
33
|
|
|
|
|
34
|
15 |
|
if (null !== $element) { |
|
35
|
12 |
|
$this->init($element); |
|
36
|
12 |
|
} |
|
37
|
15 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param DOMElement $element |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
12 |
|
protected function init(DOMElement $element) |
|
45
|
|
|
{ |
|
46
|
12 |
|
$this->parseType($element); |
|
47
|
12 |
|
$this->parseId($element, false); |
|
48
|
12 |
|
$this->parseName($element, false); |
|
49
|
|
|
|
|
50
|
12 |
|
$this->parseArgs($element); |
|
51
|
12 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Создает DOMElement - эквивалентный состоянию дескриптора |
|
56
|
|
|
* |
|
57
|
|
|
* @param DOMDocument $dom |
|
58
|
|
|
* |
|
59
|
|
|
* @return DOMElement |
|
60
|
|
|
* @throws InvalidDescriptorException |
|
61
|
|
|
* @throws \OldTown\Workflow\Exception\InvalidWriteWorkflowException |
|
62
|
|
|
*/ |
|
63
|
7 |
|
public function writeXml(DOMDocument $dom = null) |
|
64
|
|
|
{ |
|
65
|
7 |
|
if (null === $dom) { |
|
66
|
1 |
|
$errMsg = 'Не передан DOMDocument'; |
|
67
|
1 |
|
throw new InvalidWriteWorkflowException($errMsg); |
|
68
|
|
|
} |
|
69
|
6 |
|
$descriptor = $dom->createElement('validator'); |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
6 |
|
if ($this->hasId()) { |
|
73
|
5 |
|
$id = $this->getId(); |
|
74
|
5 |
|
$descriptor->setAttribute('id', $id); |
|
75
|
5 |
|
} |
|
76
|
|
|
|
|
77
|
6 |
|
$name = $this->getName(); |
|
78
|
6 |
|
if (null !== $name) { |
|
79
|
5 |
|
$nameEncode = XmlUtil::encode($name); |
|
80
|
5 |
|
$descriptor->setAttribute('name', $nameEncode); |
|
81
|
5 |
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
$type = $this->getType(); |
|
84
|
6 |
|
if (null === $type) { |
|
85
|
1 |
|
$errMsg = 'Некорректное значение для атрибута type'; |
|
86
|
1 |
|
throw new InvalidDescriptorException($errMsg); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
$descriptor->setAttribute('type', $type); |
|
90
|
|
|
|
|
91
|
5 |
|
$this->writeArgs($descriptor); |
|
92
|
|
|
|
|
93
|
5 |
|
return $descriptor; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $key |
|
99
|
|
|
* @param string $value |
|
100
|
|
|
* |
|
101
|
|
|
* @return boolean |
|
102
|
|
|
*/ |
|
103
|
5 |
|
public function flagUseCustomArgWriter($key, $value) |
|
104
|
|
|
{ |
|
105
|
5 |
|
$flag = 'phpshell' === $this->getType(); |
|
106
|
|
|
|
|
107
|
5 |
|
return $flag; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Генерирует значение аргумента |
|
112
|
|
|
* |
|
113
|
|
|
* @param $key |
|
114
|
|
|
* @param $value |
|
115
|
|
|
* |
|
116
|
|
|
* @param DOMElement $argElement |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
4 |
|
public function buildArgValue($key, $value, DOMElement $argElement) |
|
121
|
|
|
{ |
|
122
|
4 |
|
$dom = $argElement->ownerDocument; |
|
123
|
4 |
|
$argValueElement = $dom->createCDATASection($value); |
|
124
|
4 |
|
$argElement->appendChild($argValueElement); |
|
125
|
4 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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.