ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ILIAS\Validation\Constraints\Custom Class Reference
+ Inheritance diagram for ILIAS\Validation\Constraints\Custom:
+ Collaboration diagram for ILIAS\Validation\Constraints\Custom:

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...
 
 restrict (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...
 

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 10 of file Custom.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Validation\Constraints\Custom::__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 41 of file Custom.php.

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

Member Function Documentation

◆ accepts()

ILIAS\Validation\Constraints\Custom::accepts (   $value)
final

Tells if the provided value complies.

Parameters
mixed$value
Returns
bool

Implements ILIAS\Validation\Constraint.

Definition at line 72 of file Custom.php.

Referenced by ILIAS\Validation\Constraints\Custom\check(), ILIAS\Validation\Constraints\Custom\problemWith(), and LogicalOrTest\testAccept().

73  {
74  return call_user_func($this->is_ok, $value);
75  }
+ Here is the caller graph for this function:

◆ check()

ILIAS\Validation\Constraints\Custom::check (   $value)
final

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

Exceptions

Implements ILIAS\Validation\Constraint.

Definition at line 60 of file Custom.php.

References ILIAS\Validation\Constraints\Custom\accepts(), and ILIAS\Validation\Constraints\Custom\getErrorMessage().

Referenced by LogicalOrTest\testCheck().

61  {
62  if (!$this->accepts($value)) {
63  throw new \UnexpectedValueException($this->getErrorMessage($value));
64  }
65 
66  return null;
67  }
accepts($value)
Tells if the provided value complies.bool
Definition: Custom.php:72
getErrorMessage($value)
Get the problem message.
Definition: Custom.php:122
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getErrorMessage()

ILIAS\Validation\Constraints\Custom::getErrorMessage (   $value)
final

Get the problem message.

Returns
string

Definition at line 122 of file Custom.php.

References ILIAS\Validation\Constraints\Custom\getLngClosure().

Referenced by ILIAS\Validation\Constraints\Custom\check(), and ILIAS\Validation\Constraints\Custom\problemWith().

123  {
124  $lng_closure = $this->getLngClosure();
125  return call_user_func($this->error, $lng_closure, $value);
126  }
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
Definition: Custom.php:134
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getLngClosure()

ILIAS\Validation\Constraints\Custom::getLngClosure ( )
finalprotected

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

Returns

Definition at line 134 of file Custom.php.

References ILIAS\Validation\Constraints\Custom\$error, and $i.

Referenced by ILIAS\Validation\Constraints\Custom\getErrorMessage().

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

◆ problemWith()

ILIAS\Validation\Constraints\Custom::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\Validation\Constraint.

Definition at line 80 of file Custom.php.

References ILIAS\Validation\Constraints\Custom\accepts(), and ILIAS\Validation\Constraints\Custom\getErrorMessage().

Referenced by ILIAS\Validation\Constraints\Custom\restrict(), and LogicalOrTest\testProblemWith().

81  {
82  if (!$this->accepts($value)) {
83  return $this->getErrorMessage($value);
84  }
85 
86  return null;
87  }
accepts($value)
Tells if the provided value complies.bool
Definition: Custom.php:72
getErrorMessage($value)
Get the problem message.
Definition: Custom.php:122
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ restrict()

ILIAS\Validation\Constraints\Custom::restrict ( 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\Validation\Constraint.

Definition at line 92 of file Custom.php.

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

Referenced by LogicalOrTest\testRestrict().

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

◆ withProblemBuilder()

ILIAS\Validation\Constraints\Custom::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\Validation\Constraint.

Definition at line 110 of file Custom.php.

References $builder.

Referenced by LogicalOrTest\testWithProblemBuilder().

111  {
112  $clone = clone $this;
113  $clone->error = $builder;
114  return $clone;
115  }
$builder
Definition: parser.php:5
+ Here is the caller graph for this function:

Field Documentation

◆ $data_factory

◆ $error

ILIAS\Validation\Constraints\Custom::$error
protected

◆ $is_ok

ILIAS\Validation\Constraints\Custom::$is_ok
protected

Definition at line 25 of file Custom.php.

Referenced by ILIAS\Validation\Constraints\Custom\__construct().

◆ $lng


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