ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilTestLegacyFormsHelper.php
Go to the documentation of this file.
1 <?php
2 
20 
24 
29 {
30  protected Refinery $refinery;
31 
32  public function __construct(
33 
34  ) {
35  global $DIC;
36  $this->refinery = $DIC['refinery'];
37  }
38 
46  public function checkPointsInput($data, bool $required, string $key = 'points'): string|array
47  {
48  if (!is_array($data) || !$this->inArray($data, $key)) {
49  return $required ? 'msg_input_is_required' : [];
50  }
51 
52  try {
53  $points = $this->refinery->to()->listOf($this->refinery->kindlyTo()->float())->transform($data[$key]);
55  return 'form_msg_numeric_value_required';
56  }
57 
58  if (count($points) === 0) {
59  return $required ? 'msg_input_is_required' : [];
60  }
61 
62  return $points;
63  }
64 
71  public function checkPointsInputEnoughPositive($data, bool $required, string $key = 'points'): string|array
72  {
73  $points = $this->checkPointsInput($data, $required, $key);
74  if (!is_array($points)) {
75  return $points;
76  }
77 
78  return max($points) <= 0 ? 'enter_enough_positive_points' : $points;
79  }
80 
81  public function transformArray($data, string $key, Transformation $transformation): array
82  {
83  if (!$this->inArray($data, $key)) {
84  return [];
85  }
86 
87  return $this->refinery->byTrying([
88  $this->refinery->kindlyTo()->listOf($transformation),
89  $this->refinery->always([])
90  ])->transform($data[$key]);
91  }
92 
96  public function transformPoints($data, string $key = 'points'): array
97  {
98  if (!$this->inArray($data, $key)) {
99  return [];
100  }
101 
102  return array_map(
103  fn($v): ?float => $this->refinery->byTrying([$this->refinery->kindlyTo()->float(), $this->refinery->always(null)])->transform($v),
104  $data[$key]
105  );
106  }
107 
111  public function inArray($array, $key): bool
112  {
113  return is_array($array)
114  && array_key_exists($key, $array)
115  && $array[$key] !== null
116  && $array[$key] !== false
117  && $array[$key] !== ''
118  && $array[$key] !== []
119  && $array[$key] !== 0
120  && $array[$key] !== 0.0;
121  }
122 }
checkPointsInputEnoughPositive($data, bool $required, string $key='points')
Extends the checkPointsInput function by checking if at least one point is greater than 0...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26
transformArray($data, string $key, Transformation $transformation)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
checkPointsInput($data, bool $required, string $key='points')
Checks if the provides points of an input are valid.
inArray($array, $key)
Returns true if the given key is set in the array and the value is not empty.
A transformation is a function from one datatype to another.