ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
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 return $this->mergeErrors($errors);
91 }
92
93 return $repr;
94 }
95
96 protected function possibleMethodNames(string $name): \Generator
97 {
98 $camel_cased = $this->camelCased($name);
99 $snake_cased = $this->snakeCased($name);
100
101 yield "get" . ucfirst($name);
102 yield "get_" . $name;
103 yield "get" . $camel_cased;
104 yield "get_" . $snake_cased;
105 yield "is" . ucfirst($name);
106 yield "is_" . $name;
107 yield "is" . $camel_cased;
108 yield "is_" . $snake_cased;
109 }
110
111 protected function possiblePropertyNames(string $name): \Generator
112 {
113 yield $name;
114 yield ucfirst($name);
115 yield $this->camelCased($name);
116 yield $this->snakeCased($name);
117 }
118
119
120 protected function camelCased(string $name): string
121 {
122 return preg_replace_callback("/_(\w)/", fn($v) => strtoupper($v[1]), $name);
123 }
124
125 protected function snakeCased(string $name): string
126 {
127 return preg_replace_callback("/[A-Z]/", fn($v) => "_" . strtolower($v[0]), $name);
128 }
129}
possiblePropertyNames(string $name)
Definition: DObject.php:111
possibleMethodNames(string $name)
Definition: DObject.php:96
__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