-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSC_BaseObject.php
More file actions
131 lines (118 loc) · 3.17 KB
/
SC_BaseObject.php
File metadata and controls
131 lines (118 loc) · 3.17 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
129
130
131
<?php
/*
Sow Peace License (MIT-Compatible with Attribution Visit)
Copyright (c) 2025 Ruben Schaffer Levine and Luca Lauretta
https://simplonphp.org/Sow-PeaceLicense.txt
*/
/**
* Provides base functionality for all SimplOn classes.
*
* This class offers common utilities like:
* - Retrieving class name information (full name, prefix, words).
* - Introspecting methods and attributes.
* - Automatic getter and setter generation via the magic __call method for both
* instance and static properties.
* - Checking for the existence of attributes and properties.
* - Clearing property values.
* - Generating unique instance identifiers for UI linking.
*
* @version 1b.1.0
* @package SimplOn\Core
* @author Ruben Schaffer
*/
class SC_BaseObject
{
/**
* Returns the object's class
*
* @return string The short class name of the object instance.
*/
public function getClass()
{
$class = explode('\\',get_class($this));
return end($class);
}
/**
* Checks if a an object has a specific method
*
* @return boolean
*/
public function hasAttribute($attribute) {
return property_exists($this,$attribute);
}
/**
* Checks if a an object has a specific property
*
* @return boolean
*/
function hasProperty(string $propName): bool {
// Get the class name if an object is passed
$className = get_class($this);
// Check if the class exists
if (!class_exists($className)) {
return false;
}
try {
$reflection = new ReflectionClass($className);
// Get all static properties
$staticProps = $reflection->getStaticProperties();
// Check if the property exists and is static
return array_key_exists($propName, $staticProps);
} catch (ReflectionException $e) {
return false;
}
}
/**
* Auto Makes the Setters and Getters
*
* @param $name
* @param $arguments
*/
public function __call($name, $arguments) {
//Get and Set
if($this->hasProperty($name)) {
if($arguments) {
get_class($this)::$$name = $arguments[0];
return $this;
} else {
return get_class($this)::$$name;
}
}elseif($this->hasAttribute($name)) {
if($arguments) {
$this->$name = $arguments[0];
return $this;
} else {
return $this->$name;
}
}else{
//Adds the ability to automaticaly print any 'str' method of a SimplOn's object by just calling it changing the 'str' by a 'prt'
$function = substr($name, 0, 1);
$stringAttrib = substr($name, 1);
if($function == 'P'){
echo call_user_func_array( array($this,$stringAttrib),$arguments );
return;
}
}
throw new SC_Exception('The method: '.$name.' is not defined in the object: ' . get_class($this));
}
/**
* Clears the value of a property or attribute
*
* @param string $name
*/
public function clear(string $name) {
$this->$name = null;
}
/**
* Run time id of the object used to creat ids to link things in the interface like Labels and Inputs
*/
public function instanceId() {
return 'objID-'.spl_object_id($this);
}
/**
* Class and instance id
*/
public function ObjectId() {
return $this->getClass() . '-' . $this->instanceId();
}
}