ILIAS  trunk Revision v11.0_alpha-2658-ge2404539063
ILIAS\Data\Description\DObject Class Reference
+ Inheritance diagram for ILIAS\Data\Description\DObject:
+ Collaboration diagram for ILIAS\Data\Description\DObject:

Public Member Functions

 __construct (Text\SimpleDocumentMarkdown $description, Field ... $fields)
 
 getFields ()
 
 getPrimitiveRepresentation (mixed $data)
 
- Public Member Functions inherited from ILIAS\Data\Description\Description
 __construct (protected ?Text\SimpleDocumentMarkdown $description,)
 
 getDescription ()
 
 getPrimitiveRepresentation (mixed $data)
 Each of the types that can be described has a canonical representation created from primitive PHP types. More...
 
 matches (mixed $data)
 

Protected Member Functions

 possibleMethodNames (string $name)
 
 possiblePropertyNames (string $name)
 
 camelCased (string $name)
 
 snakeCased (string $name)
 

Protected Attributes

array $fields
 

Detailed Description

Definition at line 25 of file DObject.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Data\Description\DObject::__construct ( Text\SimpleDocumentMarkdown  $description,
Field ...  $fields 
)

Definition at line 31 of file DObject.php.

References ILIAS\Data\Description\DObject\$fields, and ILIAS\GlobalScreen\Provider\__construct().

34  {
35  parent::__construct($description);
36  $this->fields = $fields;
37  }
__construct(Container $dic, ilPlugin $plugin)
+ Here is the call graph for this function:

Member Function Documentation

◆ camelCased()

ILIAS\Data\Description\DObject::camelCased ( string  $name)
protected

Definition at line 121 of file DObject.php.

Referenced by ILIAS\Data\Description\DObject\possibleMethodNames(), and ILIAS\Data\Description\DObject\possiblePropertyNames().

121  : string
122  {
123  return preg_replace_callback("/_(\w)/", fn($v) => strtoupper($v[1]), $name);
124  }
+ Here is the caller graph for this function:

◆ getFields()

ILIAS\Data\Description\DObject::getFields ( )

Definition at line 39 of file DObject.php.

39  : \Generator
40  {
41  foreach ($this->fields as $field) {
42  yield $field;
43  }
44  }

◆ getPrimitiveRepresentation()

ILIAS\Data\Description\DObject::getPrimitiveRepresentation ( mixed  $data)

Definition at line 46 of file DObject.php.

References null, ILIAS\Data\Description\DObject\possibleMethodNames(), and ILIAS\Data\Description\DObject\possiblePropertyNames().

46  : 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  }
possiblePropertyNames(string $name)
Definition: DObject.php:112
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
possibleMethodNames(string $name)
Definition: DObject.php:97
+ Here is the call graph for this function:

◆ possibleMethodNames()

ILIAS\Data\Description\DObject::possibleMethodNames ( string  $name)
protected

Definition at line 97 of file DObject.php.

References ILIAS\Data\Description\DObject\camelCased(), and ILIAS\Data\Description\DObject\snakeCased().

Referenced by ILIAS\Data\Description\DObject\getPrimitiveRepresentation().

97  : \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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ possiblePropertyNames()

ILIAS\Data\Description\DObject::possiblePropertyNames ( string  $name)
protected

Definition at line 112 of file DObject.php.

References ILIAS\Data\Description\DObject\camelCased(), and ILIAS\Data\Description\DObject\snakeCased().

Referenced by ILIAS\Data\Description\DObject\getPrimitiveRepresentation().

112  : \Generator
113  {
114  yield $name;
115  yield ucfirst($name);
116  yield $this->camelCased($name);
117  yield $this->snakeCased($name);
118  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ snakeCased()

ILIAS\Data\Description\DObject::snakeCased ( string  $name)
protected

Definition at line 126 of file DObject.php.

Referenced by ILIAS\Data\Description\DObject\possibleMethodNames(), and ILIAS\Data\Description\DObject\possiblePropertyNames().

126  : string
127  {
128  return preg_replace_callback("/[A-Z]/", fn($v) => "_" . strtolower($v[0]), $name);
129  }
+ Here is the caller graph for this function:

Field Documentation

◆ $fields

array ILIAS\Data\Description\DObject::$fields
protected

Definition at line 29 of file DObject.php.

Referenced by ILIAS\Data\Description\DObject\__construct().


The documentation for this class was generated from the following file: