ILIAS  release_7 Revision v7.30-3-g800a261c036
Constraint.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
5
6use ILIAS\Refinery\Constraint as ConstraintInterface;
9use ILIAS\Data;
12
13class Constraint implements ConstraintInterface
14{
18
22 protected $data_factory;
23
27 protected $lng;
28
32 protected $is_ok;
33
37 protected $error;
38
48 public function __construct(callable $is_ok, $error, Data\Factory $data_factory, \ilLanguage $lng)
49 {
50 $this->is_ok = $is_ok;
51 $this->error = $error;
52 $this->data_factory = $data_factory;
53 $this->lng = $lng;
54 }
55
59 protected function getError()
60 {
61 return $this->error;
62 }
63
67 final public function check($value)
68 {
69 if (!$this->accepts($value)) {
70 throw new \UnexpectedValueException($this->getErrorMessage($value));
71 }
72
73 return null;
74 }
75
79 final public function accepts($value)
80 {
81 return call_user_func($this->is_ok, $value);
82 }
83
87 final public function problemWith($value)
88 {
89 if (!$this->accepts($value)) {
90 return $this->getErrorMessage($value);
91 }
92
93 return null;
94 }
95
99 final public function applyTo(Result $result) : 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 }
113}
$result
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
check($value)
Checks the provided value.Should not throw if accepts($value).UnexpectedValueException if value does ...
Definition: Constraint.php:67
accepts($value)
Tells if the provided value complies.bool
Definition: Constraint.php:79
applyTo(Result $result)
Restricts a Result.Must do nothing with the result if $result->isError(). Must replace the result wit...
Definition: Constraint.php:99
problemWith($value)
Tells what the problem with the provided value is.Should return null if accepts($value)....
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:48
error($a_errmsg)
set error message @access public
language handling
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:12
value()
Get the encapsulated value.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:15
getErrorMessage($value)
Get the problem message.