ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
Constraint.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
6use ILIAS\Refinery\Constraint as ConstraintInterface;
9use ILIAS\Data;
11
12class Constraint implements ConstraintInterface
13{
16
20 protected $data_factory;
21
25 protected $lng;
26
30 protected $is_ok;
31
35 protected $error;
36
46 public function __construct(callable $is_ok, $error, Data\Factory $data_factory, \ilLanguage $lng)
47 {
48 $this->is_ok = $is_ok;
49
50 if (!is_callable($error)) {
51 $this->error = function () use ($error) {
52 return $error;
53 };
54 } else {
55 $this->error = $error;
56 }
57
58 $this->data_factory = $data_factory;
59 $this->lng = $lng;
60 }
61
65 final public function check($value)
66 {
67 if (!$this->accepts($value)) {
68 throw new \UnexpectedValueException($this->getErrorMessage($value));
69 }
70
71 return null;
72 }
73
77 final public function accepts($value)
78 {
79 return call_user_func($this->is_ok, $value);
80 }
81
85 final public function problemWith($value)
86 {
87 if (!$this->accepts($value)) {
88 return $this->getErrorMessage($value);
89 }
90
91 return null;
92 }
93
97 final public function applyTo(Result $result) : Result
98 {
99 if ($result->isError()) {
100 return $result;
101 }
102
103 $problem = $this->problemWith($result->value());
104 if ($problem !== null) {
105 $error = $this->data_factory->error($problem);
106 return $error;
107 }
108
109 return $result;
110 }
111
115 final public function withProblemBuilder(callable $builder)
116 {
117 $clone = clone $this;
118 $clone->error = $builder;
119 return $clone;
120 }
121
127 final public function getErrorMessage($value)
128 {
129 $lng_closure = $this->getLngClosure();
130 return call_user_func($this->error, $lng_closure, $value);
131 }
132
139 final protected function getLngClosure()
140 {
141 return function () {
142 $args = func_get_args();
143 if (count($args) < 1) {
144 throw new \InvalidArgumentException(
145 "Expected an id of a lang var as first parameter"
146 );
147 }
148 $error = $this->lng->txt($args[0]);
149 if (count($args) > 1) {
150 $args[0] = $error;
151 for ($i = 0; $i < count($args); $i++) {
152 $v = $args[$i];
153 if ((is_array($v) || is_object($v) || is_null($v))
154 && !method_exists($v, "__toString")) {
155 if (is_array($v)) {
156 $args[$i] = "array";
157 } elseif (is_null($v)) {
158 $args[$i] = "null";
159 } else {
160 $args[$i] = get_class($v);
161 }
162 }
163 }
164 $error = call_user_func_array("sprintf", $args);
165 }
166 return $error;
167 };
168 }
169}
$result
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
check($value)
Checks the provided value.Should not throw if accepts($value).UnexpectedValueException if value does ...
Definition: Constraint.php:65
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
Definition: Constraint.php:139
accepts($value)
Tells if the provided value complies.bool
Definition: Constraint.php:77
withProblemBuilder(callable $builder)
Get a constraint like this one with a builder for a custom error message.problemWith() must return an...
Definition: Constraint.php:115
getErrorMessage($value)
Get the problem message.
Definition: Constraint.php:127
applyTo(Result $result)
Restricts a Result.Must do nothing with the result if $result->isError(). Must replace the result wit...
Definition: Constraint.php:97
problemWith($value)
Tells what the problem with the provided value is.Should return null if accepts($value)....
Definition: Constraint.php:85
__construct(callable $is_ok, $error, Data\Factory $data_factory, \ilLanguage $lng)
If $error is a callable it needs to take two parameters:
Definition: Constraint.php:46
error($a_errmsg)
set error message @access public
language handling
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
$i
Definition: metadata.php:24
$builder
Definition: parser.php:5