From 3178bd7ea4f346180a4289b076f247f4007144bd Mon Sep 17 00:00:00 2001 From: Sven Arild Helleland Date: Thu, 7 May 2015 15:11:20 +0200 Subject: [PATCH] Included more of the TP functionality, and also added conversion of normal dev/sql conditions into TP conditions --- src/Request.php | 76 +++++++++++++++++++++++++++++++++++++++---- tests/RequestTest.php | 20 +++++++++++- 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/src/Request.php b/src/Request.php index 12ef504..07057c3 100644 --- a/src/Request.php +++ b/src/Request.php @@ -26,7 +26,7 @@ class Request public function __construct($api_base_url, $username = null, $password = null) { if ($api_base_url == '') { - throw new Exception('Undefined API base URL'); + throw new \Exception('Undefined API base URL'); } else { $this->api_base_url = $api_base_url; } @@ -34,6 +34,22 @@ public function __construct($api_base_url, $username = null, $password = null) $this->password = $password; } + /** + * Allow the use of normal Dev and SQL commands when formatting the query condition instead of TP specific ones + * This makes it easier to read and understand the queries + * @see http://dev.targetprocess.com/rest/response_format#filtering + * + * @param string $value The condition + * @return string + */ + private function processTags($value) + { + $from = array('=', '!=', '<>', '>', '<', '>=', '<=', ' is false', ' is true', " like '"); + $to = array(' eq ', ' ne ', ' ne ', ' gt ', ' lt ', ' gte ', ' lte ', " eq 'false'", " eq 'true'", " contains '"); + + return str_ireplace($from, $to, $value); + } + /** * Generates the condition string that will be put in the WHERE part. * Based on Yii query builder. @@ -44,7 +60,7 @@ public function __construct($api_base_url, $username = null, $password = null) private function processConditions($conditions) { if (!is_array($conditions)) { - return $conditions; + return $this->processTags($conditions); } elseif ($conditions === array()) { return ''; } @@ -81,7 +97,7 @@ private function processConditions($conditions) $values[$i] = (string) $value; } } - return $column . ' ' . $operator . ' (' . implode(',', $values) . ')'; + return $this->processTags($column . ' ' . $operator . ' (' . implode(',', $values) . ')'); } throw new \Exception('Unknown operator ' . $operator); } @@ -90,7 +106,7 @@ private function processConditions($conditions) * Set collection name. * * @param string $name Collection name - * @return \TpReport + * @return \TpReport\Request */ public function collection($name) { @@ -104,7 +120,7 @@ public function collection($name) * Always overwrites previews where conditions. * * @param string|array $conditions the conditions that should be put in the WHERE part. String or array in form of Polish prefix notation. - * @return \TpReport + * @return \TpReport\Request */ public function where($conditions) { @@ -133,7 +149,7 @@ private function addArrayParams($name, $values) * You can get items with specified fields only. * * @param array $fields Fields to be included - * @return \TpReport + * @return \TpReport\Request */ public function inc($fields) { @@ -146,7 +162,7 @@ public function inc($fields) * @see http://dev.targetprocess.com/rest/response_format#paging * * @param int $limit - * @return \TpReport + * @return \TpReport\Request */ public function take($limit) { @@ -156,11 +172,57 @@ public function take($limit) return $this; } + /** + * Set the amount of items to skip. + * @see http://dev.targetprocess.com/rest/response_format#paging + * + * @param int $limit + * @return \TpReport\Request + */ + public function skip($limit) + { + if (is_int($limit)) { + $this->params['skip'] = $limit; + } + return $this; + } + + /** + * Set the order the content is sorted + * @see http://dev.targetprocess.com/rest/response_format#sorting + * + * @param string $column The column we should sort by + * @return \TpReport\Request + */ + public function orderByAsc($column) + { + + $this->params['orderBy'] = $column; + + return $this; + } + + /** + * Set the order the content is sorted + * @see http://dev.targetprocess.com/rest/response_format#sorting + * + * @param string $column The column we should sort by + * @return \TpReport\Request + */ + public function orderByDesc($column) + { + + $this->params['orderByDesc'] = $column; + + return $this; + } + /** * Final URL buliding. * @see http://dev.targetprocess.com/rest/response_format#filtering * * @return string + * @throws \Exception */ public function getUrl() { diff --git a/tests/RequestTest.php b/tests/RequestTest.php index b48fdf1..f43be05 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -99,7 +99,25 @@ public function testTake() $url = $this->request->collection('Bugs')->take(666)->getUrl(); $this->assertEquals($this->api_base_url . '/Bugs?take=666', $url); } - + + public function testSkipRecords() + { + $url = $this->request->collection('Bugs')->take(25)->skip(40)->getUrl(); + $this->assertEquals($this->api_base_url . '/Bugs?take=25&skip=40', $url); + } + + public function testOrderByAsc() + { + $url = $this->request->collection('Bugs')->orderByAsc('Id')->getUrl(); + $this->assertEquals($this->api_base_url . '/Bugs?orderBy=Id', $url); + } + + public function testOrderByDesc() + { + $url = $this->request->collection('Bugs')->orderByDesc('Id')->getUrl(); + $this->assertEquals($this->api_base_url . '/Bugs?orderByDesc=Id', $url); + } + /** * * @param array $include