-
-
Notifications
You must be signed in to change notification settings - Fork 921
Allow abstract resource #311
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,8 +199,6 @@ public function denormalize($data, $class, $format = null, array $context = []) | |
} | ||
} | ||
|
||
$reflectionClass = new \ReflectionClass($class); | ||
|
||
if (isset($data['@id']) && !isset($context['object_to_populate'])) { | ||
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['@id']); | ||
|
||
|
@@ -210,9 +208,20 @@ public function denormalize($data, $class, $format = null, array $context = []) | |
$overrideClass = false; | ||
} | ||
|
||
$instanceClass = $overrideClass ? get_class($context['object_to_populate']) : $class; | ||
$reflectionClass = new \ReflectionClass($instanceClass); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using reflection at runtime looks like a bad idea. It will considerably decrease performances. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reflection's Instance is necessary to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok missed that... We probably have a vector to optimize performance here too. |
||
if ($reflectionClass->isAbstract()) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Cannot create an instance of %s from serialized data because it is an abstract resource', | ||
$instanceClass | ||
) | ||
); | ||
} | ||
|
||
$object = $this->instantiateObject( | ||
$normalizedData, | ||
$overrideClass ? get_class($context['object_to_populate']) : $class, | ||
$instanceClass, | ||
$context, | ||
$reflectionClass, | ||
$allowedAttributes | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the DunglasApiBundle package. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Dunglas\ApiBundle\Tests\Behat\TestBundle\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Dunglas\ApiBundle\Annotation\Iri; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* AbstractDummy. | ||
* | ||
* @author Jérémy Derussé <jeremy@derusse.com> | ||
* | ||
* @ORM\Entity | ||
* @ORM\InheritanceType("SINGLE_TABLE") | ||
* @ORM\DiscriminatorColumn(name="discr", type="string", length=16) | ||
* @ORM\DiscriminatorMap({"concrete" = "ConcreteDummy"}) | ||
*/ | ||
abstract class AbstractDummy | ||
{ | ||
/** | ||
* @var int The id. | ||
* | ||
* @ORM\Column(type="integer") | ||
* @ORM\Id | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
*/ | ||
private $id; | ||
/** | ||
* @var string The dummy name. | ||
* | ||
* @ORM\Column | ||
* @Assert\NotBlank | ||
* @Iri("http://schema.org/name") | ||
*/ | ||
private $name; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the DunglasApiBundle package. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Dunglas\ApiBundle\Tests\Behat\TestBundle\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* ConcreteDummy. | ||
* | ||
* @author Jérémy Derusse <jeremy@derusse.com> | ||
* | ||
* @ORM\Entity | ||
*/ | ||
class ConcreteDummy extends AbstractDummy | ||
{ | ||
/** | ||
* @var string a concrete thing | ||
* | ||
* @ORM\Column | ||
* @Assert\NotBlank | ||
*/ | ||
private $instance; | ||
|
||
public function setInstance($instance) | ||
{ | ||
$this->instance = $instance; | ||
} | ||
|
||
public function getInstance() | ||
{ | ||
return $this->instance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
Feature: Create-Retrieve-Update-Delete on abstract resource | ||
In order to use an hypermedia API | ||
As a client software developer | ||
I need to be able to retrieve, create, update and delete JSON-LD encoded resources even if they are abstract. | ||
|
||
@createSchema | ||
Scenario: Create a concrete resource | ||
When I send a "POST" request to "/concrete_dummies" with body: | ||
""" | ||
{ | ||
"instance": "Concrete", | ||
"name": "My Dummy" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/ConcreteDummy", | ||
"@id": "/concrete_dummies/1", | ||
"@type": "ConcreteDummy", | ||
"instance": "Concrete", | ||
"name": "My Dummy" | ||
} | ||
""" | ||
|
||
Scenario: Get a resource | ||
When I send a "GET" request to "/abstract_dummies/1" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/ConcreteDummy", | ||
"@id": "/concrete_dummies/1", | ||
"@type": "ConcreteDummy", | ||
"instance": "Concrete", | ||
"name": "My Dummy" | ||
} | ||
""" | ||
|
||
Scenario: Get a collection | ||
When I send a "GET" request to "/abstract_dummies" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/AbstractDummy", | ||
"@id": "/abstract_dummies", | ||
"@type": "hydra:PagedCollection", | ||
"hydra:totalItems": 1, | ||
"hydra:itemsPerPage": 3, | ||
"hydra:firstPage": "/abstract_dummies", | ||
"hydra:lastPage": "/abstract_dummies", | ||
"hydra:member": [ | ||
{ | ||
"@id":"/concrete_dummies/1", | ||
"@type":"ConcreteDummy", | ||
"instance": "Concrete", | ||
"name": "My Dummy" | ||
} | ||
], | ||
"hydra:search": { | ||
"@type": "hydra:IriTemplate", | ||
"hydra:template": "\/abstract_dummies{?id,name,order[id],order[name]}", | ||
"hydra:variableRepresentation": "BasicRepresentation", | ||
"hydra:mapping": [ | ||
{ | ||
"@type": "IriTemplateMapping", | ||
"variable": "id", | ||
"property": "id", | ||
"required": false | ||
}, | ||
{ | ||
"@type": "IriTemplateMapping", | ||
"variable": "name", | ||
"property": "name", | ||
"required": false | ||
}, | ||
{ | ||
"@type": "IriTemplateMapping", | ||
"variable": "order[id]", | ||
"property": "id", | ||
"required": false | ||
}, | ||
{ | ||
"@type": "IriTemplateMapping", | ||
"variable": "order[name]", | ||
"property": "name", | ||
"required": false | ||
} | ||
] | ||
} | ||
} | ||
""" | ||
|
||
Scenario: Update a concrete resource | ||
When I send a "PUT" request to "/concrete_dummies/1" with body: | ||
""" | ||
{ | ||
"@id": "/concrete_dummies/1", | ||
"instance": "Become real", | ||
"name": "A nice dummy" | ||
} | ||
""" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/ConcreteDummy", | ||
"@id": "/concrete_dummies/1", | ||
"@type": "ConcreteDummy", | ||
"instance": "Become real", | ||
"name": "A nice dummy" | ||
} | ||
""" | ||
|
||
@dropSchema | ||
Scenario: Delete a resource | ||
When I send a "DELETE" request to "/abstract_dummies/1" | ||
Then the response status code should be 204 | ||
And the response should be empty |
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.
Methods can be used as keys to avoid the
array_map
call and reduce the complexity O(1) instead of O(n).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.
I don't see the point: Methods used as keys.. of what?
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.
Forget about that it has no interest here.