From 1ac7fa31bb9aab840b9428c4480ce32c1fc13099 Mon Sep 17 00:00:00 2001 From: Marian Sollmann Date: Wed, 5 Nov 2014 18:15:49 +0100 Subject: [PATCH 1/2] add logic --- source/Feedlabs/Feedify/Client.php | 140 +++++++++++--- .../Feedify/Exception/ParamsException.php | 11 ++ source/Feedlabs/Feedify/Params.php | 174 ++++++++++++++++++ ...stractResource.php => AbstractElement.php} | 10 +- .../Feedify/Resource/AbstractList.php | 90 +++++++++ .../Feedlabs/Feedify/Resource/Application.php | 79 ++++++++ .../Feedify/Resource/ApplicationList.php | 16 ++ source/Feedlabs/Feedify/Resource/Entry.php | 12 +- .../Feedlabs/Feedify/Resource/EntryList.php | 16 ++ source/Feedlabs/Feedify/Resource/Feed.php | 65 +++++-- source/Feedlabs/Feedify/Resource/FeedList.php | 16 ++ source/Feedlabs/Feedify/Resource/Token.php | 67 +++++++ .../Feedlabs/Feedify/Resource/TokenList.php | 16 ++ 13 files changed, 669 insertions(+), 43 deletions(-) create mode 100644 source/Feedlabs/Feedify/Exception/ParamsException.php create mode 100644 source/Feedlabs/Feedify/Params.php rename source/Feedlabs/Feedify/Resource/{AbstractResource.php => AbstractElement.php} (61%) create mode 100644 source/Feedlabs/Feedify/Resource/AbstractList.php create mode 100644 source/Feedlabs/Feedify/Resource/Application.php create mode 100644 source/Feedlabs/Feedify/Resource/ApplicationList.php create mode 100644 source/Feedlabs/Feedify/Resource/EntryList.php create mode 100644 source/Feedlabs/Feedify/Resource/FeedList.php create mode 100644 source/Feedlabs/Feedify/Resource/Token.php create mode 100644 source/Feedlabs/Feedify/Resource/TokenList.php diff --git a/source/Feedlabs/Feedify/Client.php b/source/Feedlabs/Feedify/Client.php index 1a777c1..a78f680 100644 --- a/source/Feedlabs/Feedify/Client.php +++ b/source/Feedlabs/Feedify/Client.php @@ -2,7 +2,12 @@ namespace Feedlabs\Feedify; +use Feedlabs\Feedify\Resource\Application; +use Feedlabs\Feedify\Resource\ApplicationList; use Feedlabs\Feedify\Resource\Feed; +use Feedlabs\Feedify\Resource\FeedList; +use Feedlabs\Feedify\Resource\Token; +use Feedlabs\Feedify\Resource\TokenList; /** * Class Client @@ -31,49 +36,118 @@ public function __construct($apiId, $apiToken) { } /** - * @param string $id - * @return Feed + * @return ApplicationList + */ + public function getApplicationList() { + // $result = static::getRequest()->get('/feed'); + // foreach ($result as $feedData) { + // $applicationList[] = new Feed($feedData['Id'], array('Data' => $feedData['Data'])); + // } + // todo: load over API + + $applicationList = []; + for ($i = 0; $i < 5; $i++) { + $applicationList[] = ['id' => 'id' . $i, 'name' => 'Name-' . $i, 'description' => 'description-' . $i, 'createStamp' => time()]; + } + + return new ApplicationList($applicationList); + } + + /** + * @param string $applicationId + * @return Application */ - public function getFeed($id) { - $id = (string) $id; - $data = static::getRequest()->get('/feed/' . $id); - return new Feed($id, $data); + public function getApplication($applicationId) { + $applicationId = (string) $applicationId; + // $data = static::getRequest()->get('/feed/' . $id); + // todo: load over API + + $data = new Params([ + 'id' => $applicationId, + 'name' => 'Name-' . $applicationId, + 'description' => 'description' . $applicationId, + 'createStamp' => time(), + ]); + return new Application($data); } /** - * @return Feed[] + * @param string $applicationId + * @return FeedList */ - public function getFeedList() { - $feedList = array(); - $result = static::getRequest()->get('/feed'); - foreach ($result as $feedData) { - $feedList[] = new Feed($feedData['Id'], array('Data' => $feedData['Data'])); + public function getFeedList($applicationId) { + $applicationId = (string) $applicationId; + // $result = static::getRequest()->get('/feed'); + // foreach ($result as $feedData) { + // $applicationList[] = new Feed($feedData['Id'], array('Data' => $feedData['Data'])); + // } + // todo: load over API + + $feedList = []; + for ($i = 0; $i < 5; $i++) { + $feedList[] = [ + 'id' => 'id' . $i, + 'name' => 'Name-' . $i, + 'description' => 'description-' . $i, + 'channel' => 'channel-' . $i, + 'createStamp' => time(), + ]; } - return $feedList; + + return new FeedList($feedList); + } + + /** + * @param string $applicationId + * @param string $feedId + * @return Feed + */ + public function getFeed($applicationId, $feedId) { + // $feedId = (string) $feedId; + // $data = static::getRequest()->get('/application/' . $applicationId . '/feed/' . $feedId); + // return new Feed(new Params($data)); + + $applicationId = (string) $applicationId; + $feedId = (string) $feedId; + // $data = static::getRequest()->get('/feed/' . $id); + // todo: load over API + + $data = new Params([ + 'id' => $feedId, + 'name' => 'Name-' . $feedId, + 'description' => 'description' . $feedId, + 'channel' => 'channel' . $feedId, + 'createStamp' => time(), + ]); + return new Feed($data); } /** - * @param array $data + * @param string $applicationId + * @param array $data * @return string */ - public function createFeed(array $data) { - $result = static::getRequest()->post('/feed', $data); + public function createFeed($applicationId, array $data) { + $result = static::getRequest()->post('/application/' . $applicationId . '/feed', $data); + // todo: also return channel Id return $result['id']; } /** - * @param string $id + * @param string $applicationId + * @param string $feedId * @param array $data */ - public function updateFeed($id, array $data) { - static::getRequest()->put('/feed/' . $id, $data); + public function updateFeed($applicationId, $feedId, array $data) { + static::getRequest()->put('/application/' . $applicationId . '/feed/' . $feedId, $data); } /** - * @param string $id + * @param string $applicationId + * @param string $feedId */ - public function deleteFeed($id) { - static::getRequest()->delete('/feed/' . $id); + public function deleteFeed($applicationId, $feedId) { + static::getRequest()->delete('/application/' . $applicationId . '/feed/' . $feedId); } /** @@ -103,6 +177,28 @@ public function deleteEntry($feedId, $entryId) { static::getRequest()->delete('/feed/' . $feedId . '/entry/' . $entryId); } + /** + * @param string $token + * @return Token + */ + public function getToken($token) { + $token = (string) $token; + // todo: load over API + + return new Token(new Params(['token' => $token, 'name' => 'Namedjgsfjhsdgfhjsfgdshj', 'createStamp' => time()])); + } + + public function getTokenList() { + // todo: load over API + + $tokenList = []; + for ($i = 0; $i < 5; $i++) { + $tokenList[] = ['token' => 'token' . $i, 'name' => 'Name-' . $i, 'createStamp' => time()]; + } + + return new TokenList($tokenList); + } + /** * @return Request */ diff --git a/source/Feedlabs/Feedify/Exception/ParamsException.php b/source/Feedlabs/Feedify/Exception/ParamsException.php new file mode 100644 index 0000000..695269b --- /dev/null +++ b/source/Feedlabs/Feedify/Exception/ParamsException.php @@ -0,0 +1,11 @@ +_params = $params ?: array(); + } + + /** + * @param string $key + * @param mixed|null $default + * @return mixed + */ + public function get($key, $default = null) { + return $this->_get($key, $default); + } + + /** + * @param string $key + * @param mixed $value + */ + public function set($key, $value) { + $this->_params[$key] = $value; + } + + /** + * @param string $key + * @return bool + */ + public function has($key) { + return array_key_exists($key, $this->_params) && null !== $this->_params[$key]; + } + + /** + * @param string $key + * @param string|null $default + * @return float + */ + public function getFloat($key, $default = null) { + $param = $this->_get($key, $default); + return $this->_getFloat($param); + } + + /** + * @param string $key + * @param string|null $default + * @return string + */ + public function getString($key, $default = null) { + $param = $this->_get($key, $default); + return $this->_getString($param); + } + + /** + * @param string $key + * @param string|null $default + * @return int + */ + public function getInt($key, $default = null) { + $param = $this->_get($key, $default); + return $this->_getInt($param); + } + + /** + * @param string $key + * @param array $default + * @return array + * @throws ParamsException + */ + public function getArray($key, array $default = null) { + $param = $this->_get($key, $default); + if (!is_array($param)) { + throw new ParamsException('Not an Array'); + } + return (array) $param; + } + + /** + * @param string $key + * @param boolean $default + * @return boolean + * @throws ParamsException + */ + public function getBoolean($key, $default = null) { + $param = $this->_get($key, $default); + if (1 === $param || '1' === $param || 'true' === $param) { + $param = true; + } + if (0 === $param || '0' === $param || 'false' === $param) { + $param = false; + } + if (!is_bool($param)) { + throw new ParamsException('Not a boolean'); + } + return (boolean) $param; + } + + /** + * @param string $key + */ + public function remove($key) { + unset($this->_params[$key]); + } + + /** + * @param string $key + * @param mixed $default + * @throws ParamsException + * @return mixed + */ + protected function _get($key, $default = null) { + if (!$this->has($key) && $default === null) { + throw new ParamsException("Param `$key` not set"); + } + if (!$this->has($key) && $default !== null) { + return $default; + } + return $this->_params[$key]; + } + + /** + * @param mixed $param + * @return float + * @throws ParamsException + */ + private function _getFloat($param) { + if (is_float($param) || is_int($param)) { + return (float) $param; + } + if (is_string($param)) { + if (preg_match('/^-?(?:\\d++\\.?+\\d*+|\\.\\d++)$/', $param)) { + return (float) $param; + } + } + throw new ParamsException('Not a float'); + } + + /** + * @param mixed $param + * @return string + * @throws ParamsException + */ + private function _getString($param) { + if (!is_string($param)) { + throw new ParamsException('Not a String'); + } + return (string) $param; + } + + /** + * @param mixed $param + * @return int + * @throws ParamsException + */ + private function _getInt($param) { + if (!ctype_digit($param) && !is_int($param)) { + throw new ParamsException('Not an Integer'); + } + return (int) $param; + } +} diff --git a/source/Feedlabs/Feedify/Resource/AbstractResource.php b/source/Feedlabs/Feedify/Resource/AbstractElement.php similarity index 61% rename from source/Feedlabs/Feedify/Resource/AbstractResource.php rename to source/Feedlabs/Feedify/Resource/AbstractElement.php index 63986e1..7ac2144 100644 --- a/source/Feedlabs/Feedify/Resource/AbstractResource.php +++ b/source/Feedlabs/Feedify/Resource/AbstractElement.php @@ -2,20 +2,22 @@ namespace Feedlabs\Feedify\Resource; +use Feedlabs\Feedify\Params; + /** * Class AbstractResource * @package Feedlabs\Feedify\Resource */ -abstract class AbstractResource { +abstract class AbstractElement { /** @var string */ protected $_id; /** - * @param string $id + * @param Params $data */ - public function __construct($id) { - $this->_id = (string) $id; + public function __construct(Params $data) { + $this->_id = $data->getString('id'); } /** diff --git a/source/Feedlabs/Feedify/Resource/AbstractList.php b/source/Feedlabs/Feedify/Resource/AbstractList.php new file mode 100644 index 0000000..462af34 --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/AbstractList.php @@ -0,0 +1,90 @@ +_itemListRaw = (array) $itemList; + } + + /** + * @return array + */ + public function getItems() { + if (!$this->_itemList) { + $itemList = array(); + foreach ($this->_itemListRaw as $item) { + $itemList[] = $this->_processItem($item); + } + $this->_itemList = $itemList; + } + return $this->_itemList; + } + + /** + * @return int + */ + public function getCount() { + return count($this->getItems()); + } + + /** + * @return bool + */ + public function isEmpty() { + return 0 === $this->getCount(); + } + + /** + * @param array $item + * @return AbstractElement|array + */ + protected function _processItem($item) { + return $item; + } + + function rewind() { + $this->_iteratorItems = $this->getItems(); + $this->_iteratorPosition = 0; + } + + function current() { + return $this->_iteratorItems[$this->_iteratorPosition]; + } + + function key() { + return $this->_iteratorPosition; + } + + function next() { + ++$this->_iteratorPosition; + } + + function valid() { + return isset($this->_iteratorItems[$this->_iteratorPosition]); + } + + public function count() { + return $this->getCount(); + } +} diff --git a/source/Feedlabs/Feedify/Resource/Application.php b/source/Feedlabs/Feedify/Resource/Application.php new file mode 100644 index 0000000..ce66161 --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/Application.php @@ -0,0 +1,79 @@ +_name = $data->getString('name'); + $this->_createStamp = $data->getInt('createStamp'); + $this->_description = ($data->has('description')) ? $data->getString('description') : null; + } + + /** + * @return string + */ + public function getName() { + return $this->_name; + } + + /** + * @return string|null + */ + public function getDescription() { + return $this->_description; + } + + /** + * @return int + */ + public function getCreated() { + return $this->_createStamp; + } + + public function getFeedList() { + // todo: load over API + $feedList = []; + for ($i = 0; $i < 3; $i++) { + $feedList[] = ['id' => 'id' . $i, 'name' => 'Name-' . $i, 'description' => 'description-' . $i, 'createStamp' => time(), 'channel' => 'channel' . $i]; + } + + return new FeedList($feedList); + } + + /** + * @return array + */ + public function delete() { + // todo: add delete + // return $this->_getRequest()->delete('/feed/' . $this->getId()); + } + + /** + * @return Request + */ + protected function _getRequest() { + return Client::getRequest(); + } +} diff --git a/source/Feedlabs/Feedify/Resource/ApplicationList.php b/source/Feedlabs/Feedify/Resource/ApplicationList.php new file mode 100644 index 0000000..eb5a640 --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/ApplicationList.php @@ -0,0 +1,16 @@ +_data = $data; + public function __construct(Params $data) { + parent::__construct($data); + $this->_data = $data->getString('data'); } /** diff --git a/source/Feedlabs/Feedify/Resource/EntryList.php b/source/Feedlabs/Feedify/Resource/EntryList.php new file mode 100644 index 0000000..a71a67e --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/EntryList.php @@ -0,0 +1,16 @@ +_data = $data; + public function __construct(Params $data) { + parent::__construct($data); + $this->_name = $data->getString('name'); + $this->_createStamp = $data->getInt('createStamp'); + $this->_channel = $data->getString('channel'); + $this->_description = ($data->has('description')) ? $data->getString('description') : null; } /** - * @return array + * @return string + */ + public function getName() { + return $this->_name; + } + + /** + * @return string|null */ - public function getData() { - return $this->_data; + public function getDescription() { + return $this->_description; + } + + /** + * @return string + */ + public function getChannel() { + return $this->_channel; + } + + /** + * @return int + */ + public function getCreated() { + return $this->_createStamp; + } + + public function getEntryList() { + // todo: load over API + $entryList = []; + for ($i = 0; $i < 3; $i++) { + $entryList[] = ['id' => 'id' . $i, 'data' => 'Data-' . $i, 'createStamp' => time()]; + } + + return new EntryList($entryList); } /** diff --git a/source/Feedlabs/Feedify/Resource/FeedList.php b/source/Feedlabs/Feedify/Resource/FeedList.php new file mode 100644 index 0000000..429974a --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/FeedList.php @@ -0,0 +1,16 @@ +_token = $data->getString('token'); + $this->_name = $data->getString('name'); + $this->_createStamp = $data->getInt('createStamp'); + } + + /** + * @return string|null + */ + public function getToken() { + return $this->_token; + } + + /** + * @return string + */ + public function getName() { + return $this->_name; + } + + /** + * @return int + */ + public function getCreated() { + return $this->_createStamp; + } + + /** + * @return array + */ + public function delete() { + // todo: add delete + } + + /** + * @return Request + */ + protected function _getRequest() { + return Client::getRequest(); + } +} diff --git a/source/Feedlabs/Feedify/Resource/TokenList.php b/source/Feedlabs/Feedify/Resource/TokenList.php new file mode 100644 index 0000000..17cc516 --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/TokenList.php @@ -0,0 +1,16 @@ + Date: Thu, 20 Nov 2014 12:51:14 +0100 Subject: [PATCH 2/2] refactoring --- composer.json | 4 +- source/Feedlabs/Feedify/Client.php | 201 +++--------------- .../Feedify/Client/AdminUserClient.php | 95 +++++++++ .../Feedify/Client/ApplicationClient.php | 102 +++++++++ .../Feedlabs/Feedify/Client/EntryClient.php | 30 +++ source/Feedlabs/Feedify/Client/FeedClient.php | 81 +++++++ .../Feedlabs/Feedify/Client/TokenClient.php | 51 +++++ source/Feedlabs/Feedify/Request.php | 23 +- .../Feedify/Resource/AbstractElement.php | 27 ++- .../Feedlabs/Feedify/Resource/AdminUser.php | 65 ++++++ .../Feedify/Resource/AdminUserList.php | 16 ++ .../Feedlabs/Feedify/Resource/Application.php | 65 +++--- source/Feedlabs/Feedify/Resource/Entry.php | 24 ++- source/Feedlabs/Feedify/Resource/Feed.php | 75 ++++--- .../Feedlabs/Tests/Feedify/ClientTest.php | 15 -- .../Feedlabs/Tests/Feedify/RequestTest.php | 6 +- 16 files changed, 599 insertions(+), 281 deletions(-) create mode 100644 source/Feedlabs/Feedify/Client/AdminUserClient.php create mode 100644 source/Feedlabs/Feedify/Client/ApplicationClient.php create mode 100644 source/Feedlabs/Feedify/Client/EntryClient.php create mode 100644 source/Feedlabs/Feedify/Client/FeedClient.php create mode 100644 source/Feedlabs/Feedify/Client/TokenClient.php create mode 100644 source/Feedlabs/Feedify/Resource/AdminUser.php create mode 100644 source/Feedlabs/Feedify/Resource/AdminUserList.php diff --git a/composer.json b/composer.json index 9f0d9c6..343c976 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,8 @@ ], "support": { "email": "hello@feedlabs.io", - "issues": "https://github.com/feedlabs/sdk-php/issues", - "source": "https://github.com/feedlabs/sdk-php" + "source": "https://github.com/feedlabs/sdk-php", + "issues": "https://github.com/feedlabs/sdk-php/issues" }, "require-dev": { "phpunit/phpunit": "~4.1.3", diff --git a/source/Feedlabs/Feedify/Client.php b/source/Feedlabs/Feedify/Client.php index a78f680..a989ef2 100644 --- a/source/Feedlabs/Feedify/Client.php +++ b/source/Feedlabs/Feedify/Client.php @@ -2,12 +2,11 @@ namespace Feedlabs\Feedify; -use Feedlabs\Feedify\Resource\Application; -use Feedlabs\Feedify\Resource\ApplicationList; -use Feedlabs\Feedify\Resource\Feed; -use Feedlabs\Feedify\Resource\FeedList; -use Feedlabs\Feedify\Resource\Token; -use Feedlabs\Feedify\Resource\TokenList; +use Feedlabs\Feedify\Client\AdminUserClient; +use Feedlabs\Feedify\Client\ApplicationClient; +use Feedlabs\Feedify\Client\EntryClient; +use Feedlabs\Feedify\Client\FeedClient; +use Feedlabs\Feedify\Client\TokenClient; /** * Class Client @@ -17,8 +16,20 @@ class Client { CONST API_VERSION = 1; - /** @var string */ - private static $_apiId; + /** @var ApplicationClient */ + public $application; + + /** @var FeedClient */ + public $feed; + + /** @var EntryClient */ + public $entry; + + /** @var AdminUserClient */ + public $adminUser; + + /** @var TokenClient */ + public $token; /** @var string */ private static $_apiToken; @@ -27,176 +38,16 @@ class Client { private static $_request; /** - * @param string $apiId * @param string $apiToken */ - public function __construct($apiId, $apiToken) { - static::$_apiId = (string) $apiId; + public function __construct($apiToken) { static::$_apiToken = (string) $apiToken; - } - - /** - * @return ApplicationList - */ - public function getApplicationList() { - // $result = static::getRequest()->get('/feed'); - // foreach ($result as $feedData) { - // $applicationList[] = new Feed($feedData['Id'], array('Data' => $feedData['Data'])); - // } - // todo: load over API - - $applicationList = []; - for ($i = 0; $i < 5; $i++) { - $applicationList[] = ['id' => 'id' . $i, 'name' => 'Name-' . $i, 'description' => 'description-' . $i, 'createStamp' => time()]; - } - - return new ApplicationList($applicationList); - } - - /** - * @param string $applicationId - * @return Application - */ - public function getApplication($applicationId) { - $applicationId = (string) $applicationId; - // $data = static::getRequest()->get('/feed/' . $id); - // todo: load over API - - $data = new Params([ - 'id' => $applicationId, - 'name' => 'Name-' . $applicationId, - 'description' => 'description' . $applicationId, - 'createStamp' => time(), - ]); - return new Application($data); - } - - /** - * @param string $applicationId - * @return FeedList - */ - public function getFeedList($applicationId) { - $applicationId = (string) $applicationId; - // $result = static::getRequest()->get('/feed'); - // foreach ($result as $feedData) { - // $applicationList[] = new Feed($feedData['Id'], array('Data' => $feedData['Data'])); - // } - // todo: load over API - - $feedList = []; - for ($i = 0; $i < 5; $i++) { - $feedList[] = [ - 'id' => 'id' . $i, - 'name' => 'Name-' . $i, - 'description' => 'description-' . $i, - 'channel' => 'channel-' . $i, - 'createStamp' => time(), - ]; - } - - return new FeedList($feedList); - } - - /** - * @param string $applicationId - * @param string $feedId - * @return Feed - */ - public function getFeed($applicationId, $feedId) { - // $feedId = (string) $feedId; - // $data = static::getRequest()->get('/application/' . $applicationId . '/feed/' . $feedId); - // return new Feed(new Params($data)); - - $applicationId = (string) $applicationId; - $feedId = (string) $feedId; - // $data = static::getRequest()->get('/feed/' . $id); - // todo: load over API - - $data = new Params([ - 'id' => $feedId, - 'name' => 'Name-' . $feedId, - 'description' => 'description' . $feedId, - 'channel' => 'channel' . $feedId, - 'createStamp' => time(), - ]); - return new Feed($data); - } - - /** - * @param string $applicationId - * @param array $data - * @return string - */ - public function createFeed($applicationId, array $data) { - $result = static::getRequest()->post('/application/' . $applicationId . '/feed', $data); - // todo: also return channel Id - return $result['id']; - } - - /** - * @param string $applicationId - * @param string $feedId - * @param array $data - */ - public function updateFeed($applicationId, $feedId, array $data) { - static::getRequest()->put('/application/' . $applicationId . '/feed/' . $feedId, $data); - } - - /** - * @param string $applicationId - * @param string $feedId - */ - public function deleteFeed($applicationId, $feedId) { - static::getRequest()->delete('/application/' . $applicationId . '/feed/' . $feedId); - } - - /** - * @param string $feedId - * @param array $data - * @return string - */ - public function createEntry($feedId, array $data) { - $result = static::getRequest()->post('/feed/' . $feedId . '/entry', $data); - return $result['id']; - } - - /** - * @param string $feedId - * @param string $entryId - * @param array $data - */ - public function updateEntry($feedId, $entryId, array $data) { - static::getRequest()->put('/feed/' . $feedId . '/entry/' . $entryId, $data); - } - - /** - * @param string $feedId - * @param string $entryId - */ - public function deleteEntry($feedId, $entryId) { - static::getRequest()->delete('/feed/' . $feedId . '/entry/' . $entryId); - } - - /** - * @param string $token - * @return Token - */ - public function getToken($token) { - $token = (string) $token; - // todo: load over API - - return new Token(new Params(['token' => $token, 'name' => 'Namedjgsfjhsdgfhjsfgdshj', 'createStamp' => time()])); - } - - public function getTokenList() { - // todo: load over API - - $tokenList = []; - for ($i = 0; $i < 5; $i++) { - $tokenList[] = ['token' => 'token' . $i, 'name' => 'Name-' . $i, 'createStamp' => time()]; - } - return new TokenList($tokenList); + $this->application = new ApplicationClient(); + $this->feed = new FeedClient(); + $this->entry = new EntryClient(); + $this->adminUser = new AdminUserClient(); + $this->token = new TokenClient(); } /** @@ -204,7 +55,7 @@ public function getTokenList() { */ public static function getRequest() { if (!static::$_request) { - static::$_request = new Request(static::$_apiId, static::$_apiToken); + static::$_request = new Request(static::$_apiToken); } return static::$_request; } diff --git a/source/Feedlabs/Feedify/Client/AdminUserClient.php b/source/Feedlabs/Feedify/Client/AdminUserClient.php new file mode 100644 index 0000000..530c614 --- /dev/null +++ b/source/Feedlabs/Feedify/Client/AdminUserClient.php @@ -0,0 +1,95 @@ + 'adminUserId-' . $i, + 'email' => 'test' . $i . '@example.com', + 'roleList' => [['id' => 1, 'name' => 'superAdmin']], + 'createStamp' => time(), + ]; + } + + return new AdminUserList($adminUserList); + } + + /** + * @param string $adminUserId + * @return AdminUser + */ + public function get($adminUserId) { + $adminUserId = (string) $adminUserId; + // todo: load over API + $data = [ + 'id' => $adminUserId, + 'email' => 'test@example.com', + 'roleList' => [['id' => 1, 'name' => 'superAdmin']], + 'createStamp' => time(), + ]; + + return new AdminUser(new Params($data)); + } + + /** + * @param string $email + * @param array|null $roleList + * @return AdminUser + */ + public function create($email, array $roleList = null) { + $email = (string) $email; + $roleList = (array) $roleList; + // todo: load over API + $data = [ + 'id' => 'ID1234', + 'email' => $email, + 'roleList' => [['id' => 1, 'name' => 'superAdmin']], + 'createStamp' => time(), + ]; + + return new AdminUser(new Params($data)); + } + + /** + * @param string $adminUserId + * @param string $email + * @param array|null $roleList + * @return AdminUser + */ + public function update($adminUserId, $email, array $roleList = null) { + $adminUserId = (string) $adminUserId; + $email = (string) $email; + $roleList = (array) $roleList; + // todo: load over API + $data = [ + 'id' => 'ID1234', + 'email' => $email, + 'roleList' => [['id' => 1, 'name' => 'superAdmin']], + 'createStamp' => time(), + ]; + + return new AdminUser(new Params($data)); + } + + public function delete($adminUserId) { + $adminUserId = (string) $adminUserId; + // todo: load over API + // todo: what to return??? + } +} diff --git a/source/Feedlabs/Feedify/Client/ApplicationClient.php b/source/Feedlabs/Feedify/Client/ApplicationClient.php new file mode 100644 index 0000000..9ec1642 --- /dev/null +++ b/source/Feedlabs/Feedify/Client/ApplicationClient.php @@ -0,0 +1,102 @@ + 'id' . $i, + 'name' => 'Name-' . $i, + 'createStamp' => time(), + ]; + } + + return new ApplicationList($applicationList); + } + + /** + * @param string $applicationId + * @return Application + */ + public function get($applicationId) { + $applicationId = (string) $applicationId; + + // todo: load over API + + $data = new Params([ + 'id' => $applicationId, + 'name' => 'Name-' . $applicationId, + 'description' => 'description' . $applicationId, + 'createStamp' => time(), + ]); + return new Application($data); + } + + /** + * @param string $name + * @param string|null $description + * @return Application + */ + public function create($name, $description = null) { + $name = (string) $name; + $description = (string) $description; + + // todo: send / load over API + + $data = new Params([ + 'id' => 'ABC123', + 'name' => 'Name-ABC123', + 'description' => 'description-ABC123', + 'createStamp' => time(), + ]); + return new Application($data); + } + + /** + * @param string $applicationId + * @param string $name + * @param string|null $description + * @return Application + */ + public function update($applicationId, $name, $description = null) { + $applicationId = (string) $applicationId; + $name = (string) $name; + $description = (string) $description; + + // todo: send / load over API + + $data = new Params([ + 'id' => $applicationId, + 'name' => 'Name-' . $applicationId, + 'description' => 'description' . $applicationId, + 'createStamp' => time(), + ]); + return new Application($data); + } + + /** + * @param string $applicationId + */ + public function delete($applicationId) { + $applicationId = (string) $applicationId; + + // todo: send / load over API + // todo: think about what to return + } +} diff --git a/source/Feedlabs/Feedify/Client/EntryClient.php b/source/Feedlabs/Feedify/Client/EntryClient.php new file mode 100644 index 0000000..797e869 --- /dev/null +++ b/source/Feedlabs/Feedify/Client/EntryClient.php @@ -0,0 +1,30 @@ +get('/feed'); + // foreach ($result as $feedData) { + // $applicationList[] = new Feed($feedData['Id'], array('Data' => $feedData['Data'])); + // } + // todo: load over API + + $feedList = []; + for ($i = 0; $i < 5; $i++) { + $feedList[] = [ + 'id' => 'id' . $i, + 'name' => 'Name-' . $i, + 'description' => 'description-' . $i, + 'channel' => 'channel-' . $i, + 'createStamp' => time(), + ]; + } + + return new FeedList($feedList); + } + + /** + * @param string $applicationId + * @param string $feedId + * @return Feed + */ + public function get($applicationId, $feedId) { + // $feedId = (string) $feedId; + // $data = static::getRequest()->get('/application/' . $applicationId . '/feed/' . $feedId); + // return new Feed(new Params($data)); + + $applicationId = (string) $applicationId; + $feedId = (string) $feedId; + // $data = static::getRequest()->get('/feed/' . $id); + // todo: load over API + + $data = new Params([ + 'id' => $feedId, + 'name' => 'Name-' . $feedId, + 'description' => 'description' . $feedId, + 'channel' => 'channel' . $feedId, + 'createStamp' => time(), + ]); + return new Feed($data); + } + + public function create($applicationId, $name, $description = null, $tagList = null) { + // todo: add + } + + public function update($applicationId, $feedId, $name, $description = null, $tagList = null) { + // todo: add + } + + /** + * @param string $applicationId + * @param string $feedId + */ + public function delete($applicationId, $feedId) { + // todo: add + } +} diff --git a/source/Feedlabs/Feedify/Client/TokenClient.php b/source/Feedlabs/Feedify/Client/TokenClient.php new file mode 100644 index 0000000..d087186 --- /dev/null +++ b/source/Feedlabs/Feedify/Client/TokenClient.php @@ -0,0 +1,51 @@ + 'token' . $i, 'name' => 'Name-' . $i, 'createStamp' => time()]; + } + + return new TokenList($tokenList); + } + + /** + * @param string $token + * @return Token + */ + public function get($token) { + $token = (string) $token; + // todo: load over API + $data = ['token' => $token, 'name' => 'Namedjgsfjhsdgfhjsfgdshj', 'createStamp' => time()]; + + return new Token(new Params($data)); + } + + public function create($name) { + // todo: load over API + $data = ['token' => 'tokenHASH', 'name' => $name, 'createStamp' => time()]; + + return new Token(new Params($data)); + } + + public function delete($token) { + // todo: add + } +} diff --git a/source/Feedlabs/Feedify/Request.php b/source/Feedlabs/Feedify/Request.php index 0367cf9..d08b2ad 100644 --- a/source/Feedlabs/Feedify/Request.php +++ b/source/Feedlabs/Feedify/Request.php @@ -16,18 +16,13 @@ class Request { CONST METHOD_DELETE = 4; CONST API_URL = 'http://www.feed.dev:10111'; - /** @var string */ - protected $_apiId; - /** @var string */ protected $_apiToken; /** - * @param string $apiId * @param string $apiToken */ - public function __construct($apiId, $apiToken) { - $this->_apiId = $apiId; + public function __construct($apiToken) { $this->_apiToken = $apiToken; } @@ -54,8 +49,8 @@ public function post($path, $data) { * @return array */ public function put($path, $data) { - return Helper::decode($this->_send(self::METHOD_PUT, $path, $data)); - } + return Helper::decode($this->_send(self::METHOD_PUT, $path, $data)); + } /** * @param $path @@ -65,13 +60,6 @@ public function delete($path) { return Helper::decode($this->_send(self::METHOD_DELETE, $path)); } - /** - * @return string - */ - public function getApiId() { - return $this->_apiId; - } - /** * @return string */ @@ -96,8 +84,9 @@ protected function _send($method, $path, $data = null) { curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); - $header = array(); - $header[] = 'Content-type:application/json'; + $header = []; + $header[] = 'apiToken: ' . $this->getApiToken(); + $header[] = 'Content-type: application/json'; switch ($method) { case self::METHOD_GET: diff --git a/source/Feedlabs/Feedify/Resource/AbstractElement.php b/source/Feedlabs/Feedify/Resource/AbstractElement.php index 7ac2144..1ed5411 100644 --- a/source/Feedlabs/Feedify/Resource/AbstractElement.php +++ b/source/Feedlabs/Feedify/Resource/AbstractElement.php @@ -2,10 +2,12 @@ namespace Feedlabs\Feedify\Resource; +use Feedlabs\Feedify\Client; use Feedlabs\Feedify\Params; +use Feedlabs\Feedify\Request; /** - * Class AbstractResource + * Class AbstractElement * @package Feedlabs\Feedify\Resource */ abstract class AbstractElement { @@ -13,11 +15,15 @@ abstract class AbstractElement { /** @var string */ protected $_id; + /** @var int */ + protected $_createStamp; + /** * @param Params $data */ public function __construct(Params $data) { $this->_id = $data->getString('id'); + $this->_createStamp = ($data->has('createStamp')) ? $data->getInt('createStamp') : null; } /** @@ -26,4 +32,23 @@ public function __construct(Params $data) { public function getId() { return $this->_id; } + + /** + * @return int + */ + public function getCreated() { + if (null === $this->_createStamp) { + $this->_load(); + } + return $this->_createStamp; + } + + /** + * @return Request + */ + protected function _getRequest() { + return Client::getRequest(); + } + + abstract protected function _load(); } diff --git a/source/Feedlabs/Feedify/Resource/AdminUser.php b/source/Feedlabs/Feedify/Resource/AdminUser.php new file mode 100644 index 0000000..3d959f1 --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/AdminUser.php @@ -0,0 +1,65 @@ +_email = ($data->has('email')) ? $data->getString('email') : null; + $this->_roleList = ($data->has('roleList')) ? $data->getArray('roleList') : null; + } + + /** + * @return string + */ + public function getEmail() { + if (null === $this->_email) { + $this->_load(); + } + return $this->_email; + } + + /** + * @return array[] + */ + public function getRoleLIst() { + if (null === $this->_roleList) { + $this->_load(); + } + return $this->_roleList; + } + + public function delete() { + // todo: add delete + } + + protected function _load() { + // todo: get userAdmin infos over API + $data = new Params([ + 'email' => 'test@example.com', + 'roleList' => [['id' => 1, 'name' => 'superAdmin'], ['id' => 2, 'name' => 'admin']], + 'createStamp' => time(), + ]); + // ////////////////////////////////////// + + $this->_email = $data->getString('email'); + $this->_roleList = $data->getArray('roleList'); + $this->_createStamp = $data->getInt('createStamp'); + } +} diff --git a/source/Feedlabs/Feedify/Resource/AdminUserList.php b/source/Feedlabs/Feedify/Resource/AdminUserList.php new file mode 100644 index 0000000..52fe5c8 --- /dev/null +++ b/source/Feedlabs/Feedify/Resource/AdminUserList.php @@ -0,0 +1,16 @@ +_name = $data->getString('name'); - $this->_createStamp = $data->getInt('createStamp'); + $this->_name = ($data->has('name')) ? $data->getString('name') : null; $this->_description = ($data->has('description')) ? $data->getString('description') : null; + $this->_feedList = ($data->has('feedList')) ? $data->get('feedList') : null; } /** * @return string */ public function getName() { + if (null === $this->_name) { + $this->_load(); + } return $this->_name; } @@ -42,38 +44,47 @@ public function getName() { * @return string|null */ public function getDescription() { + if (null === $this->_description) { + $this->_load(); + } return $this->_description; } - /** - * @return int - */ - public function getCreated() { - return $this->_createStamp; - } - public function getFeedList() { - // todo: load over API - $feedList = []; - for ($i = 0; $i < 3; $i++) { - $feedList[] = ['id' => 'id' . $i, 'name' => 'Name-' . $i, 'description' => 'description-' . $i, 'createStamp' => time(), 'channel' => 'channel' . $i]; + if (null === $this->_feedList) { + // todo: load over API + $feedList = []; + for ($i = 0; $i < 3; $i++) { + $feedList[] = [ + 'id' => 'id' . $i, + 'name' => 'Name-' . $i, + 'description' => 'description-' . $i, + 'channel' => 'channel' . $i, + 'createStamp' => time(), + ]; + } + // ////////////////////////////////////// + $this->_feedList = new FeedList($feedList); } - return new FeedList($feedList); + return $this->_feedList; } - /** - * @return array - */ public function delete() { // todo: add delete - // return $this->_getRequest()->delete('/feed/' . $this->getId()); } - /** - * @return Request - */ - protected function _getRequest() { - return Client::getRequest(); + protected function _load() { + // todo: get application infos over API + $data = new Params([ + 'name' => 'Name-ABC123', + 'description' => 'description-ABC123', + 'createStamp' => time(), + ]); + // ////////////////////////////////////// + + $this->_name = $data->getString('name'); + $this->_description = $data->getString('description'); + $this->_createStamp = $data->getInt('createStamp'); } } diff --git a/source/Feedlabs/Feedify/Resource/Entry.php b/source/Feedlabs/Feedify/Resource/Entry.php index 587f8b1..00dfb73 100644 --- a/source/Feedlabs/Feedify/Resource/Entry.php +++ b/source/Feedlabs/Feedify/Resource/Entry.php @@ -12,7 +12,7 @@ */ class Entry extends AbstractElement { - /** @var array */ + /** @var string */ protected $_data; /** @@ -20,20 +20,28 @@ class Entry extends AbstractElement { */ public function __construct(Params $data) { parent::__construct($data); - $this->_data = $data->getString('data'); + $this->_data = ($data->has('data')) ? $data->getString('data') : null; } /** - * @return array + * @return string */ public function getData() { + if (null === $this->_data) { + $this->_load(); + } return $this->_data; } - /** - * @return Request - */ - protected function _getRequest() { - return Client::getRequest(); + protected function _load() { + // todo: get feed infos over API + $data = new Params([ + 'data' => 'description123', + 'createStamp' => time(), + ]); + // ////////////////////////////////////// + + $this->_data = $data->getString('data'); + $this->_createStamp = $data->getInt('createStamp'); } } diff --git a/source/Feedlabs/Feedify/Resource/Feed.php b/source/Feedlabs/Feedify/Resource/Feed.php index 6ad6054..73c18e0 100644 --- a/source/Feedlabs/Feedify/Resource/Feed.php +++ b/source/Feedlabs/Feedify/Resource/Feed.php @@ -3,7 +3,6 @@ namespace Feedlabs\Feedify\Resource; use Feedlabs\Feedify\Client; -use Feedlabs\Feedify\Request; use Feedlabs\Feedify\Params; /** @@ -16,74 +15,86 @@ class Feed extends AbstractElement { protected $_name; /** @var string */ - protected $_channel; + protected $_channelId; - /** @var string|null */ + /** @var string */ protected $_description; - /** @var int */ - protected $_createStamp; + /** @var EntryList */ + protected $_entryList; /** * @param Params $data */ public function __construct(Params $data) { parent::__construct($data); - $this->_name = $data->getString('name'); - $this->_createStamp = $data->getInt('createStamp'); - $this->_channel = $data->getString('channel'); + $this->_name = ($data->has('name')) ? $data->getString('name') : null; $this->_description = ($data->has('description')) ? $data->getString('description') : null; + $this->_channelId = ($data->has('channelId')) ? $data->getString('channelId') : null; + $this->_entryList = ($data->has('entryList')) ? $data->get('entryList') : null; } /** * @return string */ public function getName() { + if (null === $this->_name) { + $this->_load(); + } return $this->_name; } /** - * @return string|null + * @return string */ public function getDescription() { + if (null === $this->_description) { + $this->_load(); + } return $this->_description; } /** * @return string */ - public function getChannel() { - return $this->_channel; - } - - /** - * @return int - */ - public function getCreated() { - return $this->_createStamp; + public function getChannelId() { + if (null === $this->_channelId) { + $this->_load(); + } + return $this->_channelId; } public function getEntryList() { - // todo: load over API - $entryList = []; - for ($i = 0; $i < 3; $i++) { - $entryList[] = ['id' => 'id' . $i, 'data' => 'Data-' . $i, 'createStamp' => time()]; + if (null === $this->_entryList) { + // todo: load over API + $entryList = []; + for ($i = 0; $i < 3; $i++) { + $entryList[] = ['id' => 'id' . $i, 'data' => 'Data-' . $i, 'createStamp' => time()]; + } + // ////////////////////////////////////// + $this->_entryList = new EntryList($entryList); } - return new EntryList($entryList); + return $this->_entryList; } - /** - * @return array - */ public function delete() { - return $this->_getRequest()->delete('/feed/' . $this->getId()); + // todo: add delete } - /** - * @return Request - */ - protected function _getRequest() { - return Client::getRequest(); + protected function _load() { + // todo: get feed infos over API + $data = new Params([ + 'name' => 'Feed123', + 'description' => 'description123', + 'channelId' => 'channelId-123', + 'createStamp' => time(), + ]); + // ////////////////////////////////////// + + $this->_name = $data->getString('name'); + $this->_description = $data->getString('description'); + $this->_channelId = $data->getString('channelId'); + $this->_createStamp = $data->getInt('createStamp'); } } diff --git a/tests/source/Feedlabs/Tests/Feedify/ClientTest.php b/tests/source/Feedlabs/Tests/Feedify/ClientTest.php index c7fa5aa..01255b0 100644 --- a/tests/source/Feedlabs/Tests/Feedify/ClientTest.php +++ b/tests/source/Feedlabs/Tests/Feedify/ClientTest.php @@ -27,21 +27,6 @@ public function testGetFeed() { $this->assertSame($data, $feed->getData()); } - public function testGetFeedList() { - } - - public function testCreateFeed() { - } - - public function testUpdateFeed() { - } - - public function testDeleteFeed() { - } - - public function testCreateEntry() { - } - public function testGetRequest() { $id = 'foo123'; $token = 'bar123'; diff --git a/tests/source/Feedlabs/Tests/Feedify/RequestTest.php b/tests/source/Feedlabs/Tests/Feedify/RequestTest.php index 1f179ca..4bc7df6 100644 --- a/tests/source/Feedlabs/Tests/Feedify/RequestTest.php +++ b/tests/source/Feedlabs/Tests/Feedify/RequestTest.php @@ -8,9 +8,7 @@ class RequestTest extends TestCase { public function testConstruct() { - $request = new Request('foo', 'bar'); - - $this->assertSame('foo', $request->getApiId()); + $request = new Request('bar'); $this->assertSame('bar', $request->getApiToken()); } @@ -29,6 +27,6 @@ public function testPost() { $expected = ['id' => '111']; /** @var Request $request */ - $this->assertSame($expected, $request->get('www.example.com')); + $this->assertSame($expected, $request->post('www.example.com', $expected)); } }