This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PClass.php
147 lines (127 loc) · 3.64 KB
/
PClass.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Poundation;
require_once ('PObject.php');
require_once ('PString.php');
use Poundation\PObject;
/**
* @abstract PClass is a helper class to handle classes.
* @author danielkbx
*
*/
class PClass extends PObject {
static private $_staticMap = array();
private $_name;
private $_classHirarchy = array();
private $_classInterfaces = array();
/**
* Returns a class object matching the given object.
* @param $object
* @throws \Exception
* @return \Poundation\PClass
*/
static public function classFromObject($object) {
if ($object && is_object($object)) {
$objectsClassName = get_class($object);
return self::classFromString($objectsClassName);
} else {
throw new \Exception(@"Cannot create class class from non-object value",101,null);
}
}
static public function classFromString($name) {
if ($name && (is_string($name) || $name instanceof PString)) {
$existingClass = (isset(PClass::$_staticMap[$name])) ? PClass::$_staticMap[$name] : null;
if (!$existingClass) {
$existingClass = new PClass($name);
PClass::$_staticMap[$name] = $existingClass;
}
return $existingClass;
} else {
throw new \Exception(@"Cannot create class class from non-string value",101,null);
}
}
private function __construct($name) {
if ($name && (is_string($name) || $name instanceof PString)) {
$this->_name = $name;
$parentClassname = get_parent_class($this->_name);
while($parentClassname) {
$this->_classHirarchy[] = $parentClassname;
$parentClassname = get_parent_class($parentClassname);
}
$interfaces = class_implements($this->_name);
foreach ($interfaces as $interface) {
$this->_classInterfaces[] = $interface;
}
return $this;
} else {
return false;
}
}
/**
* Returns the name of the class.
* @return PString
*/
function name() {
return PString::createFromString($this->_name);
}
/**
* Determines if the class inherits from the given class or is the given class.
* @param PClass|string $class
* @throws \Exception
* @return boolean
*/
function isKindOfClass($class) {
$classname = false;
if (is_string($class)) {
$classname = $class;
} else if ($class instanceof PString) {
$classname = (string)$class;
} else if ($class instanceof PClass) {
$classname = (string)$class->name();
} else if (is_object($class)) {
$classname = get_class($class);
}
if ($classname !== false) {
if ($this->_name == $classname) {
return true;
} else {
return (array_search($classname,$this->_classHirarchy) !== false);
}
} else {
throw new \Exception('Cannot check class with target value ' . $class,104,null);
}
}
/**
* Determines if a class implements the given interface.
* @param string $interface
* @return boolean
*/
function implementsInterface($interface) {
if ($interface) {
return (array_search($interface, $this->_classInterfaces) !== false);
}
return false;
}
/**
* Invokes the method on the class (therefore it is a class method call) and returns the result.
* @param string $methodName
*/
function invokeMethod($methodName) {
return $this->invokeMethodWithParameters($methodName, array());
}
/**
* Invokes the method on the class (therefore it is a class method call), passes the arguments and returns the result.
* @param string $methodName
* @param array $params
* @return mixed
*/
function invokeMethodWithParameters($methodName,$params) {
if (is_string($methodName)) {
return call_user_func_array(array($this->_name,$methodName), $params);
}
}
function createInstance() {
$instance = new $this->_name;
return $instance;
}
}
?>