ILIAS  release_7 Revision v7.30-3-g800a261c036
ProblemBuilder.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
3/* Copyright (c) 2020 Nils Haagen <nils.haagen@concepts-and-training.de>, Extended GPL, see docs/LICENSE */
4
5namespace ILIAS\Refinery;
6
8
10{
14 abstract protected function getError();
15
19 final public function withProblemBuilder(callable $builder)
20 {
21 $clone = clone $this;
22 $clone->error = $builder;
23 return $clone;
24 }
25
31 final public function getErrorMessage($value)
32 {
33 $error = $this->getError();
34 if (!is_callable($error)) {
35 return (string) $error;
36 }
37 $lng_closure = $this->getLngClosure();
38 return call_user_func($this->error, $lng_closure, $value);
39 }
40
47 final protected function getLngClosure()
48 {
49 return function () {
50 $args = func_get_args();
51 if (count($args) < 1) {
52 throw new \InvalidArgumentException(
53 "Expected an id of a lang var as first parameter"
54 );
55 }
56 $error = $this->lng->txt($args[0]);
57 if (count($args) > 1) {
58 $args[0] = $error;
59 for ($i = 0; $i < count($args); $i++) {
60 $v = $args[$i];
61 if ((is_array($v) || is_object($v) || is_null($v))
62 && !method_exists($v, "__toString")) {
63 if (is_array($v)) {
64 $args[$i] = "array";
65 } elseif (is_null($v)) {
66 $args[$i] = "null";
67 } else {
68 $args[$i] = get_class($v);
69 }
70 }
71 }
72 $error = call_user_func_array("sprintf", $args);
73 }
74 return $error;
75 };
76 }
77}
An exception for terminatinating execution or to throw for unit testing.
getError()
returns error @access public
error($a_errmsg)
set error message @access public
$i
Definition: metadata.php:24
getErrorMessage($value)
Get the problem message.
withProblemBuilder(callable $builder)
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
$builder
Definition: parser.php:5