ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Constraint.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Refinery\Constraint as ConstraintInterface;
26use ILIAS\Data;
29
30class Constraint implements ConstraintInterface
31{
35
36 protected Data\Factory $data_factory;
37 protected \ILIAS\Language\Language $lng;
39 protected $is_ok;
41 protected $error;
42
56 {
57 $this->is_ok = $is_ok;
58 $this->error = $error;
59 $this->data_factory = $data_factory;
60 $this->lng = $lng;
61 }
62
66 protected function getError()
67 {
68 return $this->error;
69 }
70
74 final public function check($value)
75 {
76 if (!$this->accepts($value)) {
77 throw new \UnexpectedValueException($this->getErrorMessage($value));
78 }
79
80 return null;
81 }
82
86 final public function accepts($value): bool
87 {
88 return call_user_func($this->is_ok, $value);
89 }
90
94 final public function problemWith($value): ?string
95 {
96 if (!$this->accepts($value)) {
97 return $this->getErrorMessage($value);
98 }
99
100 return null;
101 }
102
106 final public function applyTo(Result $result): Result
107 {
108 if ($result->isError()) {
109 return $result;
110 }
111
112 $problem = $this->problemWith($result->value());
113 if ($problem !== null) {
114 $error = $this->data_factory->error($problem);
115 return $error;
116 }
117
118 return $result;
119 }
120}
Builds data types.
Definition: Factory.php:36
__construct(callable $is_ok, $error, Data\Factory $data_factory, \ILIAS\Language\Language $lng)
If $error is a callable it needs to take two parameters:
Definition: Constraint.php:55
ILIAS Language Language $lng
Definition: Constraint.php:37
accepts($value)
@inheritDoc
Definition: Constraint.php:86
applyTo(Result $result)
@inheritDoc
Definition: Constraint.php:106
problemWith($value)
@inheritDoc
Definition: Constraint.php:94
error(string $a_errmsg)
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
isError()
Get to know if the result is an error.
value()
Get the encapsulated value.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
getErrorMessage($value)
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.