Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
helpers_RdfDiff | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
380 | |
0.00% |
0 / 1 |
create | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getTriplesToAdd | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTriplesToRemove | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
add | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
remove | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
generateSerial | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getSummary | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
dump | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
applyTo | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | use oat\generis\model\data\Model; |
4 | |
5 | class helpers_RdfDiff |
6 | { |
7 | private $added = []; |
8 | |
9 | private $removed = []; |
10 | |
11 | private $toAdd = []; |
12 | |
13 | private $toRemove = []; |
14 | |
15 | /** |
16 | * Creates a diff from a set of triples to a set of troples |
17 | * |
18 | * |
19 | * @param Traversable $from |
20 | * @param Traversable $to |
21 | */ |
22 | public static function create(Traversable $from, Traversable $to) |
23 | { |
24 | $diff = new self(); |
25 | |
26 | foreach ($to as $triple) { |
27 | $diff->add($triple); |
28 | } |
29 | |
30 | |
31 | foreach ($from as $triple) { |
32 | $diff->remove($triple); |
33 | } |
34 | return $diff; |
35 | } |
36 | |
37 | /** |
38 | * @return Iterator |
39 | */ |
40 | public function getTriplesToAdd() |
41 | { |
42 | return new ArrayIterator($this->toAdd); |
43 | } |
44 | |
45 | /** |
46 | * @return Iterator |
47 | */ |
48 | public function getTriplesToRemove() |
49 | { |
50 | return new ArrayIterator($this->toRemove); |
51 | } |
52 | |
53 | protected function add(core_kernel_classes_Triple $triple) |
54 | { |
55 | $serial = $this->generateSerial($triple); |
56 | $this->added[$serial] = true; |
57 | if (! isset($this->removed[$serial])) { |
58 | $this->toAdd[$serial] = $triple; |
59 | } elseif (isset($this->toRemove[$serial])) { |
60 | unset($this->toRemove[$serial]); |
61 | } |
62 | } |
63 | |
64 | protected function remove(core_kernel_classes_Triple $triple) |
65 | { |
66 | $serial = $this->generateSerial($triple); |
67 | $this->removed[$serial] = true; |
68 | if (! isset($this->added[$serial])) { |
69 | $this->toRemove[$serial] = $triple; |
70 | } elseif (isset($this->toAdd[$serial])) { |
71 | unset($this->toAdd[$serial]); |
72 | } |
73 | } |
74 | |
75 | protected function generateSerial(core_kernel_classes_Triple $triple) |
76 | { |
77 | return md5( |
78 | implode( |
79 | ' ', |
80 | [$triple->subject, $triple->predicate, $triple->object, (string)$triple->lg] |
81 | ) |
82 | ); |
83 | } |
84 | |
85 | public function getSummary() |
86 | { |
87 | return count($this->toAdd) . ' triples to add and ' . count($this->toRemove) . ' triples to remove'; |
88 | } |
89 | |
90 | public function dump() |
91 | { |
92 | foreach ($this->toAdd as $triple) { |
93 | echo '+ ' . str_pad($triple->subject, 80) . ' ' . str_pad($triple->predicate, 80) . ' ' |
94 | . str_pad($triple->object, 80) . PHP_EOL; |
95 | } |
96 | foreach ($this->toRemove as $triple) { |
97 | echo '- ' . str_pad($triple->subject, 80) . ' ' . str_pad($triple->predicate, 80) . ' ' |
98 | . str_pad($triple->object, 80) . PHP_EOL; |
99 | } |
100 | } |
101 | |
102 | public function applyTo(Model $model) |
103 | { |
104 | $rdf = $model->getRdfInterface(); |
105 | foreach ($this->getTriplesToRemove() as $triple) { |
106 | $rdf->remove($triple); |
107 | } |
108 | foreach ($this->getTriplesToAdd() as $triple) { |
109 | $rdf->add($triple); |
110 | } |
111 | } |
112 | } |