1 <?php
2
3 namespace Docoflow\Entity;
4
5 use Exception;
6 use Docoflow\Traits\Entity;
7 use Docolight\Support\Collection;
8
9 10 11 12 13
14 class Group extends Collection
15 {
16 use Entity;
17
18 19 20 21 22 23 24
25 public function rebuild(Step &$steps)
26 {
27 $groups = new static;
28
29 foreach ($this as $group) {
30 $group = fluent($group);
31
32 if (! $groupId = $group->{'$id'}) {
33 throw new Exception("Group doesn't have an id.");
34 }
35
36 if (! $assignedStep = $group->{'$step'}) {
37 throw new Exception("Group doesn't have any assigned step id.");
38 }
39
40 if ($steps->has($assignedStep)) {
41 if ($steps->get($assignedStep)->groups->has($groupId)) {
42 throw new Exception("Group id [$groupId] has been assigned before and it can't be overriden.");
43 }
44
45 $group->verificator = new Verificator();
46
47 $groups->offsetSet($groupId, $group);
48
49 $steps->pushGroup($groupId, $group);
50 $steps->get($assignedStep)->groups->offsetSet($groupId, $group);
51 } else {
52 throw new Exception("Assigned step [$assignedStep] doesn't exist.");
53 }
54 }
55
56 return $groups;
57 }
58
59 60 61 62 63
64 public function gatherVerificators()
65 {
66 $verificators = new Verificator();
67
68 foreach ($this as $group) {
69 $verificators->assign($group->getRelated('verificators'));
70 }
71
72 return $verificators;
73 }
74
75 76 77 78 79
80 public function approve()
81 {
82 $this->gatherVerificators()->approve();
83
84 return $this;
85 }
86
87 88 89 90 91
92 public function approveIf()
93 {
94 $this->gatherVerificators()->approveIf();
95
96 return $this;
97 }
98
99 100 101 102 103
104 public function reject()
105 {
106 $this->gatherVerificators()->reject();
107
108 return $this;
109 }
110
111 112 113 114 115
116 public function rejectIf()
117 {
118 $this->gatherVerificators()->rejectIf();
119
120 return $this;
121 }
122
123 124 125 126 127
128 public function reset()
129 {
130 $this->gatherVerificators()->reset();
131
132 return $this;
133 }
134
135 136 137 138 139
140 public function resetIf()
141 {
142 $this->gatherVerificators()->resetIf();
143
144 return $this;
145 }
146 }
147