ILIAS  release_7 Revision v7.30-3-g800a261c036
RevisionCollection.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
4
6
12{
13
17 private $revisions = [];
22
29 {
30 $this->identification = $identification;
31 $this->revisions = $revisions;
32 }
33
34 public function add(Revision $revision) : void
35 {
36 if ($this->identification->serialize() !== $revision->getIdentification()->serialize()) {
37 throw new NonMatchingIdentificationException("Can't add Revision since it's not the same ResourceIdentification");
38 }
39 foreach ($this->revisions as $r) {
40 if ($r->getVersionNumber() === $revision->getVersionNumber()) {
41 throw new RevisionExistsException(sprintf("Can't add already existing version number: %s",
42 $revision->getVersionNumber()));
43 }
44 }
45 $this->revisions[$revision->getVersionNumber()] = $revision;
46 asort($this->revisions);
47 }
48
49 public function remove(Revision $revision) : void
50 {
51 foreach ($this->revisions as $k => $revision_e) {
52 if ($revision->getVersionNumber() === $revision_e->getVersionNumber()) {
53 unset($this->revisions[$k]);
54
55 return;
56 }
57 }
58 }
59
60 public function replaceSingleRevision(Revision $revision) : void
61 {
62 foreach ($this->revisions as $k => $revision_e) {
63 if ($revision_e->getVersionNumber() === $revision->getVersionNumber()) {
64 $this->revisions[$k] = $revision;
65 }
66 }
67 }
68
69 public function replaceAllRevisions(Revision $revision) : void
70 {
71 $this->revisions = [];
72 $this->add($revision);
73 }
74
75 public function getCurrent() : Revision
76 {
77 $v = array_values($this->revisions);
78 sort($v);
79 $current = end($v);
80 if (!$current instanceof Revision) {
81 $current = new NullRevision($this->identification);
82 }
83
84 return $current;
85 }
86
90 public function getAll() : array
91 {
92 return $this->revisions;
93 }
94
95 public function getMax() : int
96 {
97 if (count($this->revisions) === 0) {
98 return 0;
99 }
100 return max(array_keys($this->revisions));
101 }
102}
An exception for terminatinating execution or to throw for unit testing.
__construct(ResourceIdentification $identification, array $revisions=[])
RevisionCollection constructor.