ILIAS  release_8 Revision v8.24
ProblemBuilder.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21namespace ILIAS\Refinery;
22
23use Closure;
24use InvalidArgumentException;
25
27{
31 abstract protected function getError();
32
36 final public function withProblemBuilder(callable $builder): self
37 {
38 $clone = clone $this;
39 $clone->error = $builder;
40 return $clone;
41 }
42
48 final public function getErrorMessage($value): string
49 {
50 $error = $this->getError();
51 if (!is_callable($error)) {
52 return $error;
53 }
54 $lng_closure = $this->getLngClosure();
55 return call_user_func($this->error, $lng_closure, $value);
56 }
57
62 final protected function getLngClosure(): Closure
63 {
64 return function () {
65 $args = func_get_args();
66 if (count($args) < 1) {
67 throw new InvalidArgumentException(
68 "Expected an id of a lang var as first parameter"
69 );
70 }
71
72 $error = $this->lng->txt($args[0]);
73 if (count($args) > 1) {
74 $args[0] = $error;
75 for ($i = 0, $numArgs = count($args); $i < $numArgs; $i++) {
76 $v = $args[$i];
77 if (is_array($v) || is_null($v) || (is_object($v) && !method_exists($v, "__toString"))) {
78 if (is_array($v)) {
79 $args[$i] = "array";
80 } elseif (is_null($v)) {
81 $args[$i] = "null";
82 } else {
83 $args[$i] = get_class($v);
84 }
85 }
86 }
87 $error = sprintf(...$args);
88 }
89
90 return $error;
91 };
92 }
93}
ilErrorHandling $error
Definition: class.ilias.php:55
getError()
error(string $a_errmsg)
$i
Definition: metadata.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21
getErrorMessage($value)
Get the problem message.
withProblemBuilder(callable $builder)
@inheritDoc
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.