ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
SymbolDecoratorTrait.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
4
5use Closure;
8use LogicException;
9use ReflectionFunction;
10use ReflectionType;
11
18{
19
23 private $symbol_decorator;
24
29 public function addSymbolDecorator(Closure $symbol_decorator) : hasSymbol
30 {
31 if (!$this->checkClosure($symbol_decorator)) {
32 throw new LogicException('first argument of closure must be type-hinted to \ILIAS\UI\Component\Symbol\Symbol');
33 }
34 if ($this->symbol_decorator instanceof Closure) {
35 $existing = $this->symbol_decorator;
36 $this->symbol_decorator = static function (Symbol $c) use ($symbol_decorator, $existing) : Symbol {
37 $component = $existing($c);
38
39 return $symbol_decorator($component);
40 };
41 } else {
42 $this->symbol_decorator = $symbol_decorator;
43 }
44
45 return $this;
46 }
47
51 public function getSymbolDecorator() : ?Closure
52 {
53 return $this->symbol_decorator;
54 }
55
56 private function checkClosure(Closure $c) : bool
57 {
58 try {
59 $r = new ReflectionFunction($c);
60 if (count($r->getParameters()) !== 1) {
61 return false;
62 }
63 $first_param_type = $r->getParameters()[0]->getType();
64 if ($first_param_type instanceof ReflectionType && $first_param_type->getName() !== Symbol::class) {
65 return false;
66 }
67 $return_type = $r->getReturnType();
68 if ($return_type === null) {
69 return false;
70 }
71 if ($return_type->getName() !== Symbol::class) {
72 return false;
73 }
74
75 return true;
76 } catch (\Throwable $i) {
77 return false;
78 }
79 }
80}
An exception for terminatinating execution or to throw for unit testing.
Interface hasSymbol Methods for Entries with Symbols.
Definition: hasSymbol.php:12
A component is the most general form of an entity in the UI.
Definition: Component.php:14
This describes a symbol.
Definition: Symbol.php:12
$i
Definition: metadata.php:24