-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.php
109 lines (85 loc) · 3.49 KB
/
base.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
<?php
namespace Framework {
use Framework\Inspector as Inspector;
use Framework\ArrayMethods as ArrayMethods;
use Framework\StringMethods as StringMethods;
use Framework\Core\Exception as Exception;
/**
* All our framework classes inherit from one base class
*/
class Base {
private $_inspector;
public function __construct($options = array()) {
$this->_inspector = new Inspector($this);
if (is_array($options) || is_object($options)) {
foreach ($options as $key => $value) {
$key = ucfirst($key);
$method = "set{$key}";
$this->$method($value);
}
}
}
/**
* Checking to see that the inspector is set, handling the getProperty() methods, and handling the setProperty() methods.
* @param type $name
* @param type $arguments
* @return \Framework\Base
* @throws Exception
* @throws type
*/
public function __call($name, $arguments) {
if (empty($this->_inspector)) {
throw new Exception("Call parent::__construct!");
}
$getMatches = StringMethods::match($name, "^get([a-zA-Z0-9_]+)$");
if (sizeof($getMatches) > 0) {
$normalized = lcfirst($getMatches[0]);
$property = "_{$normalized}";
if (property_exists($this, $property)) {
$meta = $this->_inspector->getPropertyMeta($property);
if (empty($meta["@readwrite"]) && empty($meta["@read"])) {
throw $this->_getExceptionForWriteonly($normalized);
}
if (isset($this->$property)) {
return $this->$property;
}
return null;
}
}
$setMatches = StringMethods::match($name, "^set([a-zA-Z0-9_]+)$");
if (sizeof($setMatches) > 0) {
$normalized = lcfirst($setMatches[0]);
$property = "_{$normalized}";
if (property_exists($this, $property)) {
$meta = $this->_inspector->getPropertyMeta($property);
if (empty($meta["@readwrite"]) && empty($meta["@write"])) {
throw $this->_getExceptionForReadonly($normalized);
}
$this->$property = $arguments[0];
return $this;
}
}
throw $this->_getExceptionForImplementation($name);
}
public function __get($name) {
$function = "get" . ucfirst($name);
return $this->$function();
}
public function __set($name, $value) {
$function = "set" . ucfirst($name);
return $this->$function($value);
}
protected function _getExceptionForReadonly($property) {
return new Exception\ReadOnly("{$property} is read-only");
}
protected function _getExceptionForWriteonly($property) {
return new Exception\WriteOnly("{$property} is write-only");
}
protected function _getExceptionForProperty() {
return new Exception\Property("Invalid property");
}
protected function _getExceptionForImplementation($method) {
return new Exception\Argument("{$method} method not implemented");
}
}
}