ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
5 namespace ILIAS\Refinery;
6 
8 
9 trait ProblemBuilder
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 }
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
withProblemBuilder(callable $builder)
getErrorMessage($value)
Get the problem message.
$builder
Definition: parser.php:5
$i
Definition: metadata.php:24