ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DObject.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
25class DObject extends Description
26{
27 use ErrorHandling;
28
29 protected array $fields;
30
31 public function __construct(
32 Text\SimpleDocumentMarkdown $description,
33 Field ...$fields
34 ) {
35 parent::__construct($description);
36 $this->fields = $fields;
37 }
38
39 public function getFields(): \Generator
40 {
41 foreach ($this->fields as $field) {
42 yield $field;
43 }
44 }
45
46 public function getPrimitiveRepresentation(mixed $data): mixed
47 {
48 if (!is_object($data)) {
49 return fn() => yield "Expected an object.";
50 }
51
52 $repr = new \StdClass();
53 $errors = [];
54
55 foreach ($this->fields as $field) {
56 $name = $field->getName();
57 $found = false;
58 $value = null;
59 foreach ($this->possibleMethodNames($name) as $method) {
60 if (method_exists($data, $method)) {
61 $found = true;
62 $value = $data->$method();
63 break;
64 }
65 }
66 if (!$found) {
67 foreach ($this->possiblePropertyNames($name) as $property) {
68 if (property_exists($data, $property)) {
69 $found = true;
70 $value = $data->$property;
71 break;
72 }
73 }
74 }
75 if (!$found) {
76 $errors[] = fn() => yield "Object does not have property \"$name\".";
77 continue;
78 }
79
80 $value = $field->getType()->getPrimitiveRepresentation($value);
81 if ($value instanceof \Closure) {
82 $errors[] = $value;
83 } else {
84 $repr->$name = $value;
85 }
86
87 }
88
89 if ($errors) {
90 die("foo");
91 return $this->mergeErrors($errors);
92 }
93
94 return $repr;
95 }
96
97 protected function possibleMethodNames(string $name): \Generator
98 {
99 $camel_cased = $this->camelCased($name);
100 $snake_cased = $this->snakeCased($name);
101
102 yield "get" . ucfirst($name);
103 yield "get_" . $name;
104 yield "get" . $camel_cased;
105 yield "get_" . $snake_cased;
106 yield "is" . ucfirst($name);
107 yield "is_" . $name;
108 yield "is" . $camel_cased;
109 yield "is_" . $snake_cased;
110 }
111
112 protected function possiblePropertyNames(string $name): \Generator
113 {
114 yield $name;
115 yield ucfirst($name);
116 yield $this->camelCased($name);
117 yield $this->snakeCased($name);
118 }
119
120
121 protected function camelCased(string $name): string
122 {
123 return preg_replace_callback("/_(\w)/", fn($v) => strtoupper($v[1]), $name);
124 }
125
126 protected function snakeCased(string $name): string
127 {
128 return preg_replace_callback("/[A-Z]/", fn($v) => "_" . strtolower($v[0]), $name);
129 }
130}
possiblePropertyNames(string $name)
Definition: DObject.php:112
possibleMethodNames(string $name)
Definition: DObject.php:97
__construct(Text\SimpleDocumentMarkdown $description, Field ... $fields)
Definition: DObject.php:31
getPrimitiveRepresentation(mixed $data)
Each of the types that can be described has a canonical representation created from primitive PHP typ...
Definition: DObject.php:46
This describes some datastructure in terms of standard data structures such as primitives,...
Definition: Description.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc