      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\DNS\Resource;
     19:
     20: use OpenCloud\Common\Exceptions;
     21: use OpenCloud\Common\Http\Message\Formatter;
     22: use OpenCloud\Common\Lang;
     23: use OpenCloud\Common\Resource\PersistentResource;
     24:
     25: abstract class AbstractResource extends PersistentResource
     26: {
     27:     public function create($params = array())
     28:     {
     29:         $body = Formatter::decode(parent::create($params));
     30:
     31:         return new AsyncResponse($this->getService(), $body);
     32:     }
     33:
     34:     public function update($params = array())
     35:     {
     36:         $response = parent::update($params);
     37:         $body = Formatter::decode($response);
     38:
     39:         return new AsyncResponse($this->getService(), $body);
     40:     }
     41:
     42:     public function delete()
     43:     {
     44:         $body = Formatter::decode(parent::delete());
     45:
     46:         return new AsyncResponse($this->getService(), $body);
     47:     }
     48:
     49:     protected function createJson()
     50:     {
     51:         if (!$this->getCreateKeys()) {
     52:             throw new Exceptions\CreateError(
     53:                 Lang::translate('Missing [createKeys]')
     54:             );
     55:         }
     56:
     57:         return (object) array(
     58:             self::jsonCollectionName() => array(
     59:                 $this->getJson($this->getCreateKeys())
     60:             )
     61:         );
     62:     }
     63:
     64:     protected function updateJson($params = array())
     65:     {
     66:         if (!$this->getUpdateKeys()) {
     67:             throw new Exceptions\UpdateError(
     68:                 Lang::translate('Missing [updateKeys]')
     69:             );
     70:         }
     71:
     72:         return $this->getJson($this->getUpdateKeys());
     73:     }
     74:
     75:     /**
     76:      * returns JSON based on $keys
     77:      *
     78:      * @param array $keys list of items to include
     79:      * @return stdClass
     80:      */
     81:     private function getJson($keys)
     82:     {
     83:         $object = new \stdClass;
     84:         foreach ($keys as $item) {
     85:             if (!empty($this->$item)) {
     86:                 $object->$item = $this->$item;
     87:             }
     88:         }
     89:
     90:         return $object;
     91:     }
     92:
     93:     /**
     94:      * Retrieve the keys which are required when the object is created.
     95:      *
     96:      * @return array|false
     97:      */
     98:     public function getCreateKeys()
     99:     {
    100:         return (!empty($this->createKeys)) ? $this->createKeys : false;
    101:     }
    102:
    103:     /**
    104:      * Retrieve the keys which are required when the object is updated.
    105:      *
    106:      * @return array|false
    107:      */
    108:     public function getUpdateKeys()
    109:     {
    110:         return (!empty($this->updateKeys)) ? $this->updateKeys : false;
    111:     }
    112: }
    113: