ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
AbstractLayoutModification.php
Go to the documentation of this file.
2 
3 use Closure;
6 
13 {
14 
18  private $priority;
22  private $modification = null;
23 
24 
28  public function isFinal() : bool
29  {
30  return false;
31  }
32 
33 
37  public function getPriority() : int
38  {
39  return $this->priority ?? LayoutModification::PRIORITY_LOW;
40  }
41 
42 
46  final public function withPriority(int $priority) : LayoutModification
47  {
48  if ((self::PRIORITY_LOW > $priority) || ($priority > self::PRIORITY_HIGH)) {
49  throw new LogicException("\$priority MUST be between LayoutModification::PRIORITY_LOW, LayoutModification::PRIORITY_MEDIUM or LayoutModification::PRIORITY_HIGH");
50  }
51  $clone = clone $this;
52  $clone->priority = $priority;
53 
54  return $clone;
55  }
56 
57 
61  final public function withHighPriority() : LayoutModification
62  {
63  $clone = clone $this;
64  $clone->priority = LayoutModification::PRIORITY_HIGH;
65 
66  return $clone;
67  }
68 
69 
73  final public function withLowPriority() : LayoutModification
74  {
75  $clone = clone $this;
76  $clone->priority = LayoutModification::PRIORITY_LOW;
77 
78  return $clone;
79  }
80 
81 
87  final public function withModification(Closure $closure) : LayoutModification
88  {
89  $clone = clone $this;
90  $clone->modification = $closure;
91 
92  return $clone;
93  }
94 
95 
99  final public function getModification() : Closure
100  {
101  return $this->modification;
102  }
103 
104 
108  final public function hasValidModification() : bool
109  {
110  return ($this->modification instanceof Closure && $this->checkClosure());
111  }
112 
113 
117  private function checkClosure() : bool
118  {
119  $closure = $this->modification;
120  $return_type = $this->getClosureReturnType();
121 
122  try {
123  $r = new ReflectionFunction($closure);
124  // First Argument
125  if (!$this->firstArgumentAllowsNull()) {
126  $first_argument_type = $this->getClosureFirstArgumentType();
127  if (!isset($r->getParameters()[0])
128  || !$r->getParameters()[0]->hasType()
129  || ($r->getParameters()[0]->getType()->getName() !== $first_argument_type)
130  ) {
131  return false;
132  }
133  }
134 
135  // Return type
136  if (!$this->returnTypeAllowsNull()) {
137  if (!$r->hasReturnType()
138  || ($r->getReturnType()->getName() !== $return_type)
139  ) {
140  return false;
141  }
142  }
143  } catch (\ReflectionException $e) {
144  return false;
145  }
146 
147  return true;
148  }
149 }