1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: horat1us |
5
|
|
|
* Date: 5/11/17 |
6
|
|
|
* Time: 6:32 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Horat1us\Services; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Horat1us\Services\Traits\PropertiesDifferenceTrait; |
13
|
|
|
use Horat1us\XmlConvertibleInterface; |
14
|
|
|
use Horat1us\XmlConvertibleObject; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class XmlIntersectionService |
18
|
|
|
* @package Horat1us\Services |
19
|
|
|
*/ |
20
|
|
|
class XmlIntersectionService |
21
|
|
|
{ |
22
|
|
|
use PropertiesDifferenceTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var XmlConvertibleInterface |
26
|
|
|
*/ |
27
|
|
|
protected $source; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var XmlConvertibleInterface |
31
|
|
|
*/ |
32
|
|
|
protected $target; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* XmlIntersectionService constructor. |
36
|
|
|
* @param XmlConvertibleInterface $source |
37
|
|
|
* @param XmlConvertibleInterface $target |
38
|
|
|
*/ |
39
|
4 |
|
public function __construct( |
40
|
|
|
XmlConvertibleInterface $source, |
41
|
|
|
XmlConvertibleInterface $target |
42
|
|
|
) |
43
|
|
|
{ |
44
|
|
|
$this |
45
|
4 |
|
->setSource($source) |
46
|
4 |
|
->setTarget($target); |
47
|
4 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return null|XmlConvertibleInterface |
51
|
|
|
*/ |
52
|
4 |
|
public function intersect() |
53
|
|
|
{ |
54
|
4 |
|
if ($this->getIsCommonDifferent()) { |
55
|
1 |
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
$newChildren = array_uintersect( |
59
|
3 |
|
$this->getTarget()->getXmlChildren() ?? [], |
60
|
3 |
|
$this->getSource()->getXmlChildren() ?? [], |
61
|
3 |
|
[$this, 'compare'] |
62
|
|
|
); |
63
|
|
|
|
64
|
3 |
|
return clone $this->getSource()->setXmlChildren($newChildren); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
public function getIsCommonDifferent() :bool |
68
|
|
|
{ |
69
|
4 |
|
return $this->getTarget()->getXmlElementName() !== $this->getSource()->getXmlElementName() |
70
|
4 |
|
|| $this->getIsDifferentProperties(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $comparedChild |
75
|
|
|
* @param $currentChild |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
2 |
|
public function compare($comparedChild, $currentChild) |
79
|
|
|
{ |
80
|
2 |
|
$source = $currentChild instanceof XmlConvertibleInterface |
81
|
2 |
|
? $currentChild |
82
|
2 |
|
: XmlConvertibleObject::fromXml($currentChild); |
83
|
|
|
|
84
|
2 |
|
$target = $comparedChild instanceof XmlConvertibleInterface |
85
|
2 |
|
? $comparedChild |
86
|
2 |
|
: XmlConvertibleObject::fromXml($comparedChild); |
87
|
|
|
|
88
|
2 |
|
$diff = $source->xmlIntersect($target); |
89
|
|
|
|
90
|
2 |
|
return $diff === null ? -1 : 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return XmlConvertibleInterface |
95
|
|
|
*/ |
96
|
4 |
|
public function getSource(): XmlConvertibleInterface |
97
|
|
|
{ |
98
|
4 |
|
return $this->source; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param XmlConvertibleInterface $source |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
4 |
|
public function setSource(XmlConvertibleInterface $source) |
106
|
|
|
{ |
107
|
4 |
|
$this->source = $source; |
108
|
|
|
|
109
|
4 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return XmlConvertibleInterface |
114
|
|
|
*/ |
115
|
4 |
|
public function getTarget(): XmlConvertibleInterface |
116
|
|
|
{ |
117
|
4 |
|
return $this->target; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param XmlConvertibleInterface $target |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
4 |
|
public function setTarget(XmlConvertibleInterface $target) |
125
|
|
|
{ |
126
|
4 |
|
$this->target = $target; |
127
|
|
|
|
128
|
4 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
} |