From ed391646c621f451a81e378adaeececcc14a41f6 Mon Sep 17 00:00:00 2001 From: e1himself Date: Tue, 15 Sep 2015 10:08:42 +0300 Subject: [PATCH 1/2] Weaken guzzle version requirement to >=4 <6 (instead of ~5) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7ff571c..2ff7669 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": ">=5.4", - "guzzlehttp/guzzle": "~5.0" + "guzzlehttp/guzzle": ">=4,<6" }, "require-dev": { "phpunit/phpunit": "~4" From 778e2f201f78986b2b414f11d0067b97dba538cb Mon Sep 17 00:00:00 2001 From: e1himself Date: Tue, 15 Sep 2015 10:09:42 +0300 Subject: [PATCH 2/2] Fix files to be PSR-4 compatible Previously, If you try to load NylasModelCollection before Nylas, autoloader fails --- src/Nylas.php | 121 +---------------------------------- src/NylasAPIObject.php | 30 +++++++++ src/NylasModelCollection.php | 91 ++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 120 deletions(-) create mode 100644 src/NylasAPIObject.php create mode 100644 src/NylasModelCollection.php diff --git a/src/Nylas.php b/src/Nylas.php index 3cf271a..3f2888a 100644 --- a/src/Nylas.php +++ b/src/Nylas.php @@ -178,125 +178,6 @@ private function generateId() { mt_rand(0, 0xffff), mt_rand(0, 0xffff) ); -} - -} - - -class NylasModelCollection { - - private $chunkSize = 50; - - public function __construct($klass, $api, $namespace=NULL, $filter=array(), $offset=0, $filters=array()) { - $this->klass = $klass; - $this->api = $api; - $this->namespace = $namespace; - $this->filter = $filter; - $this->filters = $filters; - - if(!array_key_exists('offset', $filter)) { - $this->filter['offset'] = 0; - } - } - - public function items() { - $offset = 0; - while (True) { - $items = $this->_getModelCollection($offset, $this->chunkSize); - if(!$items) { - break; - } - foreach ($items as $item) { - yield $item; - } - if (count($items) < $this->chunkSize) { - break; - } - $offset += count($items); - } - } - - public function first() { - $results = $this->_getModelCollection(0, 1); - if ($results) { - return $results[0]; - } - return NULL; - } - - public function all($limit=INF) { - return $this->_range($this->filter['offset'], $limit); - } - - public function where($filter, $filters=array()) { - $this->filter = array_merge($this->filter, $filter); - $this->filter['offset'] = 0; - $collection = clone $this; - $collection->filter = $this->filter; - return $collection; } - public function find($id) { - return $this->_getModel($id); - } - - public function create($data) { - return $this->klass->create($data, $this); - } - - private function _range($offset, $limit) { - $result = array(); - while (count($result) < $limit) { - $to_fetch = min($limit - count($result), $this->chunkSize); - $data = $this->_getModelCollection($offset+count($result), $to_fetch); - $result = array_merge($result, $data); - - if(!$data || count($data) < $to_fetch) { - break; - } - } - return $result; - } - - private function _getModel($id) { - // make filter a kwarg filters - return $this->api->getResource($this->namespace, $this->klass, $id, $this->filter); - } - - private function _getModelCollection($offset, $limit) { - $this->filter['offset'] = $offset; - $this->filter['limit'] = $limit; - return $this->api->getResources($this->namespace, $this->klass, $this->filter); - } - -} - - -class NylasAPIObject { - - public $apiRoot; - - public function __construct() { - $this->apiRoot = 'n'; - } - - public function json() { - return $this->data; - } - - public function _createObject($klass, $namespace, $objects) { - $this->data = $objects; - $this->klass = $klass; - return $this; - } - - public function __get($key) { - if(array_key_exists($key, $this->data)) { - return $this->data[$key]; - } - return NULL; - } - -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/src/NylasAPIObject.php b/src/NylasAPIObject.php new file mode 100644 index 0000000..1c6ccee --- /dev/null +++ b/src/NylasAPIObject.php @@ -0,0 +1,30 @@ +apiRoot = 'n'; + } + + public function json() { + return $this->data; + } + + public function _createObject($klass, $namespace, $objects) { + $this->data = $objects; + $this->klass = $klass; + return $this; + } + + public function __get($key) { + if(array_key_exists($key, $this->data)) { + return $this->data[$key]; + } + return NULL; + } + +} \ No newline at end of file diff --git a/src/NylasModelCollection.php b/src/NylasModelCollection.php new file mode 100644 index 0000000..0e9bf76 --- /dev/null +++ b/src/NylasModelCollection.php @@ -0,0 +1,91 @@ +klass = $klass; + $this->api = $api; + $this->namespace = $namespace; + $this->filter = $filter; + $this->filters = $filters; + + if(!array_key_exists('offset', $filter)) { + $this->filter['offset'] = 0; + } + } + + public function items() { + $offset = 0; + while (True) { + $items = $this->_getModelCollection($offset, $this->chunkSize); + if(!$items) { + break; + } + foreach ($items as $item) { + yield $item; + } + if (count($items) < $this->chunkSize) { + break; + } + $offset += count($items); + } + } + + public function first() { + $results = $this->_getModelCollection(0, 1); + if ($results) { + return $results[0]; + } + return NULL; + } + + public function all($limit=INF) { + return $this->_range($this->filter['offset'], $limit); + } + + public function where($filter, $filters=array()) { + $this->filter = array_merge($this->filter, $filter); + $this->filter['offset'] = 0; + $collection = clone $this; + $collection->filter = $this->filter; + return $collection; + } + + public function find($id) { + return $this->_getModel($id); + } + + public function create($data) { + return $this->klass->create($data, $this); + } + + private function _range($offset, $limit) { + $result = array(); + while (count($result) < $limit) { + $to_fetch = min($limit - count($result), $this->chunkSize); + $data = $this->_getModelCollection($offset+count($result), $to_fetch); + $result = array_merge($result, $data); + + if(!$data || count($data) < $to_fetch) { + break; + } + } + return $result; + } + + private function _getModel($id) { + // make filter a kwarg filters + return $this->api->getResource($this->namespace, $this->klass, $id, $this->filter); + } + + private function _getModelCollection($offset, $limit) { + $this->filter['offset'] = $offset; + $this->filter['limit'] = $limit; + return $this->api->getResources($this->namespace, $this->klass, $this->filter); + } + +} \ No newline at end of file