ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
RevisionCollection.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 
30 {
35  public function __construct(private ResourceIdentification $identification, private array $revisions = [])
36  {
37  }
38 
39  public function add(Revision $revision): void
40  {
41  if ($this->identification->serialize() !== $revision->getIdentification()->serialize()) {
43  "Can't add Revision since it's not the same ResourceIdentification"
44  );
45  }
46  foreach ($this->revisions as $r) {
47  if ($r->getVersionNumber() === $revision->getVersionNumber()) {
48  throw new RevisionExistsException(
49  sprintf(
50  "Can't add already existing version number: %s",
51  $revision->getVersionNumber()
52  )
53  );
54  }
55  }
56  $this->revisions[$revision->getVersionNumber()] = $revision;
57  asort($this->revisions);
58  }
59 
60  public function remove(Revision $revision): void
61  {
62  foreach ($this->revisions as $k => $revision_e) {
63  if ($revision->getVersionNumber() === $revision_e->getVersionNumber()) {
64  unset($this->revisions[$k]);
65 
66  return;
67  }
68  }
69  }
70 
71  public function replaceSingleRevision(Revision $revision): void
72  {
73  foreach ($this->revisions as $k => $revision_e) {
74  if ($revision_e->getVersionNumber() === $revision->getVersionNumber()) {
75  $this->revisions[$k] = $revision;
76  }
77  }
78  }
79 
80  public function replaceAllRevisions(Revision $revision): void
81  {
82  $this->revisions = [];
83  $this->add($revision);
84  }
85 
86  public function getCurrent(bool $including_drafts): Revision
87  {
88  $v = $this->revisions;
89 
90  if (!$including_drafts) {
91  $v = array_filter($v, static fn(Revision $revision): bool => $revision->getStatus() === RevisionStatus::PUBLISHED);
92  }
93  usort($v, static fn(Revision $a, Revision $b): int => $a->getVersionNumber() <=> $b->getVersionNumber());
94 
95  $current = end($v);
96  if (!$current instanceof Revision) {
97  return new NullRevision($this->identification);
98  }
99 
100  return $current;
101  }
102 
106  public function getAll(bool $including_drafts): array
107  {
108  if ($including_drafts) {
109  return $this->revisions;
110  }
111  return array_filter($this->revisions, static fn(Revision $revision): bool => $revision->getStatus() === RevisionStatus::PUBLISHED);
112  }
113 
114  public function getMax(bool $including_drafts): int
115  {
116  if ($this->revisions === []) {
117  return 0;
118  }
119  return $this->getCurrent($including_drafts)->getVersionNumber();
120  }
121 
122  public function getFullSize(): int
123  {
124  $size = 0;
125  foreach ($this->revisions as $revision) {
126  $size += $revision->getInformation()->getSize();
127  }
128 
129  return $size;
130  }
131 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
__construct(private ResourceIdentification $identification, private array $revisions=[])
RevisionCollection constructor.
$r