ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DValue.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
25class 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}
__construct(Text\SimpleDocumentMarkdown $description, protected ValueType $type,)
Definition: DValue.php:27
getPrimitiveRepresentation(mixed $data)
Each of the types that can be described has a canonical representation created from primitive PHP typ...
Definition: DValue.php:39
This describes some datastructure in terms of standard data structures such as primitives,...
Definition: Description.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc