1: <?php
2: namespace Izberg;
3: abstract class Resource
4: {
5: protected static $Izberg = null;
6: protected $_name;
7:
8: 9: 10:
11: public function __toString()
12: {
13: return json_encode($this);
14: }
15:
16: 17: 18: 19: 20:
21: private function setName($name = null)
22: {
23: 24: 25:
26: if ($name === null)
27: {
28: $name = str_replace("Izberg\Resource\\", "", get_class($this));
29: $final_str = self::$Izberg->getHelper()->uncamelize($name);
30: }
31: else
32: $final_str = $name;
33: $this->_name = $final_str;
34: }
35:
36: 37: 38: 39:
40: public function parseUri($uri)
41: {
42: if (strncmp("http", $uri, 4) == 0)
43: $uri = substr($uri, strlen(self::$Izberg->getApiUrl()));
44: $uri = explode('/', $uri);
45: $uri = $uri[0];
46: $tabname = explode('_', $uri);
47: foreach ($tabname as &$value)
48: $value = ucfirst($value);
49: $uri = implode('', $tabname);
50: if ($uri == "Return") $uri = "ReturnRequest";
51: return "Izberg\Resource\\".$uri;
52: }
53:
54: 55: 56: 57: 58:
59: public function getName()
60: {
61: return ($this->_name);
62: }
63:
64: 65: 66: 67:
68: public function getPrefix()
69: {
70: return "";
71: }
72:
73: public function __construct()
74: {
75: if (self::$Izberg === null)
76: throw new Exception\GenericException("Can't create instance of ".get_class().", no valid Izberg singleton");
77: if (!$this->getName())
78: $this->setName();
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public static function setIzberg($instance)
89: {
90: self::$Izberg = $instance;
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: public function log($message, $level="error", $path = null)
101: {
102:
103: if (false === Izberg::LOGS)
104: return ;
105: if (false === is_dir($path))
106: $path = null;
107: else if ($path && substr($path, -1) != '/')
108: $path .= '/';
109: file_put_contents($path."log-".$level."-".date("m-d").".txt", date("H:i:s | ")." : ".$message."\n", FILE_APPEND);
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function hydrate($obj)
119: {
120: if (!$obj)
121: return ;
122: if (isset($obj->objects))
123: $this->hydrate($obj->objects[0]);
124: else
125: foreach ($obj as $key=>$value)
126: {
127: if (is_object($value))
128: {
129: $classname = $this->parseUri($key);
130: try {
131: if (!class_exists($classname) && isset($value->resource_uri)) {
132: $classname = $this->parseUri($value->resource_uri);
133: }
134: } catch (\LogicException $e) {
135: $this->$key = $value;
136: continue;
137: }
138:
139: try {
140: if (class_exists($classname)) {
141: $new_obj = new $classname();
142: $new_obj->hydrate($value);
143: $this->$key = $new_obj;
144: continue;
145: } else {
146: $this->$key = $value;
147: continue;
148: }
149: } catch (\LogicException $e) {
150: $this->$key = $value;
151: continue;
152: }
153: }
154: else if (is_array($value) && isset($value[0]->resource_uri))
155: {
156: $classname = $this->parseUri($value[0]->resource_uri);
157: if (!class_exists($classname)) {
158: $classname = null;
159: }
160: $list = array();
161: foreach ($value as $val) {
162: if (!is_null($classname)) {
163: $new_obj = new $classname();
164: $new_obj->hydrate($val);
165: $list[] = $new_obj;
166: } else {
167: $list[] = $val;
168: }
169: }
170: $this->$key = $list;
171: }
172: else
173: $this->$key = $value;
174: }
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function delete($params = null)
184: { if (!$this->id)
185: throw new Exception\GenericException(__METHOD__." needs a valid ID");
186: $name = $this->getName();
187: return self::$Izberg->Call( $name . "/" . $this->id . "/", 'DELETE', $params, 'Content-Type: application/json');
188: }
189:
190: 191: 192: 193: 194: 195:
196: public function save()
197: {
198: if (!$this->resource_uri)
199: return ;
200: $data = $this;
201: $data = json_encode($data);
202: $data = (array)json_decode($data, true);
203: if (!$this->id)
204: {
205: $response = self::$Izberg->Call($this->getName()."/", 'POST', $data);
206: $this->hydrate($response);
207: return ;
208: }
209: $url_params = parse_url($data["resource_uri"]);
210: $addr = str_replace("/v1", "",$url_params["path"]);
211: foreach ($data as $key=>$value)
212: if (!$value || is_array($value))
213: unset($data[$key]);
214:
215: return self::$Izberg->Call($addr, 'PUT', $data, "Content-Type: application/json");
216: }
217: }
218: