-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/json api output #24
Draft
Nizari
wants to merge
4
commits into
master
Choose a base branch
from
feature/json-api-output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ Homestead.json | |
/.vagrant | ||
.phpunit.result.cache | ||
coverage/ | ||
.php_cs.cache | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Apitizer\JsonApi; | ||
|
||
class Document | ||
{ | ||
// /** @var ResourceObject[] */ | ||
// protected $includedResources = []; | ||
|
||
/** @var ResourceObject[] */ | ||
protected $resources = []; | ||
|
||
/** @var array */ | ||
protected $includes = []; | ||
|
||
/** | ||
* add a resource to the collection | ||
* adds included resources if found inside the resource's relationships, unless $options['includeContainedResources'] is set to false. | ||
* | ||
* @param string $type | ||
* @param string $id | ||
* @param array $attributes optional, if given a ResourceObject is added, otherwise a ResourceIdentifierObject is added | ||
*/ | ||
public function addResource(string $type, string $id, array $attributes = []): void | ||
{ | ||
$object = ResourceObject::factory($attributes, $type, $id); | ||
|
||
$this->resources[] = $object; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$resources = collect($this->resources); | ||
|
||
return $resources->count() == 1 | ||
? ['data' => $resources->first()->toArray()] | ||
: ['data' => $resources->toArray()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?php | ||
|
||
namespace Apitizer\JsonApi; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
|
||
class ResourceObject implements Arrayable | ||
{ | ||
|
||
/** @var array */ | ||
protected $attributes = []; | ||
|
||
/** @var array */ | ||
protected $relationships; | ||
|
||
/** @var string */ | ||
protected $type; | ||
|
||
/** @var string */ | ||
protected $id; | ||
|
||
public function __construct(string $type, string $id) | ||
{ | ||
$this->type = $type; | ||
$this->id = $id; | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $type | ||
* @param string $id | ||
* @return ResourceObject | ||
*/ | ||
public static function factory(array $attributes, string $type, string $id) | ||
{ | ||
$resourceObject = new self($type, $id); | ||
$resourceObject->attributes = $attributes; | ||
|
||
return $resourceObject; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$array = []; | ||
$array['type'] = $this->type; | ||
$array['id'] = $this->id; | ||
if (! empty($this->attributes)) { | ||
$array['attributes'] = $this->attributes; | ||
} | ||
|
||
// if ($this->meta !== null && $this->meta->isEmpty() === false) { | ||
// $array['meta'] = $this->meta->toArray(); | ||
// } | ||
// if ($this->relationships !== null && $this->relationships->isEmpty() === false) { | ||
// $array['relationships'] = $this->relationships->toArray(); | ||
// } | ||
// if ($this->links !== null && $this->links->isEmpty() === false) { | ||
// $array['links'] = $this->links->toArray(); | ||
// } | ||
|
||
return $array; | ||
} | ||
|
||
|
||
// /** | ||
// * @param object $attributes | ||
// * @param string $type optional | ||
// * @param string|int $id optional | ||
// * @param array $options optional {@see ResourceObject::$defaults} | ||
// * @return ResourceObject | ||
// */ | ||
// public static function fromObject($attributes, $type=null, $id=null, array $options=[]) { | ||
// $array = Converter::objectToArray($attributes); | ||
|
||
// return self::fromArray($array, $type, $id, $options); | ||
// } | ||
|
||
// /** | ||
// * add key-value pairs to attributes | ||
// * | ||
// * @param string $key | ||
// * @param mixed $value | ||
// * @param array $options optional {@see ResourceObject::$defaults} | ||
// */ | ||
// public function add($key, $value, array $options=[]) { | ||
// $options = array_merge(self::$defaults, $options); | ||
|
||
// if ($this->attributes === null) { | ||
// $this->attributes = new AttributesObject(); | ||
// } | ||
|
||
// $this->validator->claimUsedFields([$key], Validator::OBJECT_CONTAINER_ATTRIBUTES, $options); | ||
|
||
// $this->attributes->add($key, $value); | ||
// } | ||
|
||
// /** | ||
// * @param string $key | ||
// * @param mixed $relation ResourceInterface | ResourceInterface[] | CollectionDocument | ||
// * @param array $links optional | ||
// * @param array $meta optional | ||
// * @param array $options optional {@see ResourceObject::$defaults} | ||
// * @return RelationshipObject | ||
// */ | ||
// public function addRelationship($key, $relation, array $links=[], array $meta=[], array $options=[]) { | ||
// $relationshipObject = RelationshipObject::fromAnything($relation, $links, $meta); | ||
|
||
// $this->addRelationshipObject($key, $relationshipObject, $options); | ||
|
||
// return $relationshipObject; | ||
// } | ||
|
||
// /** | ||
// * @param string $href | ||
// * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added | ||
// */ | ||
// public function setSelfLink($href, array $meta=[]) { | ||
// $this->addLink('self', $href, $meta); | ||
// } | ||
|
||
|
||
// /** | ||
// * @param string $key | ||
// * @param RelationshipObject $relationshipObject | ||
// * @param array $options optional {@see ResourceObject::$defaults} | ||
// * | ||
// * @throws DuplicateException if the resource is contained as a resource in the relationship | ||
// */ | ||
// public function addRelationshipObject($key, RelationshipObject $relationshipObject, array $options=[]) { | ||
// if ($relationshipObject->hasResource($this)) { | ||
// throw new DuplicateException('can not add relation to self'); | ||
// } | ||
|
||
// if ($this->relationships === null) { | ||
// $this->setRelationshipsObject(new RelationshipsObject()); | ||
// } | ||
|
||
// $this->validator->claimUsedFields([$key], Validator::OBJECT_CONTAINER_RELATIONSHIPS, $options); | ||
|
||
// $this->relationships->addRelationshipObject($key, $relationshipObject); | ||
// } | ||
|
||
// /** | ||
// * @param RelationshipsObject $relationshipsObject | ||
// */ | ||
// public function setRelationshipsObject(RelationshipsObject $relationshipsObject) { | ||
// $newKeys = $relationshipsObject->getKeys(); | ||
// $this->validator->clearUsedFields(Validator::OBJECT_CONTAINER_RELATIONSHIPS); | ||
// $this->validator->claimUsedFields($newKeys, Validator::OBJECT_CONTAINER_RELATIONSHIPS); | ||
|
||
// $this->relationships = $relationshipsObject; | ||
// } | ||
|
||
// /** | ||
// * internal api | ||
// */ | ||
|
||
// /** | ||
// * whether the ResourceObject is empty except for the ResourceIdentifierObject | ||
// * | ||
// * this can be used to determine if a Relationship's resource could be added as included resource | ||
// * | ||
// * @internal | ||
// * | ||
// * @return boolean | ||
// */ | ||
// public function hasIdentifierPropertiesOnly() { | ||
// if ($this->attributes !== null && $this->attributes->isEmpty() === false) { | ||
// return false; | ||
// } | ||
// if ($this->relationships !== null && $this->relationships->isEmpty() === false) { | ||
// return false; | ||
// } | ||
// if ($this->links !== null && $this->links->isEmpty() === false) { | ||
// return false; | ||
// } | ||
|
||
// return true; | ||
// } | ||
|
||
// /** | ||
// * ResourceInterface | ||
// */ | ||
|
||
// /** | ||
// * @inheritDoc | ||
// */ | ||
// public function getResource($identifierOnly=false) { | ||
// if ($identifierOnly) { | ||
// return ResourceIdentifierObject::fromResourceObject($this); | ||
// } | ||
|
||
// return $this; | ||
// } | ||
|
||
// /** | ||
// * ObjectInterface | ||
// */ | ||
|
||
// /** | ||
// * @inheritDoc | ||
// */ | ||
// public function isEmpty() { | ||
// if (parent::isEmpty() === false) { | ||
// return false; | ||
// } | ||
// if ($this->attributes !== null && $this->attributes->isEmpty() === false) { | ||
// return false; | ||
// } | ||
// if ($this->relationships !== null && $this->relationships->isEmpty() === false) { | ||
// return false; | ||
// } | ||
// if ($this->links !== null && $this->links->isEmpty() === false) { | ||
// return false; | ||
// } | ||
|
||
// return true; | ||
// } | ||
|
||
|
||
// /** | ||
// * @inheritDoc | ||
// */ | ||
// public function getNestedContainedResourceObjects() { | ||
// if ($this->relationships === null) { | ||
// return []; | ||
// } | ||
|
||
// return $this->relationships->getNestedContainedResourceObjects(); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the contstructor? Or
factory(ResourceObject::class)->create($attributes);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
je can use the factory pattern also just for initing your object if you have some specific create logic you dont always want inside your constructor. I remember we had this discussion before ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't finish that discussion