Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included more of the TP functionality #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
76 changes: 69 additions & 7 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,30 @@ 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;
}
$this->username = $username;
$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.
Expand All @@ -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 '';
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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()
{
Expand Down
20 changes: 19 additions & 1 deletion tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down