-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhactoryGurl.php
178 lines (122 loc) · 3.75 KB
/
PhactoryGurl.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
177
<?php
require_once "PhactoryGurl/Dataset.php";
trait PhactoryGurl {
protected $factory;
function setUp() {
parent::setUp();
if(true) { //Check if this is a database test case
} else {
//otherwise instantiate our own database test case functionality.
}
}
function getDataSet() {
return $this->factory()->emptyDataset();
}
function build($class, $args = array()) {
return $this->factory()->build($class, $args);
}
function buildOrFind($class, $args= array()) {
return $this->factory()->buildOrFind($class, $args);
}
function create($class, $args = array()) {
return $this->factory()->create($class, $args);
}
function mock($class, $args = array()) {
throw new Exception("Not implemented.");
}
function setFactory($factory) {
$this->factory = $factory;
}
function factory() {
if($this->factory) {
return $this->factory;
} else {
return $this->factory = new PhactoryGurl_Factory;
}
}
}
class PhactoryGurl_Definitions {
static protected $builders = [];
static protected $attributes = [];
static function clear() {
self::$builders = array();
self::$attributes = array();
}
static function register($class_name, $attributes, $builder = null) {
if(is_null($builder)) {
$builder = $attributes;
$attributes = array();
}
self::$attributes[$class_name] = $attributes;
self::$builders[$class_name] = $builder;
}
static function registered($class_name) {
return isset(self::$builders[$class_name]);
}
static function definition($gurl) {
if(!self::registered($gurl)) throw new Exception("$gurl has not been registered to PhactoryGurl.");
return array("builder" => self::$builders[$gurl]) + array_merge(["class" => $gurl], self::$attributes[$gurl] ?: array());
}
static function builder($gurl) {
if(!self::registered($gurl)) throw new Exception("$gurl has not been registered to PhactoryGurl.");
return self::$builders[$gurl];
}
static function tables() {
return array_map(function($i) { return $i["table"]; }, self::$attributes);
}
}
abstract class PhactoryGurl_Adapter {
abstract function save($object);
abstract function create($class, $args);
}
class PhactoryGurl_Default_Adapter extends PhactoryGurl_Adapter {
function save($object) {
$object->save();
return $object;
}
function create($class, $args) {
return new $class($args);
}
}
class PhactoryGurl_Factory {
public static $factory;
static protected $adapter = null;
static function setAdapter($adapter) {
self::$adapter = $adapter;
}
static function adapter() {
if(self::$adapter) {
return self::$adapter;
} else {
return self::$adapter = new PhactoryGurl_Default_Adapter;
}
}
function emptyDataset() {
return new PhactoryGurl_Dataset(PhactoryGurl_Definitions::tables());
}
function getDefinition($gurl) {
return $definition = PhactoryGurl_Definitions::definition($gurl);
}
function create($gurl, $args = array()) {
$definition = $this->getDefinition($gurl);
$class = $definition["class"];
return $this->adapter()->create($class, $this->getArgs($gurl, $args));
}
function build($class, $args = array()) {
$ob = $this->create($class, $args);
return $this->save($ob);
}
function save($object) {
return $this->adapter()->save($object);
}
protected function getArgs($class, $args) {
$argFunc = PhactoryGurl_Definitions::builder($class);
return self::merge($argFunc($this), $args);
}
static function merge($first_args, $second_args) {
return array_merge($first_args ?: array(), $second_args ?: array());
}
static function __callStatic($func, $args) {
return call_user_func_array("self::$func", $args);
}
}