Skip to content

Commit

Permalink
Add getDefaultPage getDefaultFields to model
Browse files Browse the repository at this point in the history
handleGET will output used page object in root meta object
  • Loading branch information
nohponex committed Feb 6, 2016
1 parent 921e56d commit 2aac779
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 56 deletions.
10 changes: 9 additions & 1 deletion src/Controller/GET.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,20 @@ protected static function handleGET(
$relationshipParameters
);

$meta = (object) [
'page' => (
$page === null
? $modelClass::getDefaultPage()
: $page
)
];

return static::viewData(
$data,
(object)[
'self' => $modelClass::getSelfLink()
],
null,
$meta,
(empty($requestInclude) ? null : $includedData)
);
}
Expand Down
27 changes: 26 additions & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,34 @@ public static function getFields()
return [];
}

/**
* Get default fields for this resource model
* **MAY** be overwritten, default is `'table.*']` where table is accessed from this resource model's table
* @return Fields
* @since 1.0.0
*/
public static function getDefaultFields()
{
return new Fields((object) [
static::getType() => [static::getTable() . '.*']
]);
}

/**
* Get default page for this resource model
* **MAY** be overwritten, default is with limit of 250 resource and offset 0
* @return Page
* @since 1.0.0
* @todo make current applied page somehow visible at meta
*/
public static function getDefaultPage()
{
return new Page(250, 0);
}

/**
* Get sort
* **MAY** be overwritten
* **MAY** be overwritten, default is sorting by id attribute ascending
* @return Sort
*/
public static function getSort()
Expand Down
113 changes: 65 additions & 48 deletions src/Model/Directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private static function handleSort($query, $sort = null)
* @param string $query Query
* @param Page|null $page
* @return string Query
* @uses Model::getDefaultPage if page is null
*/
private static function handlePage($query, $page = null)
{
Expand All @@ -140,20 +141,23 @@ private static function handlePage($query, $page = null)
*/
$additionalQuery = [];

if ($page !== null) {
if ($page->limit !== null) {
$additionalQuery[] = sprintf(
'LIMIT %s',
$page->limit
);
}
if ($page === null) {
//Use resource model's default page
$page = static::getDefaultPage();
}

if ($page->offset) {
$additionalQuery[] = sprintf(
'OFFSET %s',
$page->offset
);
}
if ($page->limit !== null) {
$additionalQuery[] = sprintf(
'LIMIT %s',
$page->limit
);
}

if ($page->offset) {
$additionalQuery[] = sprintf(
'OFFSET %s',
$page->offset
);
}

$query = str_replace(
Expand Down Expand Up @@ -191,7 +195,7 @@ function ($key) {
* @param string $query Query
* @param Filter|null $filter
* @param bool $hasWhere *[Optional]* If query already has an WHERE directive, default is true
* @return string uery
* @return string Query
* @throws \Phramework\Exceptions\NotImplementedException
* @throws \Exception
* @todo check if query work both in MySQL and postgreSQL
Expand Down Expand Up @@ -415,51 +419,64 @@ protected static function handleFilter(
* @param Fields|null $fields
* @return string
* @since 1.0.0
* @todo add table prefix
* @uses Model::getDefaultFields if fields is null
*/
private static function handleFields(
$query,
Fields $fields = null
) {
$type = static::getType();

$queryPart = '*';
//$queryPart = '*';

if ($fields !== null) {
$fields->validate(static::class);
if ($fields === null || empty($fields->get($type))) {
//Use resource model's default fields
$fields = static::getDefaultFields();
}

//Get field attributes for this type and force id attribute
$attributes = array_unique(array_merge(
$fields->get($type),
[static::$idAttribute]
));
//if ($fields !== null && !empty($fields->get($type))) {
$fields->validate(static::class);

$escape = function ($input) {
if ($input === '*') {
return $input;
}
return sprintf('"%s"', $input);
};
//Get field attributes for this type and force id attribute
$attributes = array_unique(array_merge(
$fields->get($type),
[] //[static::$idAttribute]
));

/**
* This method will prepare the attributes by prefixing then with "
* - * ->
* - id -> "id"
* - table.id -> "table"."id"
* and glue them with comma separator
*/
$queryPart = implode(
',',
array_map(
function ($attribute) use ($escape) {
return implode(
'.',
array_map($escape, explode('.', $attribute))
);
},
$attributes
)
);
}
/**
* @param string $column
* @return string
*/
$escape = function ($column) {
if ($column === '*') {
return $column;
}

return sprintf('"%s"', $column);
};

/**
* This method will prepare the attributes by prefixing then with "
* - * -> *
* - table.* -> "table".*
* - id -> "id"
* - table.id -> "table"."id"
* and glue them with comma separator
*/
$queryPart = implode(
',',
array_map(
function ($attribute) use ($escape) {
return implode(
'.',
array_map($escape, explode('.', $attribute))
);
},
$attributes
)
);
//}

$query = str_replace(
'{{fields}}',
Expand Down
7 changes: 6 additions & 1 deletion src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @property-read int|null $limit
* @property-read int $offset
*/
class Page
class Page implements \JsonSerializable
{
/**
* @var int
Expand Down Expand Up @@ -115,4 +115,9 @@ public function __get($name)
$name
));
}

public function jsonSerialize()
{
return get_object_vars($this);
}
}
2 changes: 1 addition & 1 deletion tests/APP/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ public static function invokeStaticMethod($className, $methodName, ...$parameter
}
}

Bootstrap::prepare();
//Bootstrap::prepare();
5 changes: 5 additions & 0 deletions tests/src/Controllers/GETTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class GETTest extends \PHPUnit_Framework_TestCase
*/
protected $phramework;

/**
* @var object
*/
protected $parameters;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
Expand Down
8 changes: 4 additions & 4 deletions tests/src/Model/DirectivesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ public function testHandleFields()
);

$pattern = sprintf(
'/^SELECT "%s" FROM "%s"$/',
Tag::getIdAttribute(),
'/^SELECT "%s"\.\* FROM "%s"$/',
Tag::getTable(),
//Tag::getIdAttribute(),
Tag::getTable()
);

Expand All @@ -178,8 +179,7 @@ public function testHandleFields()
);

$pattern = sprintf(
'/^SELECT \*,\s*"%s" FROM "%s"$/',
Tag::getIdAttribute(),
'/^SELECT \*\s*FROM "%s"$/',
Tag::getTable()
);

Expand Down

0 comments on commit 2aac779

Please sign in to comment.