1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the kaloa/metadata package. |
5
|
|
|
* |
6
|
|
|
* For full copyright and license information, please view the LICENSE file |
7
|
|
|
* that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Kaloa\Metadata; |
11
|
|
|
|
12
|
|
|
use Kaloa\Metadata\TripleTag; |
13
|
|
|
use Kaloa\Util\AbstractSet; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
final class TripleTagSet extends AbstractSet |
19
|
|
|
{ |
20
|
|
|
protected $_managedClass = 'Kaloa\\Metadata\\TripleTag'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @param TripleTagSet |
25
|
|
|
* @param TripleTagSet |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
5 |
|
public static function calculateTransformations( |
29
|
|
|
TripleTagSet $oldSet = null, |
30
|
|
|
TripleTagSet $newSet = null |
31
|
|
|
) { |
32
|
|
|
$transformations = array( |
33
|
5 |
|
'keep' => array(), |
34
|
5 |
|
'add' => array(), |
35
|
5 |
|
'delete' => array() |
36
|
5 |
|
); |
37
|
|
|
|
38
|
5 |
|
if (null === $oldSet && null === $newSet) { |
39
|
1 |
|
return $transformations; |
40
|
|
|
} |
41
|
|
|
|
42
|
5 |
|
if (null === $oldSet) { |
43
|
2 |
|
foreach ($newSet as $newTag) { |
|
|
|
|
44
|
1 |
|
$transformations['add'][] = $newTag; |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
return $transformations; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
if (null === $newSet) { |
51
|
2 |
|
foreach ($oldSet as $oldTag) { |
52
|
1 |
|
$transformations['delete'][] = $oldTag; |
53
|
2 |
|
} |
54
|
|
|
|
55
|
2 |
|
return $transformations; |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
foreach ($newSet as $newTag) { |
59
|
3 |
|
foreach ($oldSet as $oldTag) { |
60
|
3 |
|
if ($newTag->equals($oldTag)) { |
61
|
|
|
// Tags in ($newSet AND $oldSet) |
|
|
|
|
62
|
2 |
|
$transformations['keep'][] = $newTag; |
63
|
2 |
|
continue 2; |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
// Tags in ($newSet AND NOT $oldSet) |
|
|
|
|
68
|
2 |
|
$transformations['add'][] = $newTag; |
69
|
4 |
|
} |
70
|
|
|
|
71
|
4 |
|
foreach ($oldSet as $oldTag) { |
72
|
3 |
|
foreach ($newSet as $newTag) { |
73
|
3 |
|
if ($oldTag->equals($newTag)) { |
74
|
2 |
|
continue 2; |
75
|
|
|
} |
76
|
2 |
|
} |
77
|
|
|
|
78
|
|
|
// Tags in ($oldSet AND NOT $newSet) |
|
|
|
|
79
|
2 |
|
$transformations['delete'][] = $oldTag; |
80
|
4 |
|
} |
81
|
|
|
|
82
|
4 |
|
return $transformations; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Transforms a string of linebreak-separated triple tags into a tag array |
87
|
|
|
* with namespace/predicate/value entries |
88
|
|
|
* |
89
|
|
|
* @param string $s string of triple tags |
90
|
|
|
* @return TripleTagSet array of TripleTag |
91
|
|
|
*/ |
92
|
1 |
|
public static function convertStringToTagSet($s) |
93
|
|
|
{ |
94
|
1 |
|
$tags = new TripleTagSet(); |
95
|
|
|
|
96
|
1 |
|
$s = str_replace(array("\r\n", "\r"), array("\n", "\n"), $s); |
97
|
|
|
|
98
|
1 |
|
$tmp = explode("\n", $s); |
99
|
1 |
|
foreach ($tmp as $t) { |
100
|
1 |
|
$newTag = new TripleTag(); |
101
|
|
|
|
102
|
1 |
|
$t = trim($t); |
103
|
1 |
|
if ('' !== $t) { |
104
|
1 |
View Code Duplication |
if (false !== strpos($t, ':')) { |
|
|
|
|
105
|
1 |
|
$parts = explode(':', $t, 2); |
106
|
1 |
|
$newTag->setNamespace($parts[0]); |
107
|
1 |
|
$t = $parts[1]; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
1 |
View Code Duplication |
if (false !== strpos($t, '=')) { |
|
|
|
|
111
|
1 |
|
$parts = explode('=', $t, 2); |
112
|
1 |
|
$newTag->setPredicate($parts[0]); |
113
|
1 |
|
$t = $parts[1]; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
1 |
|
$newTag->setValue($t); |
117
|
|
|
|
118
|
1 |
|
$tags->add($newTag); |
119
|
1 |
|
} |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
return $tags; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* @param array $tags |
128
|
|
|
* @return TripleTagSet |
129
|
|
|
*/ |
130
|
1 |
|
public static function convertArrayToTagSet(array $tags) |
131
|
|
|
{ |
132
|
1 |
|
$tagSet = new TripleTagSet(); |
133
|
|
|
|
134
|
1 |
|
foreach ($tags as $tag) { |
135
|
1 |
|
$newTag = new TripleTag( |
136
|
1 |
|
$tag['value'], |
137
|
1 |
|
$tag['predicate'], |
138
|
1 |
|
$tag['namespace'] |
139
|
1 |
|
); |
140
|
|
|
|
141
|
1 |
|
$tagSet->add($newTag); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
1 |
|
return $tagSet; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.