-
-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
664 additions
and
102 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
83 changes: 83 additions & 0 deletions
83
src/Laravel/Eloquent/Metadata/Factory/Property/EloquentAttributePropertyMetadataFactory.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,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (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. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\Eloquent\Metadata\Factory\Property; | ||
|
||
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
final class EloquentAttributePropertyMetadataFactory implements PropertyMetadataFactoryInterface | ||
{ | ||
public function __construct(private readonly ?PropertyMetadataFactoryInterface $decorated = null) | ||
{ | ||
} | ||
|
||
public function create(string $resourceClass, string $property, array $options = []): ApiProperty | ||
{ | ||
$refl = new \ReflectionClass($resourceClass); | ||
$model = $refl->newInstanceWithoutConstructor(); | ||
|
||
$propertyMetadata = $this->decorated?->create($resourceClass, $property, $options); | ||
if (!$model instanceof Model) { | ||
return $propertyMetadata ?? new ApiProperty(); | ||
} | ||
|
||
try { | ||
$method = $refl->getMethod($property); | ||
|
||
if ($attributes = $method->getAttributes(ApiProperty::class)) { | ||
return $this->createMetadata($attributes[0]->newInstance(), $propertyMetadata); | ||
} | ||
} catch (\ReflectionException) { | ||
} | ||
|
||
$attributes = $refl->getAttributes(ApiProperty::class); | ||
foreach ($attributes as $attribute) { | ||
$instance = $attribute->newInstance(); | ||
if ($instance->getProperty() === $property) { | ||
return $this->createMetadata($instance, $propertyMetadata); | ||
} | ||
} | ||
|
||
return $propertyMetadata; | ||
} | ||
|
||
private function createMetadata(ApiProperty $attribute, ?ApiProperty $propertyMetadata = null): ApiProperty | ||
{ | ||
if (null === $propertyMetadata) { | ||
return $this->handleUserDefinedSchema($attribute); | ||
} | ||
|
||
foreach (get_class_methods(ApiProperty::class) as $method) { | ||
if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches) && null !== $val = $attribute->{$method}()) { | ||
$propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val); | ||
} | ||
} | ||
|
||
return $this->handleUserDefinedSchema($propertyMetadata); | ||
} | ||
|
||
private function handleUserDefinedSchema(ApiProperty $propertyMetadata): ApiProperty | ||
{ | ||
// can't know later if the schema has been defined by the user or by API Platform | ||
// store extra key to make this difference | ||
if (null !== $propertyMetadata->getSchema()) { | ||
$extraProperties = $propertyMetadata->getExtraProperties() ?? []; | ||
$propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties); | ||
} | ||
|
||
return $propertyMetadata; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...vel/Eloquent/Metadata/Factory/Property/EloquentAttributePropertyNameCollectionFactory.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,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (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. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\Eloquent\Metadata\Factory\Property; | ||
|
||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; | ||
use ApiPlatform\Metadata\Property\PropertyNameCollection; | ||
|
||
final class EloquentAttributePropertyNameCollectionFactory implements PropertyNameCollectionFactoryInterface | ||
{ | ||
public function __construct(private readonly ?PropertyNameCollectionFactoryInterface $decorated = null) | ||
{ | ||
} | ||
|
||
public function create(string $resourceClass, array $options = []): PropertyNameCollection | ||
{ | ||
$properties = $this->decorated ? iterator_to_array($this->decorated->create($resourceClass, $options)) : []; | ||
|
||
$refl = new \ReflectionClass($resourceClass); | ||
$attributes = $refl->getAttributes(ApiProperty::class); | ||
foreach ($attributes as $attribute) { | ||
$instance = $attribute->newInstance(); | ||
if ($property = $instance->getProperty()) { | ||
$properties[] = $property; | ||
} | ||
} | ||
|
||
return new PropertyNameCollection($properties); | ||
} | ||
} |
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
Oops, something went wrong.