ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ILIAS\Refinery\Custom\Constraint Class Reference
+ Inheritance diagram for ILIAS\Refinery\Custom\Constraint:
+ Collaboration diagram for ILIAS\Refinery\Custom\Constraint:

Public Member Functions

 __construct (callable $is_ok, $error, Data\Factory $data_factory, \ilLanguage $lng)
 If $error is a callable it needs to take two parameters: More...
 
 check ($value)
 Checks the provided value.Should not throw if accepts($value).
Exceptions
More...
 
 accepts ($value)
 Tells if the provided value complies.
Parameters
mixed$value
Returns
bool
More...
 
 problemWith ($value)
 Tells what the problem with the provided value is.Should return null if accepts($value).
Parameters
mixed$value
Returns
string|null
More...
 
 applyTo (Result $result)
 Restricts a Result.Must do nothing with the result if $result->isError(). Must replace the result with an error according to problemWith() if !accepts($result->value()).
Parameters
Result$result
Returns
Result
More...
 
 withProblemBuilder (callable $builder)
 Get a constraint like this one with a builder for a custom error message.problemWith() must return an error message according to the new builder for the new constraint.The builder needs to be callable that takes two parameters:
Parameters
callable$builder
Returns
Constraint
More...
 
 getErrorMessage ($value)
 Get the problem message. More...
 
- Public Member Functions inherited from ILIAS\Refinery\Transformation
 transform ($from)
 Perform the transformation. More...
 
 __invoke ($from)
 Transformations should be callable. More...
 

Protected Member Functions

 getLngClosure ()
 Get the closure to be passed to the error-function that does i18n and sprintf. More...
 

Protected Attributes

 $data_factory
 
 $lng
 
 $is_ok
 
 $error
 

Detailed Description

Definition at line 12 of file Constraint.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Refinery\Custom\Constraint::__construct ( callable  $is_ok,
  $error,
Data\Factory  $data_factory,
\ilLanguage  $lng 
)

If $error is a callable it needs to take two parameters:

  • one callback $txt($lng_id, ($value, ...)) that retrieves the lang var with the given id and uses sprintf to replace placeholder if more values are provide.
  • the $value for which the error message should be build.
Parameters
string | callable$error

Definition at line 46 of file Constraint.php.

References ILIAS\Refinery\Custom\Constraint\$data_factory, ILIAS\Refinery\Custom\Constraint\$error, ILIAS\Refinery\Custom\Constraint\$is_ok, and ILIAS\Refinery\Custom\Constraint\$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  }

Member Function Documentation

◆ accepts()

ILIAS\Refinery\Custom\Constraint::accepts (   $value)
final

Tells if the provided value complies.

Parameters
mixed$value
Returns
bool

Implements ILIAS\Refinery\Constraint.

Definition at line 77 of file Constraint.php.

Referenced by ILIAS\Refinery\Custom\Constraint\check(), and ILIAS\Refinery\Custom\Constraint\problemWith().

78  {
79  return call_user_func($this->is_ok, $value);
80  }
+ Here is the caller graph for this function:

◆ applyTo()

ILIAS\Refinery\Custom\Constraint::applyTo ( Result  $result)
final

Restricts a Result.Must do nothing with the result if $result->isError(). Must replace the result with an error according to problemWith() if !accepts($result->value()).

Parameters
Result$result
Returns
Result

Implements ILIAS\Refinery\Constraint.

Definition at line 97 of file Constraint.php.

References ILIAS\Refinery\Custom\Constraint\$error, $result, ILIAS\Data\Result\isError(), ILIAS\Refinery\Custom\Constraint\problemWith(), and ILIAS\Data\Result\value().

97  : 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  }
$result
problemWith($value)
Tells what the problem with the provided value is.Should return null if accepts($value).string|null
Definition: Constraint.php:85
+ Here is the call graph for this function:

◆ check()

ILIAS\Refinery\Custom\Constraint::check (   $value)
final

Checks the provided value.Should not throw if accepts($value).

Exceptions

Implements ILIAS\Refinery\Constraint.

Definition at line 65 of file Constraint.php.

References ILIAS\Refinery\Custom\Constraint\accepts(), and ILIAS\Refinery\Custom\Constraint\getErrorMessage().

66  {
67  if (!$this->accepts($value)) {
68  throw new \UnexpectedValueException($this->getErrorMessage($value));
69  }
70 
71  return null;
72  }
accepts($value)
Tells if the provided value complies.bool
Definition: Constraint.php:77
getErrorMessage($value)
Get the problem message.
Definition: Constraint.php:127
+ Here is the call graph for this function:

◆ getErrorMessage()

ILIAS\Refinery\Custom\Constraint::getErrorMessage (   $value)
final

Get the problem message.

Returns
string

Definition at line 127 of file Constraint.php.

References ILIAS\Refinery\Custom\Constraint\getLngClosure().

Referenced by ILIAS\Refinery\Custom\Constraint\check(), and ILIAS\Refinery\Custom\Constraint\problemWith().

128  {
129  $lng_closure = $this->getLngClosure();
130  return call_user_func($this->error, $lng_closure, $value);
131  }
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
Definition: Constraint.php:139
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getLngClosure()

ILIAS\Refinery\Custom\Constraint::getLngClosure ( )
finalprotected

Get the closure to be passed to the error-function that does i18n and sprintf.

Returns

Definition at line 139 of file Constraint.php.

References ILIAS\Refinery\Custom\Constraint\$error, and $i.

Referenced by ILIAS\Refinery\Custom\Constraint\getErrorMessage().

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  }
$i
Definition: metadata.php:24
+ Here is the caller graph for this function:

◆ problemWith()

ILIAS\Refinery\Custom\Constraint::problemWith (   $value)
final

Tells what the problem with the provided value is.Should return null if accepts($value).

Parameters
mixed$value
Returns
string|null

Implements ILIAS\Refinery\Constraint.

Definition at line 85 of file Constraint.php.

References ILIAS\Refinery\Custom\Constraint\accepts(), and ILIAS\Refinery\Custom\Constraint\getErrorMessage().

Referenced by ILIAS\Refinery\Custom\Constraint\applyTo().

86  {
87  if (!$this->accepts($value)) {
88  return $this->getErrorMessage($value);
89  }
90 
91  return null;
92  }
accepts($value)
Tells if the provided value complies.bool
Definition: Constraint.php:77
getErrorMessage($value)
Get the problem message.
Definition: Constraint.php:127
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ withProblemBuilder()

ILIAS\Refinery\Custom\Constraint::withProblemBuilder ( callable  $builder)
final

Get a constraint like this one with a builder for a custom error message.problemWith() must return an error message according to the new builder for the new constraint.The builder needs to be callable that takes two parameters:

Parameters
callable$builder
Returns
Constraint

Implements ILIAS\Refinery\Constraint.

Definition at line 115 of file Constraint.php.

References $builder.

116  {
117  $clone = clone $this;
118  $clone->error = $builder;
119  return $clone;
120  }
$builder
Definition: parser.php:5

Field Documentation

◆ $data_factory

◆ $error

ILIAS\Refinery\Custom\Constraint::$error
protected

◆ $is_ok

ILIAS\Refinery\Custom\Constraint::$is_ok
protected

Definition at line 30 of file Constraint.php.

Referenced by ILIAS\Refinery\Custom\Constraint\__construct().

◆ $lng


The documentation for this class was generated from the following file: