ILIAS  release_8 Revision v8.24
Constraint.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22
23use ILIAS\Refinery\Constraint as ConstraintInterface;
26use ILIAS\Data;
29use ilLanguage;
30
31class Constraint implements ConstraintInterface
32{
36
38 protected ilLanguage $lng;
40 protected $is_ok;
42 protected $error;
43
56 public function __construct(callable $is_ok, $error, Data\Factory $data_factory, ilLanguage $lng)
57 {
58 $this->is_ok = $is_ok;
59 $this->error = $error;
60 $this->data_factory = $data_factory;
61 $this->lng = $lng;
62 }
63
67 protected function getError()
68 {
69 return $this->error;
70 }
71
75 final public function check($value)
76 {
77 if (!$this->accepts($value)) {
78 throw new \UnexpectedValueException($this->getErrorMessage($value));
79 }
80
81 return null;
82 }
83
87 final public function accepts($value): bool
88 {
89 return call_user_func($this->is_ok, $value);
90 }
91
95 final public function problemWith($value): ?string
96 {
97 if (!$this->accepts($value)) {
98 return $this->getErrorMessage($value);
99 }
100
101 return null;
102 }
103
107 final public function applyTo(Result $result): Result
108 {
109 if ($result->isError()) {
110 return $result;
111 }
112
113 $problem = $this->problemWith($result->value());
114 if ($problem !== null) {
115 $error = $this->data_factory->error($problem);
116 return $error;
117 }
118
119 return $result;
120 }
121}
Builds data types.
Definition: Factory.php:21
accepts($value)
@inheritDoc
Definition: Constraint.php:87
__construct(callable $is_ok, $error, Data\Factory $data_factory, ilLanguage $lng)
If $error is a callable it needs to take two parameters:
Definition: Constraint.php:56
applyTo(Result $result)
@inheritDoc
Definition: Constraint.php:107
problemWith($value)
@inheritDoc
Definition: Constraint.php:95
error(string $a_errmsg)
language handling
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:15
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Constraint.php:21
getErrorMessage($value)
Get the problem message.