ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DValue.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Data\Description;
22 
23 use ILIAS\Data\Text;
24 
25 class DValue extends Description
26 {
27  public function __construct(
28  Text\SimpleDocumentMarkdown $description,
29  protected ValueType $type,
30  ) {
31  parent::__construct($description);
32  }
33 
34  public function getType(): ValueType
35  {
36  return $this->type;
37  }
38 
39  public function getPrimitiveRepresentation(mixed $data): mixed
40  {
41  switch ($this->type) {
42  case ValueType::INT:
43  if (!is_int($data)) {
44  return fn() => yield "Expected an integer.";
45  }
46  return $data;
47 
48  case ValueType::FLOAT:
49  if (!is_float($data)) {
50  return fn() => yield "Expected a float.";
51  }
52  return $data;
53 
54  case ValueType::STRING:
55  if (!is_string($data)) {
56  return fn() => yield "Expected a string.";
57  }
58  return $data;
59 
60  case ValueType::DATETIME:
61  if (!$data instanceof \DateTimeImmutable) {
62  return fn() => yield "Expected a \\DateTimeImmutable.";
63  }
64  return $data;
65 
66  case ValueType::BOOL:
67  if (!is_bool($data)) {
68  return fn() => yield "Expected a bool.";
69  }
70  return $data;
71 
72  case ValueType::NULL:
73  if (!is_null($data)) {
74  return fn() => yield "Expected null.";
75  }
76  return $data;
77 
78  default:
79  throw new \LogicException("Unmatch type.");
80  }
81  }
82 }
This describes some datastructure in terms of standard data structures such as primitives, lists, maps and objects and helpful (hopefully...) human readable texts.
Definition: Description.php:32
getPrimitiveRepresentation(mixed $data)
Definition: DValue.php:39
__construct(Container $dic, ilPlugin $plugin)
__construct(Text\SimpleDocumentMarkdown $description, protected ValueType $type,)
Definition: DValue.php:27