-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObject.php
176 lines (139 loc) · 4.1 KB
/
Object.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* @author Ivan Yonkov <[email protected]>
*/
class Object
{
/**
* @var string
*/
private $name;
/**
* @var boolean
*/
private $isAbstract;
/**
* @var string?
*/
private $base;
/**
* Collection of properties
*
* @var Property[]
*/
private $properties;
public function __construct($name, $isAbstract, $base = null)
{
$this->name = $name;
$this->isAbstract = $isAbstract;
$this->base = $base;
}
/**
* @return string?
*/
public function getBase()
{
return $this->base;
}
/**
* @return boolean
*/
public function isAbstract()
{
return $this->isAbstract;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return \Property[]
*/
public function getProperties()
{
return $this->properties;
}
/**
* @param Property $property
* @return void
*/
public function addProperty(Property $property)
{
$this->properties[] = $property;
}
public function __toString()
{
return $this->generateAutomaticInvokedFunction();
}
private function generateAutomaticInvokedFunction()
{
$avf = "";
$avf .= "var " . ucfirst($this->getName()) . " = (function () { \n\t";
$avf .= $this->generateConstructor();
$avf .= $this->generateInheritance();
foreach ($this->getProperties() as $property) {
$avf .= ucfirst($this->getName()) . '.prototype.' . $property->generateSetter();
$avf .= "\n\n\t";
$avf .= ucfirst($this->getName()) . '.prototype.' . $property->generateGetter();
$avf .= "\n\n\t";
}
$avf .= $this->generateToString();
$avf .= "\n\n\treturn " . ucfirst($this->getName()) . ";";
$avf .= "\n})();";
return $avf;
}
private function generateConstructor()
{
$constructor = "";
$constructor .= "function " . ucfirst($this->getName());
$constructor .= "(";
foreach ($this->getProperties() as $property) {
$constructor .= $property->getName() . ",";
}
$constructor = rtrim($constructor, ",");
$constructor .= ") { \n\t\t";
if ($this->isAbstract()) {
$constructor .= "if (this.constructor === " . ucfirst($this->getName()) . ") {\n\t\t\t";
$constructor .= "throw new Error('Cannot instantiate abstract class');\n\t\t";
$constructor .= "}\n\n\t\t";
} else if ($this->getBase() && strlen($this->getBase()) > 0) {
$constructor .= ucfirst($this->getBase()) . ".call(this,";
foreach ($this->getProperties() as $property) {
$constructor .= $property->getName() . ",";
}
$constructor = rtrim($constructor, ",");
$constructor .= ");\n\t\t";
}
foreach ($this->getProperties() as $property) {
$constructor .= "this.set" . ucfirst($property->getName()) . "(" . $property->getName() . ");\n\t\t";
}
$constructor = rtrim($constructor, "\t");
$constructor .= "\t}\n\n";
return $constructor;
}
private function generateToString()
{
$toString = "";
$toString .= ucfirst($this->getName()) . ".prototype.toString = function() { \n\t\t";
$toString .= "return ";
foreach ($this->getProperties() as $property) {
$toString .= "'" . ucfirst($property->getName()) . ": ' + this.get" . ucfirst($property->getName()) . "() + ";
}
$toString = rtrim($toString, "+ ");
$toString .= ";\n\t};";
return $toString;
}
private function generateInheritance()
{
$inheritance = "";
if ($this->getBase() && strlen($this->getBase()) > 0) {
$inheritance .= "\t";
$inheritance .= ucfirst($this->getName()) . ".inherits(" . ucfirst($this->getBase()) . ");\n\n";
}
$inheritance .= "\t";
return $inheritance;
}
}