-
-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add IriOnly Option on Collection properties
- Loading branch information
1 parent
27f2096
commit 0a4f1b4
Showing
12 changed files
with
493 additions
and
3 deletions.
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
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
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
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
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
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
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
90 changes: 90 additions & 0 deletions
90
tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnly.php
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,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document; | ||
|
||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Post; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
/** | ||
* Assert that a property being a collection set with ApiProperty::iriOnly to true returns only the IRI of the collection | ||
*/ | ||
#[Get(normalizationContext: ['groups' => ['read']]), GetCollection(normalizationContext: ['groups' => ['read']]), Post] | ||
#[ODM\Document] | ||
class PropertyCollectionIriOnly | ||
{ | ||
#[ODM\Id(strategy: 'INCREMENT', type: 'int')] | ||
private ?int $id = null; | ||
|
||
#[ODM\ReferenceMany(targetDocument: PropertyCollectionIriOnlyRelation::class)] | ||
#[ApiProperty(iriOnly: true)] | ||
#[Groups('read')] | ||
private Collection $propertyCollectionIriOnlyRelation; | ||
|
||
/** | ||
* @var iterable<int, PropertyCollectionIriOnlyRelation> $iterableIri | ||
*/ | ||
#[ApiProperty(iriOnly: true)] | ||
#[Groups('read')] | ||
private array $iterableIri = []; | ||
|
||
public function __construct() | ||
{ | ||
$this->propertyCollectionIriOnlyRelation = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @return Collection<int, PropertyCollectionIriOnlyRelation> | ||
*/ | ||
public function getPropertyCollectionIriOnlyRelation(): Collection | ||
{ | ||
return $this->propertyCollectionIriOnlyRelation; | ||
} | ||
|
||
public function addPropertyCollectionIriOnlyRelation(PropertyCollectionIriOnlyRelation $propertyCollectionIriOnlyRelation): self | ||
{ | ||
if (!$this->propertyCollectionIriOnlyRelation->contains($propertyCollectionIriOnlyRelation)) { | ||
$this->propertyCollectionIriOnlyRelation->add($propertyCollectionIriOnlyRelation); | ||
$propertyCollectionIriOnlyRelation->setPropertyCollectionIriOnly($this); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function removePropertyCollectionIriOnlyRelation(PropertyCollectionIriOnlyRelation $propertyCollectionIriOnlyRelation): self | ||
{ | ||
if ($this->propertyCollectionIriOnlyRelation->removeElement($propertyCollectionIriOnlyRelation)) { | ||
// set the owning side to null (unless already changed) | ||
if ($propertyCollectionIriOnlyRelation->getPropertyCollectionIriOnly() === $this) { | ||
$propertyCollectionIriOnlyRelation->setPropertyCollectionIriOnly(null); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array<int, PropertyCollectionIriOnlyRelation> | ||
*/ | ||
public function getIterableIri(): array | ||
{ | ||
$propertyCollectionIriOnlyRelation = new PropertyCollectionIriOnlyRelation(); | ||
$propertyCollectionIriOnlyRelation->name = 'Michel'; | ||
|
||
$this->iterableIri[] = $propertyCollectionIriOnlyRelation; | ||
|
||
return $this->iterableIri; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelation.php
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document; | ||
|
||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Post; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
#[GetCollection, Post] | ||
#[ODM\Document] | ||
class PropertyCollectionIriOnlyRelation | ||
{ | ||
/** | ||
* The entity ID | ||
*/ | ||
#[ODM\Id(strategy: 'INCREMENT', type: 'int')] | ||
private ?int $id = null; | ||
|
||
#[ODM\Field(type: 'string')] | ||
#[Groups('read')] | ||
public string $name = ''; | ||
|
||
#[ODM\ReferenceOne(targetDocument: PropertyCollectionIriOnly::class)] | ||
private ?PropertyCollectionIriOnly $propertyCollectionIriOnly = null; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id ?? 9999; | ||
} | ||
|
||
/** | ||
* @return PropertyCollectionIriOnly|null | ||
*/ | ||
public function getPropertyCollectionIriOnly(): ?PropertyCollectionIriOnly | ||
{ | ||
return $this->propertyCollectionIriOnly; | ||
} | ||
|
||
/** | ||
* @param PropertyCollectionIriOnly|null $propertyCollectionIriOnly | ||
*/ | ||
public function setPropertyCollectionIriOnly(?PropertyCollectionIriOnly $propertyCollectionIriOnly): void | ||
{ | ||
$this->propertyCollectionIriOnly = $propertyCollectionIriOnly; | ||
} | ||
} |
Oops, something went wrong.