ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Custom.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
5
7use ILIAS\Data;
9
10class Custom implements Constraint
11{
15 protected $data_factory;
16
20 protected $lng;
21
25 protected $is_ok;
26
30 protected $error;
31
41 public function __construct(callable $is_ok, $error, Data\Factory $data_factory, \ilLanguage $lng)
42 {
43 $this->is_ok = $is_ok;
44
45 if (!is_callable($error)) {
46 $this->error = function () use ($error) {
47 return $error;
48 };
49 } else {
50 $this->error = $error;
51 }
52
53 $this->data_factory = $data_factory;
54 $this->lng = $lng;
55 }
56
60 final public function check($value)
61 {
62 if (!$this->accepts($value)) {
63 throw new \UnexpectedValueException($this->getErrorMessage($value));
64 }
65
66 return null;
67 }
68
72 final public function accepts($value)
73 {
74 return call_user_func($this->is_ok, $value);
75 }
76
80 final public function problemWith($value)
81 {
82 if (!$this->accepts($value)) {
83 return $this->getErrorMessage($value);
84 }
85
86 return null;
87 }
88
92 final public function restrict(Result $result)
93 {
94 if ($result->isError()) {
95 return $result;
96 }
97
98 $problem = $this->problemWith($result->value());
99 if ($problem !== null) {
100 $error = $this->data_factory->error($problem);
101 return $error;
102 }
103
104 return $result;
105 }
106
110 final public function withProblemBuilder(callable $builder)
111 {
112 $clone = clone $this;
113 $clone->error = $builder;
114 return $clone;
115 }
116
122 final public function getErrorMessage($value)
123 {
124 $lng_closure = $this->getLngClosure();
125 return call_user_func($this->error, $lng_closure, $value);
126 }
127
134 final protected function getLngClosure()
135 {
136 return function () {
137 $args = func_get_args();
138 if (count($args) < 1) {
139 throw new \InvalidArgumentException(
140 "Expected an id of a lang var as first parameter"
141 );
142 }
143 $error = $this->lng->txt($args[0]);
144 if (count($args) > 1) {
145 $args[0] = $error;
146 for ($i = 0; $i < count($args); $i++) {
147 $v = $args[$i];
148 if ((is_array($v) || is_object($v) || is_null($v))
149 && !method_exists($v, "__toString")) {
150 if (is_array($v)) {
151 $args[$i] = "array";
152 } elseif (is_null($v)) {
153 $args[$i] = "null";
154 } else {
155 $args[$i] = get_class($v);
156 }
157 }
158 }
159 $error = call_user_func_array("sprintf", $args);
160 }
161 return $error;
162 };
163 }
164}
$result
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:15
accepts($value)
Tells if the provided value complies.bool
Definition: Custom.php:72
problemWith($value)
Tells what the problem with the provided value is.Should return null if accepts($value)....
Definition: Custom.php:80
check($value)
Checks the provided value.Should not throw if accepts($value).UnexpectedValueException if value does ...
Definition: Custom.php:60
restrict(Result $result)
Restricts a Result.Must do nothing with the result if $result->isError(). Must replace the result wit...
Definition: Custom.php:92
withProblemBuilder(callable $builder)
Get a constraint like this one with a builder for a custom error message.problemWith() must return an...
Definition: Custom.php:110
__construct(callable $is_ok, $error, Data\Factory $data_factory, \ilLanguage $lng)
If $error is a callable it needs to take two parameters:
Definition: Custom.php:41
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
Definition: Custom.php:134
getErrorMessage($value)
Get the problem message.
Definition: Custom.php:122
error($a_errmsg)
set error message @access public
language handling
$i
Definition: disco.tpl.php:19
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:12
value()
Get the encapsulated value.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:15
$builder
Definition: parser.php:5