ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
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)
 Each of the types that can be described has a canonical representation created from primitive PHP types. More...
 
- 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.

34 {
35 parent::__construct($description);
36 $this->fields = $fields;
37 }
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc

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

+ Here is the call graph for this function:

Member Function Documentation

◆ camelCased()

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

Definition at line 120 of file DObject.php.

120 : string
121 {
122 return preg_replace_callback("/_(\w)/", fn($v) => strtoupper($v[1]), $name);
123 }

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

+ 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)

Each of the types that can be described has a canonical representation created from primitive PHP types.

This attempts to transform the provided data into such a representation.

If this returns a \Closure, the data cannot be transformed into such a representation and the \Closure will produce a generator that provides a list of defects where $data does not match the description. If this does return something else it will be plain old php data according to the description.

Reimplemented from ILIAS\Data\Description\Description.

Definition at line 46 of file DObject.php.

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 return $this->mergeErrors($errors);
91 }
92
93 return $repr;
94 }
possiblePropertyNames(string $name)
Definition: DObject.php:111
possibleMethodNames(string $name)
Definition: DObject.php:96

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

+ Here is the call graph for this function:

◆ possibleMethodNames()

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

Definition at line 96 of file DObject.php.

96 : \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 }

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

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

+ 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 111 of file DObject.php.

111 : \Generator
112 {
113 yield $name;
114 yield ucfirst($name);
115 yield $this->camelCased($name);
116 yield $this->snakeCased($name);
117 }

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

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

+ 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 125 of file DObject.php.

125 : string
126 {
127 return preg_replace_callback("/[A-Z]/", fn($v) => "_" . strtolower($v[0]), $name);
128 }

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

+ 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: