Microsoft_WindowsAzure
[ class tree: Microsoft_WindowsAzure ] [ index: Microsoft_WindowsAzure ] [ all elements ]

Source for file TableEntity.php

Documentation is available at TableEntity.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009 - 2011, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_WindowsAzure
  30.  * @subpackage Storage
  31.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  32.  * @license    http://phpazure.codeplex.com/license
  33.  * @version    $Id: BlobInstance.php 14561 2009-05-07 08:05:12Z unknown $
  34.  */
  35.  
  36. /**
  37.  * @see Microsoft_AutoLoader
  38.  */
  39. require_once dirname(__FILE__'/../../AutoLoader.php';
  40.  
  41.  
  42. /**
  43.  * @category   Microsoft
  44.  * @package    Microsoft_WindowsAzure
  45.  * @subpackage Storage
  46.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  47.  * @license    http://phpazure.codeplex.com/license
  48.  */
  49. {
  50.     /**
  51.      * Partition key
  52.      * 
  53.      * @var string 
  54.      */
  55.     protected $_partitionKey;
  56.     
  57.     /**
  58.      * Row key
  59.      * 
  60.      * @var string 
  61.      */
  62.     protected $_rowKey;
  63.     
  64.     /**
  65.      * Timestamp
  66.      * 
  67.      * @var string 
  68.      */
  69.     protected $_timestamp;
  70.     
  71.     /**
  72.      * Etag
  73.      * 
  74.      * @var string 
  75.      */
  76.     protected $_etag = '';
  77.     
  78.     /**
  79.      * Constructor
  80.      * 
  81.      * @param string  $partitionKey    Partition key
  82.      * @param string  $rowKey          Row key
  83.      */
  84.     public function __construct($partitionKey ''$rowKey ''
  85.     {            
  86.         $this->_partitionKey = $partitionKey;
  87.         $this->_rowKey       = $rowKey;
  88.     }
  89.     
  90.     /**
  91.      * Get partition key
  92.      * 
  93.      * @azure PartitionKey
  94.      * @return string 
  95.      */
  96.     public function getPartitionKey()
  97.     {
  98.         return $this->_partitionKey;
  99.     }
  100.     
  101.     /**
  102.      * Set partition key
  103.      * 
  104.      * @azure PartitionKey
  105.      * @param string $value 
  106.      */
  107.     public function setPartitionKey($value)
  108.     {
  109.         $this->_partitionKey = $value;
  110.     }
  111.     
  112.     /**
  113.      * Get row key
  114.      * 
  115.      * @azure RowKey
  116.      * @return string 
  117.      */
  118.     public function getRowKey()
  119.     {
  120.         return $this->_rowKey;
  121.     }
  122.     
  123.     /**
  124.      * Set row key
  125.      * 
  126.      * @azure RowKey
  127.      * @param string $value 
  128.      */
  129.     public function setRowKey($value)
  130.     {
  131.         $this->_rowKey = $value;
  132.     }
  133.     
  134.     /**
  135.      * Get timestamp
  136.      * 
  137.      * @azure Timestamp Edm.DateTime
  138.      * @return string 
  139.      */
  140.     public function getTimestamp()
  141.     {
  142.         if (null === $this->_timestamp{
  143.             $this->setTimestamp(new DateTime());
  144.         }
  145.         return $this->_timestamp;
  146.     }
  147.     
  148.     /**
  149.      * Set timestamp
  150.      * 
  151.      * @azure Timestamp Edm.DateTime
  152.      * @param DateTime $value 
  153.      */
  154.     public function setTimestamp(DateTime $value)
  155.     {
  156.         $this->_timestamp = $value;
  157.     }
  158.     
  159.     /**
  160.      * Get etag
  161.      * 
  162.      * @return string 
  163.      */
  164.     public function getEtag()
  165.     {
  166.         return $this->_etag;
  167.     }
  168.     
  169.     /**
  170.      * Set etag
  171.      * 
  172.      * @param string $value 
  173.      */
  174.     public function setEtag($value '')
  175.     {
  176.         $this->_etag = $value;
  177.     }
  178.     
  179.     /**
  180.      * Get Azure values
  181.      * 
  182.      * @return array 
  183.      */
  184.     public function getAzureValues()
  185.     {
  186.         // Get accessors
  187.         $accessors self::getAzureAccessors(get_class($this));
  188.         
  189.         // Loop accessors and retrieve values
  190.         $returnValue array();
  191.         foreach ($accessors as $accessor{
  192.             if ($accessor->EntityType == 'ReflectionProperty'{
  193.                 $property $accessor->EntityAccessor;
  194.                 $returnValue[= (object)array(
  195.                     'Name'  => $accessor->AzurePropertyName,
  196.                     'Type'  => $accessor->AzurePropertyType,
  197.                     'Value' => $this->$property,
  198.                 );
  199.             else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor)03== 'get'{
  200.                 $method $accessor->EntityAccessor;
  201.                 $returnValue[= (object)array(
  202.                     'Name'  => $accessor->AzurePropertyName,
  203.                     'Type'  => $accessor->AzurePropertyType,
  204.                     'Value' => $this->$method(),
  205.                 );
  206.             }
  207.         }
  208.         
  209.         // Return
  210.         return $returnValue;
  211.     }
  212.     
  213.     /**
  214.      * Set Azure values
  215.      * 
  216.      * @param array $values 
  217.      * @param boolean $throwOnError Throw Microsoft_WindowsAzure_Exception when a property is not specified in $values?
  218.      * @throws Microsoft_WindowsAzure_Exception
  219.      */
  220.     public function setAzureValues($values array()$throwOnError false)
  221.     {
  222.         // Get accessors
  223.         $accessors self::getAzureAccessors(get_class($this));
  224.         
  225.         // Loop accessors and set values
  226.         $returnValue array();
  227.         foreach ($accessors as $accessor{
  228.             if (isset($values[$accessor->AzurePropertyName])) {
  229.                 // Cast to correct type
  230.                 if ($accessor->AzurePropertyType != ''{
  231.                     switch (strtolower($accessor->AzurePropertyType)) {
  232.                         case 'edm.int32':
  233.                         case 'edm.int64':
  234.                             $values[$accessor->AzurePropertyNameintval($values[$accessor->AzurePropertyName])break;
  235.                         case 'edm.boolean':
  236.                             if ($values[$accessor->AzurePropertyName== 'true' || $values[$accessor->AzurePropertyName== '1')
  237.                                 $values[$accessor->AzurePropertyNametrue;
  238.                             else
  239.                                 $values[$accessor->AzurePropertyNamefalse;
  240.                             break;
  241.                         case 'edm.double':
  242.                             $values[$accessor->AzurePropertyNamefloatval($values[$accessor->AzurePropertyName])break;
  243.                         case 'edm.datetime':
  244.                             $values[$accessor->AzurePropertyName$this->_convertToDateTime($values[$accessor->AzurePropertyName])break;
  245.                     }
  246.                 }
  247.                 
  248.                 // Assign value
  249.                 if ($accessor->EntityType == 'ReflectionProperty'{
  250.                     $property $accessor->EntityAccessor;
  251.                     $this->$property $values[$accessor->AzurePropertyName];
  252.                 else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor)03== 'set'{
  253.                     $method $accessor->EntityAccessor;
  254.                     $this->$method($values[$accessor->AzurePropertyName]);
  255.                 }
  256.             else if ($throwOnError{
  257.                 throw new Microsoft_WindowsAzure_Exception("Property '" $accessor->AzurePropertyName "' was not found in \$values array");    
  258.             }
  259.         }
  260.         
  261.         // Return
  262.         return $returnValue;
  263.     }
  264.     
  265.     /**
  266.      * Get Azure accessors from class
  267.      * 
  268.      * @param string $className Class to get accessors for
  269.      * @return array 
  270.      */
  271.     public static function getAzureAccessors($className '')
  272.     {
  273.         // List of accessors
  274.         $azureAccessors array();
  275.         
  276.         // Get all types
  277.         $type new ReflectionClass($className);
  278.         
  279.         // Loop all properties
  280.         $properties $type->getProperties();
  281.         foreach ($properties as $property{
  282.             $accessor self::getAzureAccessor($property);
  283.             if (!is_null($accessor)) {
  284.                 $azureAccessors[$accessor;
  285.             }
  286.         }
  287.         
  288.         // Loop all methods
  289.         $methods $type->getMethods();
  290.         foreach ($methods as $method{
  291.             $accessor self::getAzureAccessor($method);
  292.             if (!is_null($accessor)) {
  293.                 $azureAccessors[$accessor;
  294.             }
  295.         }
  296.         
  297.         // Return
  298.         return $azureAccessors;
  299.     }
  300.     
  301.     /**
  302.      * Get Azure accessor from reflection member
  303.      * 
  304.      * @param ReflectionProperty|ReflectionMethod$member 
  305.      * @return object 
  306.      */
  307.     public static function getAzureAccessor($member)
  308.     {
  309.         // Get comment
  310.         $docComment $member->getDocComment();
  311.         
  312.         // Check for Azure comment
  313.         if (strpos($docComment'@azure'=== false)
  314.         {
  315.             return null;
  316.         }
  317.             
  318.         // Search for @azure contents
  319.         $azureComment '';
  320.         $commentLines explode("\n"$docComment);
  321.         foreach ($commentLines as $commentLine{
  322.             if (strpos($commentLine'@azure'!== false{
  323.                 $azureComment trim(substr($commentLinestrpos($commentLine'@azure'6));
  324.                 while (strpos($azureComment'  '!== false{
  325.                     $azureComment str_replace('  '' '$azureComment);
  326.                 }
  327.                 break;
  328.             }
  329.         }
  330.         
  331.         // Fetch @azure properties
  332.         $azureProperties explode(' '$azureComment);
  333.         return (object)array(
  334.             'EntityAccessor'    => $member->getName(),
  335.             'EntityType'        => get_class($member),
  336.             'AzurePropertyName' => $azureProperties[0],
  337.             'AzurePropertyType' => isset($azureProperties[1]$azureProperties[1''
  338.         );
  339.     }
  340.     
  341.     /**
  342.      * Converts a string to a DateTime object. Returns false on failure.
  343.      * 
  344.      * @param string $value The string value to parse
  345.      * @return DateTime|boolean
  346.      */
  347.     protected function _convertToDateTime($value ''
  348.     {
  349.         if ($value === ''{
  350.             return false;
  351.         }
  352.         
  353.         if ($value instanceof DateTime{
  354.             return $value;
  355.         }
  356.         
  357.         if (@strtotime($value!== false{
  358.             try {
  359.                 if (substr($value-1== 'Z'{
  360.                     $value substr($value0strlen($value1);
  361.                 }
  362.                 return new DateTime($valuenew DateTimeZone('UTC'));
  363.             }
  364.             catch (Exception $ex{
  365.                 return false;
  366.             }
  367.         }
  368.         
  369.         return false;
  370.     }
  371. }

Documentation generated on Wed, 18 May 2011 12:06:59 +0200 by phpDocumentor 1.4.3