-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicoDataLabel.php
More file actions
128 lines (116 loc) · 4.1 KB
/
PicoDataLabel.php
File metadata and controls
128 lines (116 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace MagicObject\DataLabel;
use MagicObject\SetterGetter;
use MagicObject\Util\ClassUtil\PicoAnnotationParser;
use MagicObject\Util\PicoStringUtil;
use stdClass;
/**
* Class representing a data label with annotations for properties and tables.
*
* This class uses annotations to define properties and their metadata.
*
* @author Kamshory
* @package MagicObject\DataLabel
* @link https://github.com/Planetbiru/MagicObject
*/
class PicoDataLabel extends SetterGetter
{
const ANNOTATION_PROPERTIES = "Properties"; // Annotation key for properties
const ANNOTATION_TABLE = "Table"; // Annotation key for table name
const KEY_NAME = "name"; // Key for the name
const ANNOTATION_VAR = "var"; // Annotation key for variable
/**
* Parameters defined in the class annotations.
*
* @var array
*/
private $classParams = array();
/**
* Name of the class.
*
* @var string
*/
private $className = "";
/**
* Constructor for the PicoDataLabel class.
*
* Initializes the class and loads data if provided.
*
* @param self|array|object $data Data to initialize the object with.
*/
public function __construct($data)
{
parent::__construct();
$this->className = get_class($this);
$jsonAnnot = new PicoAnnotationParser($this->className);
$params = $jsonAnnot->getParameters();
foreach ($params as $paramName => $paramValue) {
$vals = $jsonAnnot->parseKeyValue($paramValue);
$this->classParams[$paramName] = $vals;
}
if ($data != null) {
$this->loadData($data);
}
}
/**
* Loads data into the object from a provided array, object, or self instance.
*
* @param mixed $data The data to load into the object.
* @return self Returns the current instance for method chaining.
*/
public function loadData($data)
{
if ($data != null) {
if ($data instanceof self) {
$values = $data->value();
foreach ($values as $key => $value) {
$key2 = PicoStringUtil::camelize($key);
$this->set($key2, $value);
}
} elseif (is_array($data) || is_object($data)) {
foreach ($data as $key => $value) {
$key2 = PicoStringUtil::camelize($key);
$this->set($key2, $value);
}
}
}
return $this;
}
/**
* Retrieves object information by parsing class and property annotations.
*
* @return stdClass An object containing the table name, columns, default values, and not-null columns.
*/
public function getObjectInfo()
{
$reflexClass = new PicoAnnotationParser($this->className);
$table = $reflexClass->getParameter(self::ANNOTATION_TABLE);
$values = $reflexClass->parseKeyValue($table);
$picoTableName = $values[self::KEY_NAME];
$properties = array();
$notNullColumns = array();
$props = $reflexClass->getProperties();
$defaultValue = array();
// Iterate through each property of the class
foreach ($props as $prop) {
$reflexProp = new PicoAnnotationParser($this->className, $prop->name, PicoAnnotationParser::PROPERTY);
$parameters = $reflexProp->getParameters();
// Get column names for each parameter
foreach ($parameters as $param => $val) {
if (strcasecmp($param, self::ANNOTATION_PROPERTIES) == 0) {
$values = $reflexProp->parseKeyValue($val);
if (!empty($values)) {
$properties[$prop->name] = $values;
}
}
}
}
// Aggregate the information
$info = new stdClass;
$info->tableName = $picoTableName;
$info->columns = $properties;
$info->defaultValue = $defaultValue;
$info->notNullColumns = $notNullColumns;
return $info;
}
}