Exemplo n.º 1
0
 public function test_Metadata()
 {
     $this->addMockSubscriber($this->makeResponse(null, 204));
     $this->queue->setName('test')->saveMetadata(array('new metadata' => 'bar'));
     $metadata = $this->queue->getMetadata();
     $this->assertInstanceOf('OpenCloud\\Common\\Metadata', $metadata);
     $this->assertEquals('bar', $metadata->{'new metadata'});
     $newMetadata = new Metadata();
     $newMetadata->setArray(array('foo' => 'bar'));
     $this->queue->setMetadata($newMetadata);
 }
Exemplo n.º 2
0
 /**
  * @param  mixed $info       The data structure that is populating the object.
  * @param  bool  $setObjects If set to TRUE, then this method will try to populate associated resources as objects
  *                           rather than anonymous data types. So, a Server being populated might stock a Network
  *                           object instead of a stdClass object.
  * @throws Exceptions\InvalidArgumentError
  */
 public function populate($info, $setObjects = true)
 {
     if (is_string($info) || is_integer($info)) {
         $this->setProperty($this->primaryKeyField(), $info);
         $this->refresh($info);
     } elseif (is_object($info) || is_array($info)) {
         foreach ($info as $key => $value) {
             if ($key == 'metadata' || $key == 'meta') {
                 // Try retrieving existing value
                 if (null === ($metadata = $this->getProperty($key))) {
                     // If none exists, create new object
                     $metadata = new Metadata();
                 }
                 // Set values for metadata
                 $metadata->setArray($value);
                 // Set object property
                 $this->setProperty($key, $metadata);
             } elseif (!empty($this->associatedResources[$key]) && $setObjects === true) {
                 // Associated resource
                 try {
                     $resource = $this->getService()->resource($this->associatedResources[$key], $value);
                     $resource->setParent($this);
                     $this->setProperty($key, $resource);
                 } catch (Exception\ServiceException $e) {
                 }
             } elseif (!empty($this->associatedCollections[$key]) && $setObjects === true) {
                 // Associated collection
                 try {
                     $className = $this->associatedCollections[$key];
                     $options = $this->makeResourceIteratorOptions($className);
                     $iterator = ResourceIterator::factory($this, $options, $value);
                     $this->setProperty($key, $iterator);
                 } catch (Exception\ServiceException $e) {
                 }
             } elseif (!empty($this->aliases[$key])) {
                 // Sometimes we might want to preserve camelCase
                 // or covert `rax-bandwidth:bandwidth` to `raxBandwidth`
                 $this->setProperty($this->aliases[$key], $value);
             } else {
                 // Normal key/value pair
                 $this->setProperty($key, $value);
             }
         }
     } elseif (null !== $info) {
         throw new Exceptions\InvalidArgumentError(sprintf(Lang::translate('Argument for [%s] must be string or object'), get_class()));
     }
 }
 /**
  * @param mixed $metadata
  * @return $this
  */
 public function setMetadata($data)
 {
     if ($data instanceof Metadata) {
         $metadata = $data;
     } elseif (is_array($data) || is_object($data)) {
         $metadata = new Metadata();
         $metadata->setArray($data);
     } else {
         throw new \InvalidArgumentException(sprintf('You must specify either an array/object of parameters, or an ' . 'instance of Metadata. You provided: %s', print_r($data, true)));
     }
     $this->metadata = $metadata;
     return $this;
 }
Exemplo n.º 4
0
 public function __set($key, $value)
 {
     // if a key was supplied when creating the object, then we can't set
     // any other values
     if ($this->key && $key != $this->key) {
         throw new Exceptions\MetadataKeyError(sprintf(Lang::translate('You cannot set extra values on [%s]'), $this->Url()));
     }
     // otherwise, just set it;
     parent::__set($key, $value);
 }
Exemplo n.º 5
0
 /**
  * Given an Http response object, converts the appropriate headers
  * to metadata
  *
  * @param  OpenCloud\Common\Request\Response\Http
  * @return void
  */
 public function getMetadata(Http $response)
 {
     $this->metadata = new Metadata();
     $this->metadata->setArray($response->headers(), $this->prefix());
 }
Exemplo n.º 6
0
 /**
  * Retrieve metadata from the API and set it to the local object.
  */
 public function retrieveMetadata()
 {
     $response = $this->getClient()->get($this->url('metadata'))->send();
     $metadata = new Metadata();
     $metadata->setArray(Formatter::decode($response));
     $this->setMetadata($metadata);
 }