Skip to content

Commit 31f0f9a

Browse files
authored
Merge pull request #190 from Deuchnord/do-not-singularize-two-letter-words
Singularize/pluralize 3+ letter long words only in the property names
2 parents 139ea4d + 25318e5 commit 31f0f9a

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/TypesGenerator.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t
653653
}
654654

655655
$class['fields'][$propertyName] = [
656-
'name' => $isArray ? Inflector::pluralize($propertyName) : Inflector::singularize($propertyName),
656+
'name' => $this->getFieldName($propertyName, $isArray),
657657
'resource' => $property,
658658
'range' => $ranges[0],
659659
'cardinality' => $cardinality,
@@ -873,4 +873,18 @@ private function fixCs(array $files): void
873873
);
874874
$runner->fix();
875875
}
876+
877+
private function getFieldName(string $propertyName, bool $isArray): string
878+
{
879+
$snakeProperty = preg_replace('/([A-Z])/', '_$1', $propertyName);
880+
$exploded = explode('_', $snakeProperty);
881+
882+
if (2 < \strlen($word = $exploded[\count($exploded) - 1])) {
883+
$exploded[\count($exploded) - 1] = $isArray ? Inflector::pluralize($word) : Inflector::singularize($word);
884+
885+
return implode('', $exploded);
886+
}
887+
888+
return $propertyName;
889+
}
876890
}

tests/Command/GenerateTypesCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,15 @@ public function testReadableWritable(): void
175175
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
176176

177177
$person = file_get_contents("$outputDir/AppBundle/Entity/Person.php");
178+
$this->assertStringContainsString('private $sameAs;', $person);
178179
$this->assertStringContainsString('public function getId(', $person);
179180
$this->assertStringNotContainsString('function setId(', $person);
180181
$this->assertStringContainsString('public function getName(', $person);
181182
$this->assertStringNotContainsString('function setName(', $person);
182183
$this->assertStringContainsString('public function getFriends(', $person);
183184
$this->assertStringNotContainsString('function addFriend(', $person);
184185
$this->assertStringNotContainsString('function removeFriend(', $person);
186+
$this->assertStringNotContainsString('function setSameAs(', $person);
185187
}
186188

187189
public function testGeneratedId(): void

tests/config/readable-writable.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ types:
44
name: { writable: false }
55
familyName: { readable: false }
66
friends: { range: "Person", cardinality: (0..*), writable: false }
7+
sameAs: { writable: false }

0 commit comments

Comments
 (0)