Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Tests/Model/Issue46/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ class Article
]
private $iri;

/**
* @Rest\Attribute(name="id", type="string")
*/
#[Rest\Attribute(name: 'id', type: 'string')]
private $id;

/**
* @Rest\ManyToOne(name="section", targetEntity="Section")
*/
#[Rest\ManyToOne(name: 'section', targetEntity: 'Section')]
#[Rest\ManyToOne(name: 'section', targetEntity: Section::class)]
private $section;

public function setId($id): void
Expand Down
2 changes: 1 addition & 1 deletion Tests/Model/Issue46/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Section
#[Rest\Attribute(name: 'title', type: 'string')]
private $title;

#[Rest\OneToMany(name: 'articleList', targetEntity: 'Article')]
#[Rest\OneToMany(name: 'articleList', targetEntity: Article::class)]
private $articleList = [];

public function setId($id): void
Expand Down
9 changes: 2 additions & 7 deletions Tests/Model/Issue75/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#[Rest\Entity(key: 'articles')]
class Article
{
/**
* @Rest\Id
*
* @Rest\Attribute(name="@id", type="string")
*/
#[
Rest\Id,
Rest\Attribute(name: '@id', type: 'string')
Expand All @@ -23,10 +18,10 @@ class Article
#[Rest\Attribute(name: 'title', type: 'string')]
private $title;

#[Rest\ManyToOne(name: 'tag', targetEntity: 'Tag')]
#[Rest\ManyToOne(name: 'tag', targetEntity: Tag::class)]
private $tag;

#[Rest\OneToMany(name: 'tagList', targetEntity: 'Tag')]
#[Rest\OneToMany(name: 'tagList', targetEntity: Tag::class)]
private $tagList;

public function setTitle($title): void
Expand Down
9 changes: 2 additions & 7 deletions Tests/Model/Issue80/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ class Article
{
/**
* @var int
*
* @Rest\Id
*
* @Rest\Attribute(name="id", type="int")
*/
#[Rest\Id]
#[Rest\Attribute(name: 'id', type: 'string')]
private $id;

/**
* @Rest\Attribute(name="title", type="string")
*/
#[Rest\Attribute(name: 'title', type: 'string')]
private $title;

Expand Down
4 changes: 2 additions & 2 deletions Tests/Model/JsonLd/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Cart
#[Rest\Attribute(name: 'created_at', type: 'datetime')]
private $createdAt;

#[Rest\OneToMany(name: 'cart_items', targetEntity: 'CartItem')]
#[Rest\OneToMany(name: 'cart_items', targetEntity: CartItem::class)]
private $cartItemList = [];

/**
Expand All @@ -38,7 +38,7 @@ class Cart
#[Rest\Attribute(name: 'clientPhoneNumber', type: 'phone_number')]
private $clientPhoneNumber;

#[Rest\ManyToOne(name: 'order', targetEntity: 'Order')]
#[Rest\ManyToOne(name: 'order', targetEntity: Order::class)]
private $order;

/**
Expand Down
2 changes: 1 addition & 1 deletion Tests/Model/JsonLd/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CartItem
/**
* cart
*/
#[Rest\ManyToOne(name: 'cart', targetEntity: 'Cart')]
#[Rest\ManyToOne(name: 'cart', targetEntity: Cart::class)]
private $cart;

/**
Expand Down
20 changes: 20 additions & 0 deletions Tests/Model/JsonLd/Invalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Mapado\RestClientSdk\Tests\Model\JsonLd;

use Mapado\RestClientSdk\Mapping\Attributes\Entity;
use Mapado\RestClientSdk\Mapping\Attributes\ManyToOne;

#[Entity('invalid')]
class Invalid
{
#[ManyToOne('client', Client::class)]
private $client;

public function getClient(): Client
{
return $this->client;
}
}
5 changes: 0 additions & 5 deletions Tests/Model/JsonLd/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
*/
class Model
{
/**
* @Rest\Id
*
* @Rest\Attribute(name="id", type="string")
*/
#[Rest\Id]
#[Rest\Attribute(name: 'id', type: 'string')]
private $id;
Expand Down
43 changes: 32 additions & 11 deletions Tests/Units/Mapping/Driver/AttributeDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

namespace Mapado\RestClientSdk\Tests\Units\Mapping\Driver;

use Mapado\RestClientSdk\Exception\MappingException;
use Mapado\RestClientSdk\Mapping\Attribute;
use Mapado\RestClientSdk\Mapping\ClassMetadata;
use Mapado\RestClientSdk\Mapping\Driver\AttributeDriver;
use Mapado\RestClientSdk\Mapping\Relation;
use Mapado\RestClientSdk\Tests\Model\JsonLd\Cart;
use Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem;
use Mapado\RestClientSdk\Tests\Model\JsonLd\Client;
use Mapado\RestClientSdk\Tests\Model\JsonLd\Invalid;
use Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository;
use Mapado\RestClientSdk\Tests\Model\JsonLd\Product;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -20,31 +29,43 @@ public function testClassWithoutEntityAnnotation(): void
{
$testedInstance = new AttributeDriver($this->getCacheDir(), true);

$mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\Client');
$mapping = $testedInstance->loadClassname(Client::class);
$this->assertEmpty($mapping);
}

/**
* testClassWithoutEntityAnnotation
*/
public function testClassWithInvalidProperty(): void
{
$testedInstance = new AttributeDriver($this->getCacheDir(), true);

$this->expectException(MappingException::class);

$mapping = $testedInstance->loadClassname(Invalid::class);
}

/**
* testAnnotationDriver
*/
public function testAnnotationDriver(): void
{
$testedInstance = new AttributeDriver($this->getCacheDir(), true);

$mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\Product');
$mapping = $testedInstance->loadClassname(Product::class);
$this->assertCount(1, $mapping);

$classMetadata = current($mapping);
$this->assertInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata', $classMetadata);
$this->assertInstanceOf(ClassMetadata::class, $classMetadata);
$this->assertEquals('product', $classMetadata->getKey());
$this->assertEquals('Mapado\RestClientSdk\Tests\Model\JsonLd\Product', $classMetadata->getModelName());
$this->assertEquals('Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository', $classMetadata->getRepositoryName());
$this->assertEquals(Product::class, $classMetadata->getModelName());
$this->assertEquals(ModelRepository::class, $classMetadata->getRepositoryName());

$attributeList = $classMetadata->getAttributeList();
$this->assertCount(3, $attributeList);

$attribute = current($attributeList);
$this->assertInstanceOf('Mapado\RestClientSdk\Mapping\Attribute', $attribute);
$this->assertInstanceOf(Attribute::class, $attribute);
$this->assertEquals('id', $attribute->getSerializedKey());

$attribute = next($attributeList);
Expand All @@ -56,11 +77,11 @@ public function testAnnotationDriverWithRelations(): void
{
$testedInstance = new AttributeDriver($this->getCacheDir(), true);

$mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart');
$mapping = $testedInstance->loadClassname(Cart::class);
$this->assertCount(1, $mapping);

$classMetadata = current($mapping);
$this->assertInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata', $classMetadata);
$this->assertInstanceOf(ClassMetadata::class, $classMetadata);
$this->assertEquals('cart', $classMetadata->getKey());

$attributeList = $classMetadata->getAttributeList();
Expand All @@ -70,11 +91,11 @@ public function testAnnotationDriverWithRelations(): void
$this->assertCount(2, $relationList);
$this->assertEquals(Relation::ONE_TO_MANY, current($relationList)->getType());

$mapping = $testedInstance->loadClassname('Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem');
$mapping = $testedInstance->loadClassname(CartItem::class);
$this->assertCount(1, $mapping);

$classMetadata = current($mapping);
$this->assertInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata', $classMetadata);
$this->assertInstanceOf(ClassMetadata::class, $classMetadata);
$this->assertEquals('cart_item', $classMetadata->getKey());

$attributeList = $classMetadata->getAttributeList();
Expand All @@ -84,7 +105,7 @@ public function testAnnotationDriverWithRelations(): void
$this->assertCount(1, $relationList);
$relation = current($relationList);
$this->assertEquals(Relation::MANY_TO_ONE, $relation->getType());
$this->assertEquals('Mapado\RestClientSdk\Tests\Model\JsonLd\Cart', $relation->getTargetEntity());
$this->assertEquals(Cart::class, $relation->getTargetEntity());
}

public function testLoadDirectory(): void
Expand Down
6 changes: 3 additions & 3 deletions Tests/Units/Model/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ public function testSerializingIriManyToOne(): void
],
$this->testedInstance->serialize(
$article,
'Mapado\RestClientSdk\Tests\Model\Issue46\Article'
Issue46\Article::class
)
);

Expand All @@ -747,7 +747,7 @@ public function testSerializingIriManyToOne(): void
],
$this->testedInstance->serialize(
$section,
'Mapado\RestClientSdk\Tests\Model\Issue46\Section'
Issue46\Section::class
)
);

Expand All @@ -766,7 +766,7 @@ public function testSerializingIriManyToOne(): void
],
$this->testedInstance->serialize(
$section,
'Mapado\RestClientSdk\Tests\Model\Issue46\Section',
Issue46\Section::class,
['serializeRelations' => ['articleList']]
)
);
Expand Down
3 changes: 3 additions & 0 deletions src/Mapping/Attributes/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

abstract class Relation
{
/**
* @param class-string $targetEntity
*/
public function __construct(
public readonly string $name,
public readonly string $targetEntity,
Expand Down
12 changes: 4 additions & 8 deletions src/Mapping/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mapado\RestClientSdk\Mapping\Attributes;
use Mapado\RestClientSdk\Mapping\ClassMetadata;
use Mapado\RestClientSdk\Mapping\Relation;
use function PHPStan\dumpType;

/**
* Class AttributeDriver
Expand Down Expand Up @@ -137,20 +138,15 @@ private function getClassMetadataForClassname(
);
}

if ($relation) {
if ($relation instanceof Attributes\Relation) {
$attributeList[] = new Attribute(
$relation->name,
$property->getName(),
);

$targetEntity = $relation->targetEntity;
if (false === mb_strpos($targetEntity, '/')) {
$targetEntity =
mb_substr(
$classname,
0,
mb_strrpos($classname, '\\') + 1,
) . $targetEntity;
if (null === $this->getClassMetadataForClassname($targetEntity)) {
throw new MappingException("Invalid targetEntity");
}

$relationList[] = new Relation(
Expand Down
29 changes: 10 additions & 19 deletions src/Mapping/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,13 @@ class Relation
public const ONE_TO_MANY = 'OneToMany';

/**
* @var string
* @param class-string $targetEntity
*/
private $serializedKey;

/**
* @var string
*/
private $type;

/**
* @var string
*/
private $targetEntity;

public function __construct(
string $serializedKey,
string $type,
string $targetEntity,
private string $serializedKey,
private string $type,
private string $targetEntity,
) {
$this->serializedKey = $serializedKey;
$this->type = $type;
$this->targetEntity = $targetEntity;
}

public function getSerializedKey(): string
Expand Down Expand Up @@ -73,11 +58,17 @@ public function isManyToOne(): bool
return self::MANY_TO_ONE === $this->getType();
}

/**
* @return class-string
*/
public function getTargetEntity(): string
{
return $this->targetEntity;
}

/**
* @param class-string $targetEntity
*/
public function setTargetEntity(string $targetEntity): self
{
$this->targetEntity = $targetEntity;
Expand Down
Loading