ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
RevisionCollection.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 
30 {
35  private array $revisions = [];
36 
41  public function __construct(ResourceIdentification $identification, array $revisions = [])
42  {
43  $this->identification = $identification;
44  $this->revisions = $revisions;
45  }
46 
47  public function add(Revision $revision): void
48  {
49  if ($this->identification->serialize() !== $revision->getIdentification()->serialize()) {
51  "Can't add Revision since it's not the same ResourceIdentification"
52  );
53  }
54  foreach ($this->revisions as $r) {
55  if ($r->getVersionNumber() === $revision->getVersionNumber()) {
56  throw new RevisionExistsException(
57  sprintf(
58  "Can't add already existing version number: %s",
59  $revision->getVersionNumber()
60  )
61  );
62  }
63  }
64  $this->revisions[$revision->getVersionNumber()] = $revision;
65  asort($this->revisions);
66  }
67 
68  public function remove(Revision $revision): void
69  {
70  foreach ($this->revisions as $k => $revision_e) {
71  if ($revision->getVersionNumber() === $revision_e->getVersionNumber()) {
72  unset($this->revisions[$k]);
73 
74  return;
75  }
76  }
77  }
78 
79  public function replaceSingleRevision(Revision $revision): void
80  {
81  foreach ($this->revisions as $k => $revision_e) {
82  if ($revision_e->getVersionNumber() === $revision->getVersionNumber()) {
83  $this->revisions[$k] = $revision;
84  }
85  }
86  }
87 
88  public function replaceAllRevisions(Revision $revision): void
89  {
90  $this->revisions = [];
91  $this->add($revision);
92  }
93 
94  public function getCurrent(): Revision
95  {
96  $v = array_values($this->revisions);
97  sort($v);
98  $current = end($v);
99  if (!$current instanceof Revision) {
100  $current = new NullRevision($this->identification);
101  }
102 
103  return $current;
104  }
105 
109  public function getAll(): array
110  {
111  return $this->revisions;
112  }
113 
114  public function getMax(): int
115  {
116  if ($this->revisions === []) {
117  return 0;
118  }
119  return max(array_keys($this->revisions));
120  }
121 }
__construct(ResourceIdentification $identification, array $revisions=[])
RevisionCollection constructor.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...