ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DObject.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 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 }
getPrimitiveRepresentation(mixed $data)
Definition: DObject.php:46
possiblePropertyNames(string $name)
Definition: DObject.php:112
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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
__construct(Text\SimpleDocumentMarkdown $description, Field ... $fields)
Definition: DObject.php:31
possibleMethodNames(string $name)
Definition: DObject.php:97
__construct(Container $dic, ilPlugin $plugin)