Skip to content

Commit

Permalink
Merge pull request #32 from thanh-taro/add_relationships_data
Browse files Browse the repository at this point in the history
Update code
  • Loading branch information
thanh-taro committed Jun 21, 2016
2 parents 4e8408b + 4fa4e5d commit 7ddab5d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 42 deletions.
29 changes: 19 additions & 10 deletions src/JsonObjects/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ public function __construct(array $params = [])
}
}

/**
* Convert param into Jsonapi object
*
* @param string $field
* @param mixed $params
*
* @return static
*/
public function convert($field, $params)
{
return $params;
}

/**
* Set params.
*
Expand Down Expand Up @@ -56,13 +69,9 @@ public function addParams(array $params)
if ($jsonStruct === false || $jsonStruct === null || !is_array($jsonStruct)) {
return $this;
}
if (empty($jsonStruct)) {
$this->_params = $params;
} else {
foreach ($params as $field => $value) {
if (in_array($field, $jsonStruct)) {
$this->_params[$field] = $value;
}
foreach ($params as $field => $value) {
if (empty($jsonStruct) || in_array($field, $jsonStruct)) {
$this->_params[$field] = $this->convert($field, $value);
}
}

Expand Down Expand Up @@ -115,7 +124,7 @@ public function set($field, $value)
return $this;
}
if (empty($jsonStruct) || in_array($field, $jsonStruct)) {
$this->_params[$field] = $value;
$this->_params[$field] = $this->convert($field, $value);
}

return $this;
Expand All @@ -141,9 +150,9 @@ public function add($field, $value, $key = null)
$this->_params[$field] = [];
}
if ($key === null) {
$this->_params[$field][] = $value;
$this->_params[$field][] = $this->convert($field, $value);
} else {
$this->_params[$field][$key] = $value;
$this->_params[$field][$key] = $this->convert($field, $value);
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/JsonObjects/V1_0/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@ public function getJsonStruct()
{
return ['id', 'status', 'code', 'title', 'detail', 'source', 'links', 'meta'];
}

/**
* Convert param into Jsonapi object.
*
* @param string $field
* @param mixed $params
*
* @return static
*/
public function convert($field, $params)
{
switch ($field) {
case 'meta':
if ($params instanceof Meta) {
return $params;
} elseif (is_array($params)) {
return new Meta($params);
}

return;
default:
return $params;
}
}
}
24 changes: 24 additions & 0 deletions src/JsonObjects/V1_0/Jsonapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@ public function getJsonStruct()
{
return ['version', 'meta'];
}

/**
* Convert param into Jsonapi object.
*
* @param string $field
* @param mixed $params
*
* @return static
*/
public function convert($field, $params)
{
switch ($field) {
case 'meta':
if ($params instanceof Meta) {
return $params;
} elseif (is_array($params)) {
return new Meta($params);
}

return;
default:
return $params;
}
}
}
69 changes: 66 additions & 3 deletions src/JsonObjects/V1_0/TopLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TopLevel extends Object
public function __construct(array $params = [])
{
parent::__construct($params);
$this->set('jsonapi', (new Jsonapi(['version' => '1.0'])));
$this->set('jsonapi', ['version' => '1.0']);
}

/**
Expand All @@ -29,6 +29,62 @@ public function getJsonStruct()
return ['data', 'errors', 'included', 'links', 'meta', 'jsonapi'];
}

/**
* Convert param into Jsonapi object.
*
* @param string $field
* @param mixed $params
*
* @return static
*/
public function convert($field, $params)
{
switch ($field) {
case 'data':
if ($params instanceof Resource) {
return $params;
} elseif (is_array($params)) {
$isList = false;
$converted = [];
foreach ($params as $key => $value) {
if ($value instanceof Resource) {
$isList = true;
$converted[] = $value;
} elseif ($key === 'id') {
$converted[] = new Resource($params);
} else {
$converted[] = new Resource($value);
}
}
if ($isList) {
return empty($converted) ? null : $converted[0];
}

return $converted;
}

return;
case 'meta':
if ($params instanceof Meta) {
return $params;
} elseif (is_array($params)) {
return new Meta($params);
}

return;
case 'jsonapi':
if ($params instanceof Jsonapi) {
return $params;
} elseif (is_array($params)) {
return new Jsonapi($params);
}

return;
default:
return $params;
}
}

/**
* Set model as data.
*
Expand Down Expand Up @@ -147,6 +203,12 @@ public function parseModel($model)
}
}
$relationshipTopLevel->delete('jsonapi');
$data = $relationshipTopLevel->data;
if (is_array($data)) {
$isList = true;
} else {
$isList = false;
}
$relationshipArray = $relationshipTopLevel->toArray();
$relationship = [];
$include = [];
Expand All @@ -173,9 +235,10 @@ public function parseModel($model)
if (!empty($relationshipArray['data'])) {
$includes[] = $relationshipArray['data'];
}
if (!empty($relationship)) {
$resource->add('relationships', $relationship, $key);
if (empty($relationship) && !$isList) {
$relationship = null;
}
$resource->add('relationships', $relationship, $key);
}
}

Expand Down
29 changes: 0 additions & 29 deletions tests/V1_0/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,6 @@ public function testConstructionSetGetAddParams()
$this->assertSame(['key' => $source], $object->source);
$this->assertSame(['source' => ['key' => $source]], $object->getParams());
$this->assertSame(['source' => ['key' => $source]], $object->getParams(['source']));

$jsonStruct = $object->getJsonStruct();
if (is_array($jsonStruct)) {
if (!empty($jsonStruct)) {
foreach ($jsonStruct as $field) {
for ($i = 0;$i < static::MAX_LOOP;++$i) {
$value = $faker->word;
$object = new Error([$field => $value]);
$this->assertSame($value, $object->$field);
}
}
$params = [];
foreach ($jsonStruct as $field) {
$params[$field] = $faker->word;
}
$object = new Error($params);
foreach ($params as $field => $value) {
$this->assertSame($value, $object->$field);
}
$this->assertSame($params, $object->getParams());
} else {
for ($i = 0;$i < static::MAX_LOOP;++$i) {
$field = $faker->word;
$value = $faker->word;
$object = new Error([$field => $value]);
$this->assertSame($value, $object->$field);
}
}
}
}

public function testToArrayToJson()
Expand Down

0 comments on commit 7ddab5d

Please sign in to comment.