ILIAS  release_7 Revision v7.30-3-g800a261c036
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...
 
- Public Member Functions inherited from ILIAS\Refinery\Constraint
 withProblemBuilder (callable $builder)
 Get a constraint like this one with a builder for a custom error 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

 getError ()
 

Protected Attributes

 $data_factory
 
 $lng
 
 $is_ok
 
 $error
 

Detailed Description

Definition at line 13 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 48 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.

49  {
50  $this->is_ok = $is_ok;
51  $this->error = $error;
52  $this->data_factory = $data_factory;
53  $this->lng = $lng;
54  }

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 79 of file Constraint.php.

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

80  {
81  return call_user_func($this->is_ok, $value);
82  }
+ 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 99 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().

99  : Result
100  {
101  if ($result->isError()) {
102  return $result;
103  }
104 
105  $problem = $this->problemWith($result->value());
106  if ($problem !== null) {
107  $error = $this->data_factory->error($problem);
108  return $error;
109  }
110 
111  return $result;
112  }
$result
problemWith($value)
Tells what the problem with the provided value is.Should return null if accepts($value).string|null
Definition: Constraint.php:87
+ 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 67 of file Constraint.php.

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

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

◆ getError()

ILIAS\Refinery\Custom\Constraint::getError ( )
protected

Definition at line 59 of file Constraint.php.

References ILIAS\Refinery\Custom\Constraint\$error.

60  {
61  return $this->error;
62  }

◆ 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 87 of file Constraint.php.

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

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

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

Field Documentation

◆ $data_factory

◆ $error

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

◆ $is_ok

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

Definition at line 32 of file Constraint.php.

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

◆ $lng


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