|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Service/WindowsAzure/Exception.php'; 00027 00028 00036 class Zend_Service_WindowsAzure_Storage_TableEntity 00037 { 00043 protected $_partitionKey; 00044 00050 protected $_rowKey; 00051 00057 protected $_timestamp = '1900-01-01T00:00:00'; 00058 00064 protected $_etag = ''; 00065 00072 public function __construct($partitionKey = '', $rowKey = '') 00073 { 00074 $this->_partitionKey = $partitionKey; 00075 $this->_rowKey = $rowKey; 00076 } 00077 00084 public function getPartitionKey() 00085 { 00086 return $this->_partitionKey; 00087 } 00088 00095 public function setPartitionKey($value) 00096 { 00097 $this->_partitionKey = $value; 00098 } 00099 00106 public function getRowKey() 00107 { 00108 return $this->_rowKey; 00109 } 00110 00117 public function setRowKey($value) 00118 { 00119 $this->_rowKey = $value; 00120 } 00121 00128 public function getTimestamp() 00129 { 00130 return $this->_timestamp; 00131 } 00132 00139 public function setTimestamp($value = '1900-01-01T00:00:00') 00140 { 00141 $this->_timestamp = $value; 00142 } 00143 00149 public function getEtag() 00150 { 00151 return $this->_etag; 00152 } 00153 00159 public function setEtag($value = '') 00160 { 00161 $this->_etag = $value; 00162 } 00163 00169 public function getAzureValues() 00170 { 00171 // Get accessors 00172 $accessors = self::getAzureAccessors(get_class($this)); 00173 00174 // Loop accessors and retrieve values 00175 $returnValue = array(); 00176 foreach ($accessors as $accessor) { 00177 if ($accessor->EntityType == 'ReflectionProperty') { 00178 $property = $accessor->EntityAccessor; 00179 $returnValue[] = (object)array( 00180 'Name' => $accessor->AzurePropertyName, 00181 'Type' => $accessor->AzurePropertyType, 00182 'Value' => $this->$property, 00183 ); 00184 } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'get') { 00185 $method = $accessor->EntityAccessor; 00186 $returnValue[] = (object)array( 00187 'Name' => $accessor->AzurePropertyName, 00188 'Type' => $accessor->AzurePropertyType, 00189 'Value' => $this->$method(), 00190 ); 00191 } 00192 } 00193 00194 // Return 00195 return $returnValue; 00196 } 00197 00205 public function setAzureValues($values = array(), $throwOnError = false) 00206 { 00207 // Get accessors 00208 $accessors = self::getAzureAccessors(get_class($this)); 00209 00210 // Loop accessors and set values 00211 $returnValue = array(); 00212 foreach ($accessors as $accessor) { 00213 if (isset($values[$accessor->AzurePropertyName])) { 00214 // Cast to correct type 00215 if ($accessor->AzurePropertyType != '') { 00216 switch (strtolower($accessor->AzurePropertyType)) { 00217 case 'edm.int32': 00218 case 'edm.int64': 00219 $values[$accessor->AzurePropertyName] = intval($values[$accessor->AzurePropertyName]); break; 00220 case 'edm.boolean': 00221 if ($values[$accessor->AzurePropertyName] == 'true' || $values[$accessor->AzurePropertyName] == '1') 00222 $values[$accessor->AzurePropertyName] = true; 00223 else 00224 $values[$accessor->AzurePropertyName] = false; 00225 break; 00226 case 'edm.double': 00227 $values[$accessor->AzurePropertyName] = floatval($values[$accessor->AzurePropertyName]); break; 00228 } 00229 } 00230 00231 // Assign value 00232 if ($accessor->EntityType == 'ReflectionProperty') { 00233 $property = $accessor->EntityAccessor; 00234 $this->$property = $values[$accessor->AzurePropertyName]; 00235 } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'set') { 00236 $method = $accessor->EntityAccessor; 00237 $this->$method($values[$accessor->AzurePropertyName]); 00238 } 00239 } else if ($throwOnError) { 00240 throw new Zend_Service_WindowsAzure_Exception("Property '" . $accessor->AzurePropertyName . "' was not found in \$values array"); 00241 } 00242 } 00243 00244 // Return 00245 return $returnValue; 00246 } 00247 00254 public static function getAzureAccessors($className = '') 00255 { 00256 // List of accessors 00257 $azureAccessors = array(); 00258 00259 // Get all types 00260 $type = new ReflectionClass($className); 00261 00262 // Loop all properties 00263 $properties = $type->getProperties(); 00264 foreach ($properties as $property) { 00265 $accessor = self::getAzureAccessor($property); 00266 if (!is_null($accessor)) { 00267 $azureAccessors[] = $accessor; 00268 } 00269 } 00270 00271 // Loop all methods 00272 $methods = $type->getMethods(); 00273 foreach ($methods as $method) { 00274 $accessor = self::getAzureAccessor($method); 00275 if (!is_null($accessor)) { 00276 $azureAccessors[] = $accessor; 00277 } 00278 } 00279 00280 // Return 00281 return $azureAccessors; 00282 } 00283 00290 public static function getAzureAccessor($member) 00291 { 00292 // Get comment 00293 $docComment = $member->getDocComment(); 00294 00295 // Check for Azure comment 00296 if (strpos($docComment, '@azure') === false) 00297 { 00298 return null; 00299 } 00300 00301 // Search for @azure contents 00302 $azureComment = ''; 00303 $commentLines = explode("\n", $docComment); 00304 foreach ($commentLines as $commentLine) { 00305 if (strpos($commentLine, '@azure') !== false) { 00306 $azureComment = trim(substr($commentLine, strpos($commentLine, '@azure') + 6)); 00307 while (strpos($azureComment, ' ') !== false) { 00308 $azureComment = str_replace(' ', ' ', $azureComment); 00309 } 00310 break; 00311 } 00312 } 00313 00314 // Fetch @azure properties 00315 $azureProperties = explode(' ', $azureComment); 00316 return (object)array( 00317 'EntityAccessor' => $member->getName(), 00318 'EntityType' => get_class($member), 00319 'AzurePropertyName' => $azureProperties[0], 00320 'AzurePropertyType' => isset($azureProperties[1]) ? $azureProperties[1] : '' 00321 ); 00322 } 00323 }