ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ProblemBuilder.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Refinery;
22
23use Closure;
24
26{
30 abstract protected function getError();
31
32 final public function withProblemBuilder(callable $builder): self
33 {
34 $clone = clone $this;
35 $clone->error = $builder;
36 return $clone;
37 }
38
39 final public function getErrorMessage($value): string
40 {
41 $error = $this->getError();
42 if (!is_callable($error)) {
43 return $error;
44 }
45 $lng_closure = $this->getLngClosure();
46 return $error($lng_closure, $value);
47 }
48
52 final protected function getLngClosure(): Closure
53 {
54 return function (string $lang_var, ...$args): string {
55 $error = $this->lng->txt($lang_var);
56
57 if ($args === []) {
58 return $error;
59 }
60
61 return sprintf($error, ...array_map(function ($v) {
62 if (is_array($v)) {
63 return "array";
64 } elseif (is_null($v)) {
65 return "null";
66 } elseif (is_object($v) && !method_exists($v, "__toString")) {
67 return get_class($v);
68 }
69 return $v;
70 }, $args));
71 };
72 }
73}
ilErrorHandling $error
Definition: class.ilias.php:69
getError()
getErrorMessage($value)
withProblemBuilder(callable $builder)
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.