Skip to content
Merged
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
31 changes: 0 additions & 31 deletions src/Styles/AbstractStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Respect\Data\Styles;

use function preg_match;
use function preg_quote;
use function preg_replace;
use function preg_replace_callback;
Expand All @@ -27,34 +26,4 @@ protected function separatorToCamelCase(string $name, string $separator = '_'):
$name,
);
}

protected function pluralToSingular(string $name): string
{
$replacements = [
'/^(.+)ies$/' => '$1y',
'/^(.+)s$/' => '$1',
];
foreach ($replacements as $key => $value) {
if (preg_match($key, $name)) {
return (string) preg_replace($key, $value, $name);
}
}

return $name;
}

protected function singularToPlural(string $name): string
{
$replacements = [
'/^(.+)y$/' => '$1ies',
'/^(.+)([^s])$/' => '$1$2s',
];
foreach ($replacements as $key => $value) {
if (preg_match($key, $name)) {
return (string) preg_replace($key, $value, $name);
}
}

return $name;
}
}
49 changes: 0 additions & 49 deletions src/Styles/CakePHP.php

This file was deleted.

67 changes: 0 additions & 67 deletions src/Styles/NorthWind.php

This file was deleted.

32 changes: 32 additions & 0 deletions src/Styles/Plural.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use function array_map;
use function explode;
use function implode;
use function preg_match;
use function preg_replace;
use function strtolower;
use function substr;
use function ucfirst;
Expand Down Expand Up @@ -52,4 +54,34 @@ public function composed(string $left, string $right): string
{
return $this->singularToPlural($left) . '_' . $this->singularToPlural($right);
}

private function pluralToSingular(string $name): string
{
$replacements = [
'/^(.+)ies$/' => '$1y',
'/^(.+)s$/' => '$1',
];
foreach ($replacements as $key => $value) {
if (preg_match($key, $name)) {
return (string) preg_replace($key, $value, $name);
}
}

return $name;
}

private function singularToPlural(string $name): string
{
$replacements = [
'/^(.+)y$/' => '$1ies',
'/^(.+)([^s])$/' => '$1$2s',
];
foreach ($replacements as $key => $value) {
if (preg_match($key, $name)) {
return (string) preg_replace($key, $value, $name);
}
}

return $name;
}
}
13 changes: 0 additions & 13 deletions src/Styles/Sakila.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/AbstractMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use ReflectionObject;
use Respect\Data\Collections\Collection;
use Respect\Data\Hydrators\Nested;
use Respect\Data\Styles\CakePHP;
use Respect\Data\Styles\Plural;
use Respect\Data\Styles\Standard;
use SplObjectStorage;

Expand Down Expand Up @@ -92,7 +92,7 @@ public function getStyleShouldReturnSameInstanceOnSubsequentCalls(): void
#[Test]
public function styleShouldComeFromEntityFactory(): void
{
$style = new CakePHP();
$style = new Plural();
$mapper = new class (new Nested(new EntityFactory(style: $style))) extends AbstractMapper {
public function flush(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/EntityFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function createAndCopySkipsUninitializedSourceProperties(): void
#[Test]
public function getStyleReturnsConfiguredStyle(): void
{
$style = new Styles\CakePHP();
$style = new Styles\Plural();
$factory = new EntityFactory(style: $style);
$this->assertSame($style, $factory->style);
}
Expand Down
34 changes: 0 additions & 34 deletions tests/Styles/AbstractStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@ public function isRelationProperty(string $name): bool
};
}

/** @return array<array{string, string}> */
public static function singularPluralProvider(): array
{
return [
['post', 'posts'],
['comment', 'comments'],
['category', 'categories'],
['tag', 'tags'],
['entity', 'entities'],
];
}

/** @return array<array{string, string, string}> */
public static function camelCaseToSeparatorProvider(): array
{
Expand All @@ -96,16 +84,6 @@ public static function camelCaseToSeparatorProvider(): array
];
}

#[DataProvider('singularPluralProvider')]
public function testPluralToSingularAndViceVersa(string $singular, string $plural): void
{
$pluralToSingular = new ReflectionMethod($this->style, 'pluralToSingular');
$this->assertEquals($singular, $pluralToSingular->invoke($this->style, $plural));

$singularToPlural = new ReflectionMethod($this->style, 'singularToPlural');
$this->assertEquals($plural, $singularToPlural->invoke($this->style, $singular));
}

#[DataProvider('camelCaseToSeparatorProvider')]
public function testCamelCaseToSeparatorAndViceVersa(
string $separator,
Expand All @@ -124,16 +102,4 @@ public function testCamelCaseToSeparatorAndViceVersa(
$separatorToCamelCaseMethod->invoke($this->style, $separated, $separator),
);
}

public function testPluralToSingularReturnsUnchangedWhenNoMatch(): void
{
$method = new ReflectionMethod($this->style, 'pluralToSingular');
$this->assertEquals('fox', $method->invoke($this->style, 'fox'));
}

public function testSingularToPluralReturnsUnchangedWhenNoMatch(): void
{
$method = new ReflectionMethod($this->style, 'singularToPlural');
$this->assertEquals('news', $method->invoke($this->style, 'news'));
}
}
12 changes: 0 additions & 12 deletions tests/Styles/CakePHP/Author.php

This file was deleted.

56 changes: 0 additions & 56 deletions tests/Styles/CakePHP/CakePHPIntegrationTest.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/Styles/CakePHP/Category.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/Styles/CakePHP/Comment.php

This file was deleted.

Loading
Loading