ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ProblemBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Refinery;
22 
23 use Closure;
25 
26 trait ProblemBuilder
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 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21
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.
ilErrorHandling $error
Definition: class.ilias.php:55
$i
Definition: metadata.php:41