Skip to content

Commit

Permalink
Add relationships with flag "include by default" to include directive
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Feb 9, 2016
1 parent dfed9fb commit 7fe7732
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
15 changes: 13 additions & 2 deletions src/Controller/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
namespace Phramework\JSONAPI\Controller;

use Phramework\JSONAPI\Relationship;
use \Phramework\Models\Request;
use \Phramework\Exceptions\RequestException;

Expand Down Expand Up @@ -112,10 +113,11 @@ public static function viewData(

/**
* Extract included related resources from parameters
* @param object $parameters Request parameters
* @param object $parameters Request parameters
* @param string|null $modelClass If not null, will add additional include by default from resource model's relationships
* @return string[]
*/
protected static function getRequestInclude($parameters)
protected static function getRequestInclude($parameters, $modelClass = null)
{
//work with arrays
if (!is_object($parameters) && is_array($parameters)) {
Expand All @@ -133,6 +135,15 @@ protected static function getRequestInclude($parameters)
$include[] = trim($i);
}

if ($modelClass !== null) {
//Add additional include by default from resource model's relationships
foreach ($modelClass::getRelationships() as $relationshipKey => $relationship) {
if (($relationship->flags & Relationship::FLAG_INCLUDE_BY_DEFAULT) != 0) {
$include[] = $include;
}
}
}

return array_unique($include);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Controller/GET.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Phramework\JSONAPI\Filter;
use Phramework\JSONAPI\Fields;
use Phramework\JSONAPI\Page;
use Phramework\JSONAPI\Relationship;
use Phramework\JSONAPI\Sort;
use Phramework\JSONAPI\Model;
use \Phramework\Models\Request;
Expand Down Expand Up @@ -53,6 +54,7 @@ abstract class GET extends \Phramework\JSONAPI\Controller\GETById
* @throws \Exception
* @throws RequestException
* @return boolean
* @todo Force parsing of relationship data when included
*/
protected static function handleGET(
$parameters,
Expand All @@ -65,6 +67,8 @@ protected static function handleGET(
$sort = $modelClass::parseSort($parameters);
$fields = $modelClass::parseFields($parameters);

$requestInclude = static::getRequestInclude($parameters, $modelClass);

$data = $modelClass::get(
$page,
$filter,
Expand All @@ -73,8 +77,6 @@ protected static function handleGET(
...$primaryDataParameters
);

$requestInclude = static::getRequestInclude($parameters);

//Get included data
$includedData = $modelClass::getIncludedData(
$data,
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/GETById.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ abstract class GETById extends \Phramework\JSONAPI\Controller\Relationships
* additional argument primary data's relationships are requiring
* @uses model's `getById` method to fetch resource
* @return boolean
* @todo Force parsing of relationship data when included
*/
protected static function handleGETByid(
protected static function handleGETById(
$parameters,
$id,
$modelClass,
Expand All @@ -60,6 +61,8 @@ protected static function handleGETByid(

$fields = $modelClass::parseFields($parameters);

$requestInclude = static::getRequestInclude($parameters, $modelClass);

$data = $modelClass::getById(
$id,
$fields,
Expand All @@ -69,8 +72,6 @@ protected static function handleGETByid(
//Check if resource exists
static::exists($data);

$requestInclude = static::getRequestInclude($parameters);

$includedData = $modelClass::getIncludedData(
$data,
$requestInclude,
Expand Down
33 changes: 7 additions & 26 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,23 @@
*/
abstract class Model
{
/**
* Model's method prefix, following by ucfirst(type of resource)
* for example `getRelationshipByInput_template` if is was `input_template`
* $resource->id,
* $relationshipKey,
* $filter
*/
const GET_RELATIONSHIP_BY_PREFIX = 'getRelationshipBy';

/**
* Model's method prefix, following by ucfirst(type of resource)
* for example `postRelationshipByInput_template` if is was `input_template`
*/
const POST_RELATIONSHIP_BY_PREFIX = 'postRelationshipBy';

/**
* Model's method prefix
* @deprecated
*/
const GET_BY_PREFIX = 'getBy';

/**
* Resource's type, used to describe resource objects that share
* common attributes and relationships
* common attributes and relationships.
* **MUST** be overwritten
* @var string
*/
protected static $type = null;

/**
* Resource's table name
* Resource's table name.
* **MAY** be overwritten, default is null (no database)
* @var string|null
*/
protected static $table = null;

/**
* Resource's table's schema name
* Resource's table's schema name.
* **MAY** be overwritten, default is null (no schema)
* @var string|null
*/
Expand All @@ -79,18 +58,19 @@ abstract class Model
protected static $idAttribute = 'id';

/**
* Resource's endpoint, used for access by external request, usually it the same as type
* Resource's endpoint, used for access by external request, usually it the same as type.
* **MUST** be overwritten
* @var string
*/
protected static $endpoint = null;

/**
* Records's type casting schema for database records
* Records's type casting schema for database records.
* **MAY** be overwritten
* Also it can be set to empty array to disable type
* casting for this resource.
* @var array|null
* @deprecated
*/
protected static $cast = null;

Expand All @@ -104,6 +84,7 @@ abstract class Model
* validationModel's attributes to extract the cast schema
* @return array
* @todo Rewrite validationModel's attributes
* @deprecated
*/
public static function getCast()
{
Expand Down
18 changes: 18 additions & 0 deletions src/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,22 @@ public function __get($name)
$name
));
}

/**
* @param string $name
* @param $value
* @return $this
* @throws \Exception
*/
public function __set($name, $value) {
if (in_array($name, ['flag'])) {
$this->{$name} = $value;
return $this;
}

throw new \Exception(sprintf(
'Undefined property via __get(): %s',
$name
));
}
}
2 changes: 1 addition & 1 deletion src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Resource extends \stdClass implements \JsonSerializable
const META_MEMBER = 'attributes-meta';

const PARSE_DEFAULT = Resource::PARSE_ATTRIBUTES | Resource::PARSE_LINKS
| Resource::PARSE_RELATIONSHIP | Resource::PARSE_RELATIONSHIP_LINKS;
| Resource::PARSE_RELATIONSHIP | Resource::PARSE_RELATIONSHIP_LINKS; //15

const PARSE_ATTRIBUTES = 1;
const PARSE_LINKS = 2;
Expand Down

0 comments on commit 7fe7732

Please sign in to comment.