Skip to content
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

IBX-2355: Made inheritance of the naming methods possible #32

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 9 additions & 5 deletions spec/Schema/Domain/Content/NameHelperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
use Ibexa\Core\Repository\Values\ContentType\ContentTypeGroup;
use Ibexa\Core\Repository\Values\ContentType\FieldDefinition;
use Ibexa\GraphQL\Schema\Domain\Content\NameHelper;
use Ibexa\GraphQL\Schema\Domain\Pluralizer;
use PhpSpec\ObjectBehavior;

class NameHelperSpec extends ObjectBehavior
{
function let()
public function let(Pluralizer $pluralizer): void
{
$this->beConstructedWith(['id'=>'id_']);
$this->beConstructedWith(
['id'=>'id_'],
$pluralizer
);
}

function it_is_initializable()
public function it_is_initializable(): void
{
$this->shouldHaveType(NameHelper::class);
}

function it_removes_special_characters_from_ContentTypeGroup_identifier()
public function it_removes_special_characters_from_ContentTypeGroup_identifier(): void
{
$contentTypeGroup = new ContentTypeGroup(['identifier' => 'Name with-hyphen']);
$this->itemGroupName($contentTypeGroup)->shouldBe('ItemGroupNameWithHyphen');
}

function it_removes_field_type_identifier_colisions()
public function it_removes_field_type_identifier_colisions(): void
{
$fieldDefinition = new FieldDefinition(['identifier' => 'id']);
$this->fieldDefinitionField($fieldDefinition)->shouldBe('id_');
Expand Down
15 changes: 13 additions & 2 deletions src/bundle/Resources/config/services/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ services:

Ibexa\GraphQL\Schema\Domain\Content\LanguagesIterator: ~

Ibexa\GraphQL\Schema\Domain\Content\NameHelper: ~

Ibexa\GraphQL\Schema\Domain\NameValidator:
calls:
- method: setLogger
Expand All @@ -105,3 +103,16 @@ services:
Ibexa\GraphQL\Schema\ImagesVariationsBuilder: ~

Ibexa\GraphQL\Schema\SchemaGenerator: ~

Ibexa\GraphQL\Schema\Domain\Pluralizer: ~

Ibexa\GraphQL\Schema\Domain\BaseNameHelper:
abstract: true
arguments:
$pluralizer: '@Ibexa\GraphQL\Schema\Domain\Pluralizer'

Ibexa\GraphQL\Schema\Domain\Content\NameHelper:
parent: Ibexa\GraphQL\Schema\Domain\BaseNameHelper
arguments:
$fieldNameOverrides: '%ibexa.graphql.schema.content.field_name.override%'
$pluralizer: '@Ibexa\GraphQL\Schema\Domain\Pluralizer'
4 changes: 0 additions & 4 deletions src/bundle/Resources/config/services/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,3 @@ services:
$configResolver: '@ibexa.config.resolver'
$provider: '@ibexa.siteaccess.provider'
$siteAccessGroups: '%ibexa.site_access.groups%'

Ibexa\GraphQL\Schema\Domain\Content\NameHelper:
arguments:
$fieldNameOverrides: '%ibexa.graphql.schema.content.field_name.override%'
32 changes: 32 additions & 0 deletions src/lib/Schema/Domain/BaseNameHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\GraphQL\Schema\Domain;

use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;

abstract class BaseNameHelper
{
private CamelCaseToSnakeCaseNameConverter $caseConverter;

private Pluralizer $pluralizer;

public function __construct(Pluralizer $pluralizer)
{
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
$this->pluralizer = $pluralizer;
}

protected function toCamelCase(string $string): string
{
return $this->caseConverter->denormalize($string);
}

protected function pluralize(string $name): string
{
return $this->pluralizer->pluralize($name);
}
}
71 changes: 11 additions & 60 deletions src/lib/Schema/Domain/Content/NameHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeGroup;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
use Ibexa\GraphQL\Schema\Domain\BaseNameHelper;
use Ibexa\GraphQL\Schema\Domain\Pluralizer;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;

class NameHelper implements LoggerAwareInterface
class NameHelper extends BaseNameHelper implements LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* @var \Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter
*/
private $caseConverter;

/**
* @var string[]
*/
private $fieldNameOverrides;
private array $fieldNameOverrides;

public function __construct(
array $fieldNameOverrides,
Pluralizer $pluralizer,
LoggerInterface $logger = null
) {
parent::__construct($pluralizer);

public function __construct(array $fieldNameOverrides, LoggerInterface $logger = null)
{
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
$this->logger = $logger ?? new NullLogger();
$this->fieldNameOverrides = $fieldNameOverrides;
}
Expand Down Expand Up @@ -237,55 +237,6 @@ public function fieldDefinitionField(FieldDefinition $fieldDefinition)
return $fieldName;
}

private function toCamelCase($string)
{
return $this->caseConverter->denormalize($string);
}

private function pluralize($name)
{
if (substr($name, -1) === 'f') {
return substr($name, 0, -1) . 'ves';
}

if (substr($name, -1) === 'fe') {
return substr($name, 0, -2) . 'ves';
}

if (substr($name, -1) === 'y') {
if (\in_array(substr($name, -2, 1), ['a', 'e', 'i', 'o', 'u'])) {
return $name . 's';
} else {
return substr($name, 0, -1) . 'ies';
}
}

if (substr($name, -2) === 'is') {
return substr($name, 0, -2) . 'es';
}

if (substr($name, -2) === 'us') {
return substr($name, 0, -2) . 'i';
}

if (\in_array(substr($name, -2), ['on', 'um'])) {
return substr($name, 0, -2) . 'a';
}

if (substr($name, -2) === 'is') {
return substr($name, 0, -2) . 'es';
}

if (
preg_match('/(s|sh|ch|x|z)$/', $name) ||
substr($name, -1) === 'o'
) {
return $name . 'es';
}

return $name . 's';
}

/**
* Removes potential spaces in content types groups names.
* (content types groups identifiers are actually their name).
Expand Down
57 changes: 57 additions & 0 deletions src/lib/Schema/Domain/Pluralizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\GraphQL\Schema\Domain;
konradoboza marked this conversation as resolved.
Show resolved Hide resolved

/**
* @internal
*/
class Pluralizer
{
public function pluralize(string $name): string
{
if (substr($name, -1) === 'f') {
return substr($name, 0, -1) . 'ves';
}

if (substr($name, -1) === 'fe') {
return substr($name, 0, -2) . 'ves';
}

if (substr($name, -1) === 'y') {
if (\in_array(substr($name, -2, 1), ['a', 'e', 'i', 'o', 'u'])) {
return $name . 's';
} else {
return substr($name, 0, -1) . 'ies';
}
}

if (substr($name, -2) === 'is') {
return substr($name, 0, -2) . 'es';
}

if (substr($name, -2) === 'us') {
return substr($name, 0, -2) . 'i';
}

if (\in_array(substr($name, -2), ['on', 'um'])) {
return substr($name, 0, -2) . 'a';
}

if (substr($name, -2) === 'is') {
return substr($name, 0, -2) . 'es';
}

if (
preg_match('/(s|sh|ch|x|z)$/', $name) ||
substr($name, -1) === 'o'
) {
return $name . 'es';
}

return $name . 's';
}
}