/** * Used for object formatting. * * @param string $entity * @param array $data * @param array $options * @return mixed */ public function cast($entity, array $data, array $options = array()) { foreach ($data as $key => $val) { if (!is_array($val)) { continue; } $class = 'entity'; $model = $entity->model(); $data[$key] = $this->item($model, $val, compact('class')); } return parent::cast($entity, $data, $options); }
/** * Constructor * * @param array $config */ public function __construct(array $config = array()) { $defaults = array('host' => 'api.clickatell.com', 'scheme' => 'https', 'port' => '443', 'method' => 'get', 'service' => 'rest', 'format' => 'xml', 'method_prefix' => 'clickatell', 'encoding' => 'UTF-8', 'socket' => 'Context'); $config += $defaults; if (!isset($config['api_id'])) { throw new ConfigException('Clickatell api_id is not configured.'); } if (!isset($config['api_username'])) { throw new ConfigException('Clickatell api_username is not configured.'); } if (!isset($config['api_password'])) { throw new ConfigException('Clickatell api_password is not configured.'); } parent::__construct($config); }
/** * Casts data into proper format when added to a collection or entity object. * * @param mixed $entity The entity or collection for which data is being cast, or the name of * the model class to which the entity/collection is bound. * @param array $data An array of data being assigned. * @param array $options Any associated options with, for example, instantiating new objects in * which to wrap the data. Options implemented by `cast()` itself: * - `first` _boolean_: Used when only one value is passed to `cast()`. Even though * that value must be wrapped in an array, setting the `'first'` option to `true` * causes only that one value to be returned. * @return mixed Returns the value of `$data`, cast to the proper format according to the schema * definition of the model class specified by `$model`. */ public function cast($entity, array $data, array $options = array()) { $defaults = array('pathKey' => null, 'model' => null); $options += $defaults; $model = $options['model'] ?: $entity->model(); foreach ($data as $key => $val) { if (!is_array($val)) { continue; } $pathKey = $options['pathKey'] ? "{$options['pathKey']}.{$key}" : $key; $class = (range(0, count($val) - 1) === array_keys($val)) ? 'array' : 'entity'; $data[$key] = $this->item($model, $val, compact('class', 'pathKey') + $options); } return parent::cast($entity, $data, $options); }
public function testRespondsToParentCall() { $http = new Http(); $this->assertTrue($http->respondsTo('applyFilter')); $this->assertFalse($http->respondsTo('fooBarBaz')); }
/** * Returns a newly-created `Document` object, bound to a model and populated with default data * and options. * * @param string $model A fully-namespaced class name representing the model class to which the * `Document` object will be bound. * @param array $data The default data with which the new `Document` should be populated. * @param array $options Any additional options to pass to the `Document`'s constructor * @return object Returns a new, un-saved `Document` object bound to the model class specified * in `$model`. */ public function item($model, array $data = array(), array $options = array()) { return parent::item($model, $this->_format($data), $options); }
/** * Add configuration to the WordPress data source * * @param array $config The optional config */ public function __construct(array $config = array()) { $config += array('provider' => 'google', 'providerAdapter' => 'curl', 'providerOptions' => array()); parent::__construct($config); }
public function testDeleteWithModel() { $http = new Http($this->_testConfig); $query = new Query(array('model' => $this->_model, 'data' => array('id' => '1'))); $result = $http->delete($query); $expected = join("\r\n", array('DELETE /posts/1 HTTP/1.1', 'Host: localhost:80', 'Connection: Close', 'User-Agent: Mozilla/5.0', '', '')); $result = (string) $http->last->request; $this->assertEqual($expected, $result); }
/** * Custom check to determine if our given magic methods can be responded to. * * @param string $method Method name. * @param bool $internal Interal call or not. * @return bool */ public function respondsTo($method, $internal = false) { $parentRespondsTo = parent::respondsTo($method, $internal); return $parentRespondsTo || is_callable(array($this->connection, $method)); }
/** * get result * * @param string $type * @param string $resource * @param string $context * @return array */ public function result($type, $resource, $context) { if (!is_object($resource)) { return null; } $result = null; switch ($type) { case 'next': if (!isset($resource->rows)) { $result = (array) $resource; } elseif (isset($resource->rows[$this->_iterator])) { $result = (array) $resource->rows[$this->_iterator]->value; $result['id'] = $resource->rows[$this->_iterator]->id; if (isset($resource->rows[$this->_iterator]->key)) { $result['key'] = $resource->rows[$this->_iterator]->key; } $this->_iterator++; } else { $this->_iterator = 0; } if (isset($result['_id'])) { $result['id'] = $result['_id']; unset($result['_id']); if (isset($result['_rev'])) { $result['rev'] = $result['_rev']; unset($result['_rev']); } } break; case 'close': unset($resource); $result = null; break; default: $result = parent::result($type, $resource, $context); break; } return $result; }
/** * Returns a newly-created `Document` object, bound to a model and populated with default data * and options. * * @param string $model A fully-namespaced class name representing the model class to which the * `Document` object will be bound. * @param array $data The default data with which the new `Document` should be populated. * @param array $options Any additional options to pass to the `Document`'s constructor * @return object Returns a new, un-saved `Document` object bound to the model class specified * in `$model`. */ public function item($model, array $data = array(), array $options = array()) { $defaults = array('class' => 'entity'); $options += $defaults; if (isset($data['doc'])) { return parent::item($model, $this->_format($data['doc']), $options); } if (isset($data['value'])) { $data = $data['value']; } if (isset($options['class']) && $options['class'] === 'entity') { $data = $this->_format($data); } return parent::item($model, $data, $options); }
public function testSendWithQueryObject() { $http = new Http($this->_testConfig); $query = new Query(array('model' => $this->_model, 'data' => array('title' => 'sup'), 'method' => 'post', 'path' => '/some/resource/path')); $result = $http->send($query); $expected = join("\r\n", array('POST /some/resource/path HTTP/1.1', 'Host: localhost:80', 'Connection: Close', 'User-Agent: Mozilla/5.0', 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: 9', '', 'title=sup')); $result = (string) $http->last->request; $this->assertEqual($expected, $result); }
public function testCustomPostMethodWithModel() { $config = $this->_testConfig + array('methods' => array('do' => array('method' => 'post', 'path' => '/do'))); $http = new Http($config); $query = new Query(array('model' => $this->_model, 'data' => array('title' => 'sup'))); $result = $http->do($query); $expected = join("\r\n", array('POST /do HTTP/1.1', 'Host: localhost:80', 'Connection: Close', 'User-Agent: Mozilla/5.0', 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: 9', '', 'title=sup')); $result = (string) $http->last->request; $this->assertEqual($expected, $result); }
/** * The cast() method is used by the data source to recursively inspect and * transform data as it's placed into a collection. In this case, we'll use * cast() to transform arrays into Document objects. * * @param the query model * @param the request results * @param options ie(set, service, entity) */ public function cast($entity, array $data, array $options = array()) { $model = $entity->model(); foreach ($data as $key => $val) { if (!is_array($val)) { continue; } if (!empty($val['doclist']['docs'])) { //XXX The builder is hard-coded to limit groups to one element, // which is why we only use one doc here. //TODO This seems like something that belongs in model-config / queries. $data[$key] = $this->item($model, $val['doclist']['docs'][0], array('class' => 'entity')); } else { $data[$key] = $this->item($model, $val, array('class' => 'entity')); } } return parent::cast($entity, $data, $options); }