Skip to content

Commit

Permalink
Merge pull request #11 from TomHAnderson/feature/naming-strategy
Browse files Browse the repository at this point in the history
Added naming strategy
  • Loading branch information
TomHAnderson authored Feb 3, 2022
2 parents 365dfdd + bc11460 commit 937dd16
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,16 @@ to your `doctrine.php` configuration file:
```


### Naming Strategy

The default naming strategy uses the Inflector's `urlize()` method to change 'associationName' into 'association-name'.
If this is not the way you want to name your relationsihps or routes then create your own naming strategy and assign
it in the config file.


## Route naming

When using the `routeNamePatterns` to create a route name, the entity name becomes `$inflector->urlize(shortName)`
When using the `routeNamePatterns` to create a route name, the entity name becomes `$namingStrategy->route($entityName)`
such as `api.short-name::fetch` according to the example configuration.


Expand Down
1 change: 1 addition & 0 deletions config/hal-doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
return [
'default' => [
'entityManager' => \Doctrine\ORM\EntityManager::class,
'namingStrategy' => \ApiSkeletons\Laravel\HAL\Doctrine\NamingStrategy\DefaultNamingStrategy::class,
'routeNamePatterns' => [
'entity' => 'api.{entityName}::fetch',
'collection' => 'api.{entityName}::fetchAll',
Expand Down
4 changes: 3 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
<file>src</file>

<!-- Include full Doctrine Coding Standard -->
<rule ref="Doctrine"/>
<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
</rule>
</ruleset>
20 changes: 9 additions & 11 deletions src/DoctrineHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

namespace ApiSkeletons\Laravel\HAL\Doctrine;

use ApiSkeletons\Laravel\HAL\Doctrine\NamingStrategy\NamingStrategyInterface;
use ApiSkeletons\Laravel\HAL\Hydrator;
use ApiSkeletons\Laravel\HAL\Resource;
use DateTime;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\MappingException;
use Exception;
use Illuminate\Foundation\Application;
use ReflectionClass;

use function array_diff_key;
use function array_flip;
Expand All @@ -28,7 +26,7 @@ class DoctrineHydrator extends Hydrator
protected array $config = [];
protected string $configurationSection = 'default';
protected EntityManager $entityManager;
protected Inflector $inflector;
protected NamingStrategyInterface $namingStrategy;

public function __construct(Application $application)
{
Expand All @@ -50,8 +48,8 @@ public function __construct(Application $application)

// @codeCoverageIgnoreEnd

$this->entityManager = $application->get($this->config['entityManager']);
$this->inflector = InflectorFactory::create()->build();
$this->entityManager = $application->get($this->config['entityManager']);
$this->namingStrategy = $application->get($this->config['namingStrategy']);
}

public function extract(mixed $entity): Resource
Expand Down Expand Up @@ -108,14 +106,14 @@ public function extract(mixed $entity): Resource

if ($entityMetadata->isAssociationInverseSide($associationName)) {
$resource->addLink(
$associationName,
$this->namingStrategy->association($associationName),
route($associationRouteName, [
'filter' => [$associationMapping['mappedBy'] => $identifier],
])
);
} else {
$resource->addLink(
$associationName,
$this->namingStrategy->association($associationName),
route($associationRouteName, [
'filter' => [$associationMapping['inversedBy'] => $identifier],
])
Expand All @@ -128,13 +126,13 @@ public function extract(mixed $entity): Resource

$associationRouteName = $this->getRouteName($associationMapping['targetEntity'], 'entity');
$resource->addLink(
$associationName,
$this->namingStrategy->association($associationName),
route($associationRouteName, $identifier)
);
} else {
// For 1:1 relationships, only embed the owning side
$resource->addEmbeddedResource(
$associationName,
$this->namingStrategy->association($associationName),
$data[$associationName]
);
}
Expand All @@ -149,7 +147,7 @@ protected function getRouteName(string $entityName, string $routeType): string
return $this->config['entities'][$entityName]['routeNames'][$routeType] ??
str_replace(
'{entityName}',
$this->inflector->urlize((new ReflectionClass($entityName))->getShortName()),
$this->namingStrategy->route($entityName),
$this->config['routeNamePatterns'][$routeType]
);
}
Expand Down
30 changes: 30 additions & 0 deletions src/NamingStrategy/DefaultNamingStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace ApiSkeletons\Laravel\HAL\Doctrine\NamingStrategy;

use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use ReflectionClass;

class DefaultNamingStrategy implements NamingStrategyInterface
{
protected Inflector $inflector;

public function __construct()
{
$this->inflector = InflectorFactory::create()->build();
}

public function route(string $entityName): string
{
return $this->inflector
->urlize((new ReflectionClass($entityName))->getShortName());
}

public function association(string $associationName): string
{
return $this->inflector->urlize($associationName);
}
}
12 changes: 12 additions & 0 deletions src/NamingStrategy/NamingStrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace ApiSkeletons\Laravel\HAL\Doctrine\NamingStrategy;

interface NamingStrategyInterface
{
public function route(string $entityName): string;

public function association(string $associationName): string;
}
1 change: 1 addition & 0 deletions test/config/hal-doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
return [
'default' => [
'entityManager' => \Doctrine\ORM\EntityManager::class,
'namingStrategy' => \ApiSkeletons\Laravel\HAL\Doctrine\NamingStrategy\DefaultNamingStrategy::class,
'routeNamePatterns' => [
'entity' => 'api.{entityName}::fetch',
'collection' => 'api.{entityName}::fetchAll',
Expand Down

0 comments on commit 937dd16

Please sign in to comment.