ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
IsArrayOf.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2018 Fabian Schmid <fs@studer-raimann.ch> Extended GPL, see docs/LICENSE */
3 
5 
7 use ILIAS\Data;
8 
16 class IsArrayOf extends Custom implements Constraint
17 {
18 
25  public function __construct(Data\Factory $data_factory, Constraint $on_element, \ilLanguage $lng)
26  {
27  parent::__construct(
28  function ($value) use ($on_element) {
29  if (!is_array($value)) {
30  return false;
31  }
32  foreach ($value as $item) {
33  if (!$on_element->accepts($item)) {
34  return false;
35  }
36  }
37 
38  return true;
39  },
40  function ($txt, $value) use ($on_element) {
41  if (!is_array($value)) {
42  return $txt("not_an_array", gettype($value));
43  }
44  $sub_problems = [];
45  foreach ($value as $item) {
46  $sub_problem = $on_element->problemWith($item);
47  if ($sub_problem !== null) {
48  $sub_problems[] = $sub_problem;
49  }
50  }
51  return $txt("not_an_array_of", implode(" ", $sub_problems));
52  },
54  $lng
55  );
56  }
57 }
A constraint encodes some resrtictions on values.
Definition: Constraint.php:14
problemWith($value)
Tells what the problem with the provided value is.
accepts($value)
Tells if the provided value complies.
Builds data types.
Definition: Factory.php:14
$txt
Definition: error.php:11
__construct(Data\Factory $data_factory, Constraint $on_element, \ilLanguage $lng)
IsArrayOf constructor.
Definition: IsArrayOf.php:25
language handling