[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace RESTful; 4 5 abstract class Resource 6 { 7 protected $_collection_uris, 8 $_member_uris; 9 10 public static function getClient() 11 { 12 $class = get_called_class(); 13 14 return $class::$_client; 15 } 16 17 public static function getRegistry() 18 { 19 $class = get_called_class(); 20 21 return $class::$_registry; 22 } 23 24 public static function getURISpec() 25 { 26 $class = get_called_class(); 27 28 return $class::$_uri_spec; 29 } 30 31 public function __construct($fields = null) 32 { 33 if ($fields == null) { 34 $fields = array(); 35 } 36 $this->_objectify($fields); 37 } 38 39 public function __get($name) 40 { 41 // collection uri 42 if (array_key_exists($name, $this->_collection_uris)) { 43 $result = $this->_collection_uris[$name]; 44 $this->$name = new Collection($result['class'], $result['uri']); 45 46 return $this->$name; 47 } // member uri 48 else if (array_key_exists($name, $this->_member_uris)) { 49 $result = $this->$_collection_uris[$name]; 50 $response = self::getClient() . get($result['uri']); 51 $class = $result['class']; 52 $this->$name = new $class($response->body); 53 54 return $this->$name; 55 } 56 57 // unknown 58 $trace = debug_backtrace(); 59 trigger_error( 60 sprintf('Undefined property via __get(): %s in %s on line %s', $name, $trace[0]['file'], $trace[0]['line']), 61 E_USER_NOTICE 62 ); 63 64 return null; 65 } 66 67 public function __isset($name) 68 { 69 if (array_key_exists($name, $this->_collection_uris) || array_key_exists($name, $this->_member_uris)) { 70 return true; 71 } 72 73 return false; 74 } 75 76 protected function _objectify($fields) 77 { 78 // initialize uris 79 $this->_collection_uris = array(); 80 $this->_member_uris = array(); 81 82 foreach ($fields as $key => $val) { 83 // nested uri 84 if ((strlen($key) - 3) == strrpos($key, 'uri', 0) && $key != 'uri') { 85 $result = self::getRegistry()->match($val); 86 if ($result != null) { 87 $name = substr($key, 0, -4); 88 $class = $result['class']; 89 if ($result['collection']) { 90 $this->_collection_uris[$name] = array( 91 'class' => $class, 92 'uri' => $val, 93 ); 94 } else { 95 $this->_member_uris[$name] = array( 96 'class' => $class, 97 'uri' => $val, 98 ); 99 } 100 101 continue; 102 } 103 } elseif (is_object($val) && property_exists($val, 'uri')) { 104 // nested 105 $result = self::getRegistry()->match($val->uri); 106 if ($result != null) { 107 $class = $result['class']; 108 if ($result['collection']) { 109 $this->$key = new Collection($class, $val['uri'], $val); 110 } else { 111 $this->$key = new $class($val); 112 } 113 114 continue; 115 } 116 } elseif (is_array($val) && array_key_exists('uri', $val)) { 117 $result = self::getRegistry()->match($val['uri']); 118 if ($result != null) { 119 $class = $result['class']; 120 if ($result['collection']) { 121 $this->$key = new Collection($class, $val['uri'], $val); 122 } else { 123 $this->$key = new $class($val); 124 } 125 126 continue; 127 } 128 } 129 130 // default 131 $this->$key = $val; 132 } 133 } 134 135 public static function query() 136 { 137 $uri_spec = self::getURISpec(); 138 if ($uri_spec == null || $uri_spec->collection_uri == null) { 139 $msg = sprintf('Cannot directly query %s resources', get_called_class()); 140 throw new \LogicException($msg); 141 } 142 143 return new Query(get_called_class(), $uri_spec->collection_uri); 144 } 145 146 public static function get($uri) 147 { 148 # id 149 if (strncmp($uri, '/', 1)) { 150 $uri_spec = self::getURISpec(); 151 if ($uri_spec == null || $uri_spec->collection_uri == null) { 152 $msg = sprintf('Cannot get %s resources by id %s', $class, $uri); 153 throw new \LogicException($msg); 154 } 155 $uri = $uri_spec->collection_uri . '/' . $uri; 156 } 157 158 $response = self::getClient()->get($uri); 159 $class = get_called_class(); 160 161 return new $class($response->body); 162 } 163 164 public function save() 165 { 166 // payload 167 $payload = array(); 168 foreach ($this as $key => $val) { 169 if ($key[0] == '_' || is_object($val)) { 170 continue; 171 } 172 $payload[$key] = $val; 173 } 174 175 // update 176 if (array_key_exists('uri', $payload)) { 177 $uri = $payload['uri']; 178 unset($payload['uri']); 179 $response = self::getClient()->put($uri, $payload); 180 } else { 181 // create 182 $class = get_class($this); 183 if ($class::$_uri_spec == null || $class::$_uri_spec->collection_uri == null) { 184 $msg = sprintf('Cannot directly create %s resources', $class); 185 throw new \LogicException($msg); 186 } 187 $response = self::getClient()->post($class::$_uri_spec->collection_uri, $payload); 188 } 189 190 // re-objectify 191 foreach ($this as $key => $val) { 192 unset($this->$key); 193 } 194 $this->_objectify($response->body); 195 196 return $this; 197 } 198 199 public function delete() 200 { 201 self::getClient()->delete($this->uri); 202 203 return $this; 204 } 205 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |