ILIAS  release_7 Revision v7.30-3-g800a261c036
JavaScriptBindable.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6
12{
16 private $on_load_code_binder = null;
17
21 public function withOnLoadCode(\Closure $binder)
22 {
23 $this->checkBinder($binder);
24 $clone = clone $this;
25 $clone->on_load_code_binder = $binder;
26 return $clone;
27 }
28
32 public function withAdditionalOnLoadCode(\Closure $binder)
33 {
34 $current_binder = $this->getOnLoadCode();
35 if ($current_binder === null) {
36 return $this->withOnLoadCode($binder);
37 }
38
39 $this->checkBinder($binder);
40 return $this->withOnLoadCode(function ($id) use ($current_binder, $binder) {
41 return $current_binder($id) . "\n" . $binder($id);
42 });
43 }
44
48 public function getOnLoadCode()
49 {
50 return $this->on_load_code_binder;
51 }
52
58 private function checkBinder(\Closure $binder)
59 {
60 $refl = new \ReflectionFunction($binder);
61 $args = array_map(function ($arg) {
62 return $arg->name;
63 }, $refl->getParameters());
64 if (array("id") !== $args) {
65 throw new \InvalidArgumentException('Expected closure "$binder" to have exactly one argument "$id".');
66 }
67 }
68}
An exception for terminatinating execution or to throw for unit testing.
Interface to be extended by components that have the possibility to bind to Javascript.