Skip to content

Commit

Permalink
Implement Sort class and tests but not for multiple fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Jan 30, 2016
1 parent b603485 commit 569a2c1
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 42 deletions.
20 changes: 17 additions & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public static function delete($id, $additionalAttributes = null)

/**
* Get filterable attribute keys
* **MAY** be overwritten
* @return array
*/
public static function getFilterable()
Expand All @@ -310,25 +311,38 @@ public static function getFilterable()

/**
* Get attribute keys that can be updated using PATCH
* **MAY** be overwritten
* @return string[]
*/
public static function getMutable()
{
return [];
}

/**
* Get attribute keys that allowed to be used for sort
* **MAY** be overwritten
* @return string[]
* @since 1.0.0
*/
public static function getSortable()
{
return [];
}

/**
* Get sort attributes and default
* **MAY** be overwritten
* @return object Returns an object with attribute `attributes` containing
* an string[] with allowed sort attributes
* and attribute `default` a string|null having the value of default, boolean `ascending`
* sorting attribute
*/
public static function getSort()
{
return (new Sort(
self::$table
))->setDefault(null);
return new Sort(
static::getTable()
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static function getIncludedData(
return [];
}

//iterate through all primary data
//iterate all primary data

//if a single resource convert it to array
//so it can be iterated in the same way
Expand Down
66 changes: 34 additions & 32 deletions src/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Sort

/**
* @var
* @deprecated
*/
public $default;

Expand Down Expand Up @@ -77,48 +78,49 @@ public function setDefault($default)
* @param object $parameters Request parameters
* @param string $modelClass
* @return Sort
* @todo rewrite
* @throws RequestException
*/
public static function parseFromParameters($parameters, $modelClass)
{
$modelSort = $modelClass::getSort();
$sortableAttributes = $modelClass::getSortable();

$sort = null;
$sort = $modelClass::getSort();

if ($modelSort->default !== null) {
$sort = new Sort(
$modelClass::getTable(),
[],
$modelSort->ascending
);

//Don't accept arrays
if (isset($parameters->sort)) {
if (!is_string($parameters->sort)) {
throw new RequestException(
'String expected for sort'
);
}
//Don't accept arrays

$validateExpression = sprintf(
'/^(?P<descending>\-)?(?P<attribute>%s)$/',
implode('|', $modelSort->attributes)
if (isset($parameters->sort)) {
if (!is_string($parameters->sort)) {
throw new RequestException(
'String expected for sort'
);
}

if (!!preg_match($validateExpression, $parameters->sort, $matches)) {
$sort->attribute = $matches['attribute'];
$sort->ascending = (
isset($matches['descending']) && $matches['descending']
? false
: true
);
if (empty($sortableAttributes)) {
throw new RequestException('Not sortable attributes for this resource model');
}

} else {
throw new RequestException(
'Invalid value for sort'
);
}
//Check attribute is in resource model's sortable and parse if is descending
$validateExpression = sprintf(
'/^(?P<descending>\-)?(?P<attribute>%s)$/',
implode('|', array_map(
'preg_quote',
$sortableAttributes,
['/']
))
);

if (!!preg_match($validateExpression, $parameters->sort, $matches)) {
$sort->attribute = $matches['attribute'];
$sort->ascending = (
isset($matches['descending']) && $matches['descending']
? false
: true
);

} else {
throw new RequestException(
'Invalid value for sort'
);
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/APP/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class Article extends \Phramework\JSONAPI\Model
protected static $endpoint = 'article';
protected static $table = 'article';

/**
* @return string[]
*/
public static function getSortable()
{
return ['id'];
}

/**
* @return ValidationModel
*/
Expand Down
21 changes: 18 additions & 3 deletions tests/APP/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,22 @@ class Tag extends \Phramework\JSONAPI\Model
protected static $endpoint = 'tag';
protected static $table = 'tag';

public static function get()
/**
* @param Page|null $page *[Optional]*
* @param Filter|null $filter *[Optional]*
* @param Sort|null $sort *[Optional]*
* @param Fields|null $fields *[Optional]*
* @param mixed ...$additionalParameters *[Optional]*
* @throws NotImplementedException
* @return Resource[]
* @todo apply Page, Filter and Sort rules to arrays as helper utility
*/
public static function get(
Page $page = null,
Filter $filter = null,
Sort $sort = null,
Fields $fields = null,
...$additionalParameters)
{
return self::collection(
Database::executeAndFetchAll(
Expand All @@ -42,7 +57,7 @@ public static function get()
)
);
}

/**
public static function getById($id)
{
return self::resource(
Expand All @@ -54,7 +69,7 @@ public static function getById($id)
[$id]
)
);
}
}**/

/**
* post article-tag relationship
Expand Down
20 changes: 18 additions & 2 deletions tests/APP/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,22 @@ class User extends \Phramework\JSONAPI\Model
protected static $endpoint = 'user';
protected static $table = 'user';

public static function get()
/**
* @param Page|null $page *[Optional]*
* @param Filter|null $filter *[Optional]*
* @param Sort|null $sort *[Optional]*
* @param Fields|null $fields *[Optional]*
* @param mixed ...$additionalParameters *[Optional]*
* @throws NotImplementedException
* @return Resource[]
* @todo apply Page, Filter and Sort rules to arrays as helper utility
*/
public static function get(
Page $page = null,
Filter $filter = null,
Sort $sort = null,
Fields $fields = null,
...$additionalParameters)
{
return self::collection(
Database::executeAndFetchAll(
Expand All @@ -43,6 +58,7 @@ public static function get()
);
}

/**
public static function getById($id)
{
return self::resource(
Expand All @@ -54,5 +70,5 @@ public static function getById($id)
[$id]
)
);
}
}**/
}
104 changes: 103 additions & 1 deletion tests/src/SortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Phramework\JSONAPI;

use Phramework\JSONAPI\APP\Models\Article;
use Phramework\JSONAPI\APP\Models\Tag;

/**
* @coversDefaultClass Phramework\JSONAPI\Sort
Expand Down Expand Up @@ -62,6 +63,107 @@ public function testParseFromParametersEmpty()
Article::class
);

$this->assertNull($sort);
$this->assertEquals(Article::getSort(), $sort);
}

/**
* @covers ::parseFromParameters
*/
public function testParseFromParameters()
{
$parameters = (object) [
'sort' => '-id'
];

$sort = Sort::parseFromParameters(
$parameters,
Article::class
);

$this->assertInstanceOf(
Sort::class,
$sort
);

$this->assertSame(Article::getTable(), $sort->table);
$this->assertSame('id', $sort->attribute);
$this->assertFalse($sort->ascending);

//Test ascending
$parameters = (object) [
'sort' => 'id'
];

$sort = Sort::parseFromParameters(
$parameters,
Article::class
);

$this->assertSame('id', $sort->attribute);
$this->assertTrue($sort->ascending);
}

/**
* @covers ::parseFromParameters
* @expectedException \Phramework\Exceptions\RequestException
*/
public function testParseFromParametersFailureNotString()
{
$parameters = (object) [
'sort' => ['id']
];

$sort = Sort::parseFromParameters(
$parameters,
Article::class
);
}

/**
* @covers ::parseFromParameters
* @expectedException \Phramework\Exceptions\RequestException
*/
public function testParseFromParametersFailureParseExpression()
{
$parameters = (object) [
'sort' => '--id'
];

$sort = Sort::parseFromParameters(
$parameters,
Article::class
);
}

/**
* @covers ::parseFromParameters
* @expectedException \Phramework\Exceptions\RequestException
*/
public function testParseFromParametersFailureNotSortable()
{
$parameters = (object) [
'sort' => 'meta'
];

$sort = Sort::parseFromParameters(
$parameters,
Article::class
);
}

/**
* @covers ::parseFromParameters
* @expectedException \Phramework\Exceptions\RequestException
*/
public function testParseFromParametersFailureNoSortableAttributes()
{
$parameters = (object) [
'sort' => 'id'
];

$sort = Sort::parseFromParameters(
$parameters,
Tag::class
);
}
}

0 comments on commit 569a2c1

Please sign in to comment.