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, 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, 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_WindowsAzure_Exception
  38.  */
  39. require_once 'Microsoft/WindowsAzure/Exception.php';
  40.  
  41.  
  42. /**
  43.  * @category   Microsoft
  44.  * @package    Microsoft_WindowsAzure
  45.  * @subpackage Storage
  46.  * @copyright  Copyright (c) 2009, 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 = '1900-01-01T00:00:00';
  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.         return $this->_timestamp;
  143.     }
  144.     
  145.     /**
  146.      * Set timestamp
  147.      * 
  148.      * @azure Timestamp Edm.DateTime
  149.      * @param string $value 
  150.      */
  151.     public function setTimestamp($value '1900-01-01T00:00:00')
  152.     {
  153.         $this->_timestamp = $value;
  154.     }
  155.     
  156.     /**
  157.      * Get etag
  158.      * 
  159.      * @return string 
  160.      */
  161.     public function getEtag()
  162.     {
  163.         return $this->_etag;
  164.     }
  165.     
  166.     /**
  167.      * Set etag
  168.      * 
  169.      * @param string $value 
  170.      */
  171.     public function setEtag($value '')
  172.     {
  173.         $this->_etag = $value;
  174.     }
  175.     
  176.     /**
  177.      * Get Azure values
  178.      * 
  179.      * @return array 
  180.      */
  181.     public function getAzureValues()
  182.     {
  183.         // Get accessors
  184.         $accessors self::getAzureAccessors(get_class($this));
  185.         
  186.         // Loop accessors and retrieve values
  187.         $returnValue array();
  188.         foreach ($accessors as $accessor)
  189.         {
  190.             if ($accessor->EntityType == 'ReflectionProperty')
  191.             {
  192.                 $property $accessor->EntityAccessor;
  193.                 $returnValue[= (object)array(
  194.                     'Name'  => $accessor->AzurePropertyName,
  195.                     'Type'  => $accessor->AzurePropertyType,
  196.                     'Value' => $this->$property,
  197.                 );
  198.             }
  199.             else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor)03== 'get')
  200.             {
  201.                 $method $accessor->EntityAccessor;
  202.                 $returnValue[= (object)array(
  203.                     'Name'  => $accessor->AzurePropertyName,
  204.                     'Type'  => $accessor->AzurePropertyType,
  205.                     'Value' => $this->$method(),
  206.                 );
  207.             }
  208.         }
  209.         
  210.         // Return
  211.         return $returnValue;
  212.     }
  213.     
  214.     /**
  215.      * Set Azure values
  216.      * 
  217.      * @param array $values 
  218.      * @param boolean $throwOnError Throw Microsoft_WindowsAzure_Exception when a property is not specified in $values?
  219.      * @throws Microsoft_WindowsAzure_Exception
  220.      */
  221.     public function setAzureValues($values array()$throwOnError false)
  222.     {
  223.         // Get accessors
  224.         $accessors self::getAzureAccessors(get_class($this));
  225.         
  226.         // Loop accessors and set values
  227.         $returnValue array();
  228.         foreach ($accessors as $accessor)
  229.         {
  230.             if (isset($values[$accessor->AzurePropertyName])) {
  231.                 // Cast to correct type
  232.                 if ($accessor->AzurePropertyType != '')
  233.                 {
  234.                     switch (strtolower($accessor->AzurePropertyType))
  235.                     {
  236.                         case 'edm.int32':
  237.                         case 'edm.int64':
  238.                             $values[$accessor->AzurePropertyNameintval($values[$accessor->AzurePropertyName])break;
  239.                         case 'edm.boolean':
  240.                             if ($values[$accessor->AzurePropertyName== 'true' || $values[$accessor->AzurePropertyName== '1')
  241.                                 $values[$accessor->AzurePropertyNametrue;
  242.                             else
  243.                                 $values[$accessor->AzurePropertyNamefalse;
  244.                             break;
  245.                         case 'edm.double':
  246.                             $values[$accessor->AzurePropertyNamefloatval($values[$accessor->AzurePropertyName])break;
  247.                     }
  248.                 }
  249.                 
  250.                 // Assign value
  251.                 if ($accessor->EntityType == 'ReflectionProperty')
  252.                 {
  253.                     $property $accessor->EntityAccessor;
  254.                     $this->$property $values[$accessor->AzurePropertyName];
  255.                 }
  256.                 else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor)03== 'set')
  257.                 {
  258.                     $method $accessor->EntityAccessor;
  259.                     $this->$method($values[$accessor->AzurePropertyName]);
  260.                 }
  261.             }
  262.             else if ($throwOnError
  263.             {
  264.                 throw new Microsoft_WindowsAzure_Exception("Property '" $accessor->AzurePropertyName "' was not found in \$values array");    
  265.             }
  266.         }
  267.         
  268.         // Return
  269.         return $returnValue;
  270.     }
  271.     
  272.     /**
  273.      * Get Azure accessors from class
  274.      * 
  275.      * @param string $className Class to get accessors for
  276.      * @return array 
  277.      */
  278.     public static function getAzureAccessors($className '')
  279.     {
  280.         // List of accessors
  281.         $azureAccessors array();
  282.         
  283.         // Get all types
  284.         $type new ReflectionClass($className);
  285.         
  286.         // Loop all properties
  287.         $properties $type->getProperties();
  288.         foreach ($properties as $property)
  289.         {
  290.             $accessor self::getAzureAccessor($property);
  291.             if (!is_null($accessor)) {
  292.                 $azureAccessors[$accessor;
  293.             }
  294.         }
  295.         
  296.         // Loop all methods
  297.         $methods $type->getMethods();
  298.         foreach ($methods as $method)
  299.         {
  300.             $accessor self::getAzureAccessor($method);
  301.             if (!is_null($accessor)) {
  302.                 $azureAccessors[$accessor;
  303.             }
  304.         }
  305.         
  306.         // Return
  307.         return $azureAccessors;
  308.     }
  309.     
  310.     /**
  311.      * Get Azure accessor from reflection member
  312.      * 
  313.      * @param ReflectionProperty|ReflectionMethod$member 
  314.      * @return object 
  315.      */
  316.     public static function getAzureAccessor($member)
  317.     {
  318.         // Get comment
  319.         $docComment $member->getDocComment();
  320.         
  321.         // Check for Azure comment
  322.         if (strpos($docComment'@azure'=== false)
  323.             return null;
  324.             
  325.         // Search for @azure contents
  326.         $azureComment '';
  327.         $commentLines explode("\n"$docComment);
  328.         foreach ($commentLines as $commentLine)
  329.         {
  330.             if (strpos($commentLine'@azure'!== false)
  331.             {
  332.                 $azureComment trim(substr($commentLinestrpos($commentLine'@azure'6));
  333.                 while (strpos($azureComment'  '!== false)
  334.                 {
  335.                     $azureComment str_replace('  '' '$azureComment);
  336.                 }
  337.                 break;
  338.             }
  339.         }
  340.         
  341.         // Fetch @azure properties
  342.         $azureProperties explode(' '$azureComment);
  343.         return (object)array(
  344.             'EntityAccessor'    => $member->getName(),
  345.             'EntityType'        => get_class($member),
  346.             'AzurePropertyName' => $azureProperties[0],
  347.             'AzurePropertyType' => isset($azureProperties[1]$azureProperties[1''
  348.         );
  349.     }
  350. }

Documentation generated on Thu, 26 Nov 2009 08:05:35 +0100 by phpDocumentor 1.4.3