     1: <?php
     2: /**
     3:  * Copyright 2012-2014 Rackspace US, Inc.
     4:  *
     5:  * Licensed under the Apache License, Version 2.0 (the "License");
     6:  * you may not use this file except in compliance with the License.
     7:  * You may obtain a copy of the License at
     8:  *
     9:  * http://www.apache.org/licenses/LICENSE-2.0
    10:  *
    11:  * Unless required by applicable law or agreed to in writing, software
    12:  * distributed under the License is distributed on an "AS IS" BASIS,
    13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14:  * See the License for the specific language governing permissions and
    15:  * limitations under the License.
    16:  */
    17:
    18: namespace OpenCloud\Autoscale\Resource;
    19:
    20: use OpenCloud\Common\PersistentObject;
    21:
    22: /**
    23:  * Contains generic, abstracted functionality for Autoscale resources.
    24:  */
    25: abstract class AbstractResource extends PersistentObject
    26: {
    27:     /**
    28:      * These are used to set the object used for JSON encode.
    29:      *
    30:      * @var array
    31:      */
    32:     public $createKeys = array();
    33:
    34:     /**
    35:      * These resources are associated with this one. When this resource object
    36:      * is populated, if a key is found matching one of these array keys, it is
    37:      * set as an instantiated resource object (rather than an arbitrary string
    38:      * or stdClass object).
    39:      *
    40:      * @var array
    41:      */
    42:     public $associatedResources = array();
    43:
    44:     /**
    45:      * Same as an associated resource, but it's instantiated as a Collection.
    46:      *
    47:      * @var array
    48:      */
    49:     public $associatedCollections = array();
    50:
    51:     /**
    52:      * Creates the object which will be JSON encoded for request.
    53:      *
    54:      * @return \stdClass
    55:      */
    56:     public function createJson()
    57:     {
    58:         $object = new \stdClass;
    59:
    60:         foreach ($this->createKeys as $key) {
    61:             if ($value = $this->getProperty($key)) {
    62:                 $object->$key = $value;
    63:             }
    64:         }
    65:
    66:         if (count($this->metadata)) {
    67:             $object->metadata = new \stdClass;
    68:             foreach ($this->getMetadata()->toArray() as $key => $value) {
    69:                 $object->metadata->$key = $value;
    70:             }
    71:         }
    72:
    73:         return $object;
    74:     }
    75:
    76:     /**
    77:      * Creates the object which will be JSON encoded for request.
    78:      *
    79:      * @return array
    80:      */
    81:     protected function updateJson($params = array())
    82:     {
    83:         $existing = array();
    84:         foreach ($this->createKeys as $key) {
    85:             $existing[$key] = $this->$key;
    86:         }
    87:
    88:         return $existing + $params;
    89:     }
    90:
    91:     public function primaryKeyField()
    92:     {
    93:         return 'id';
    94:     }
    95: }
    96: