Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 1 addition & 119 deletions src/Nylas.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,124 +227,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() {
}

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;
}

}

?>
}
29 changes: 29 additions & 0 deletions src/NylasAPIObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Nylas;

class NylasAPIObject {

public $apiRoot;

public function __construct() {
}

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;
}

}
91 changes: 91 additions & 0 deletions src/NylasModelCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Nylas;

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);
}

}