ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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)
 
 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$value
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.
Parameters
callable$buildermixed -> string
Returns
Constraint
More...
 
 getErrorMessage ($value)
 Get the problem message. More...
 

Protected Attributes

 $data_factory
 
 $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 
)
Parameters
string | callable$error

Definition at line 30 of file Custom.php.

References ILIAS\Validation\Constraints\Custom\$data_factory, ILIAS\Validation\Constraints\Custom\$error, and ILIAS\Validation\Constraints\Custom\$is_ok.

31  {
32  $this->is_ok = $is_ok;
33 
34  if (!is_callable($error)) {
35  $this->error = function () use ($error) {
36  return $error;
37  };
38  } else {
39  $this->error = $error;
40  }
41 
42  $this->data_factory = $data_factory;
43  }

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

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

61  {
62  return call_user_func($this->is_ok, $value);
63  }
+ 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 48 of file Custom.php.

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

49  {
50  if (!$this->accepts($value)) {
51  throw new \UnexpectedValueException($this->getErrorMessage($value));
52  }
53 
54  return null;
55  }
accepts($value)
Tells if the provided value complies.bool
Definition: Custom.php:60
getErrorMessage($value)
Get the problem message.
Definition: Custom.php:110
+ Here is the call graph for this function:

◆ getErrorMessage()

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

Get the problem message.

Returns
string

Definition at line 110 of file Custom.php.

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

111  {
112  return call_user_func($this->error, $value);
113  }
+ 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 68 of file Custom.php.

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

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

69  {
70  if (!$this->accepts($value)) {
71  return $this->getErrorMessage($value);
72  }
73 
74  return null;
75  }
accepts($value)
Tells if the provided value complies.bool
Definition: Custom.php:60
getErrorMessage($value)
Get the problem message.
Definition: Custom.php:110
+ 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$value
Returns
Result

Implements ILIAS\Validation\Constraint.

Definition at line 80 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().

81  {
82  if ($result->isError()) {
83  return $result;
84  }
85 
86  $problem = $this->problemWith($result->value());
87  if ($problem !== null) {
88  $error = $this->data_factory->error($problem);
89  return $error;
90  }
91 
92  return $result;
93  }
$result
problemWith($value)
Tells what the problem with the provided value is.Should return null if accepts($value).string|null
Definition: Custom.php:68
+ Here is the call 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.

Parameters
callable$buildermixed -> string
Returns
Constraint

Implements ILIAS\Validation\Constraint.

Definition at line 98 of file Custom.php.

References $builder.

99  {
100  $clone = clone $this;
101  $clone->error = $builder;
102  return $clone;
103  }

Field Documentation

◆ $data_factory

◆ $error

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

◆ $is_ok

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

Definition at line 20 of file Custom.php.

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


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