-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNameHelper.php
More file actions
130 lines (105 loc) · 3.63 KB
/
NameHelper.php
File metadata and controls
130 lines (105 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Created by PhpStorm.
* User: bdunogier
* Date: 23/09/2018
* Time: 23:05
*/
namespace BD\EzPlatformGraphQLBundle\DomainContent;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
class NameHelper
{
/**
* @var \Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter
*/
private $caseConverter;
public function __construct()
{
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
}
public function domainContentCollectionField(ContentType $contentType)
{
return $this->pluralize(lcfirst($this->toCamelCase($contentType->identifier)));
}
public function domainContentName(ContentType $contentType)
{
return ucfirst($this->toCamelCase($contentType->identifier)) . 'Content';
}
public function domainContentConnection($contentType)
{
return ucfirst($this->toCamelCase($contentType->identifier)) . 'ContentConnection';
}
public function domainContentTypeName(ContentType $contentType)
{
return ucfirst($this->toCamelCase($contentType->identifier)) . 'ContentType';
}
public function domainContentField(ContentType $contentType)
{
return lcfirst($this->toCamelCase($contentType->identifier));
}
public function domainGroupName(ContentTypeGroup $contentTypeGroup)
{
return 'DomainGroup' . ucfirst($this->toCamelCase($this->sanitizeContentTypeGroupIdentifier($contentTypeGroup)));
}
public function domainGroupField(ContentTypeGroup $contentTypeGroup)
{
return lcfirst($this->toCamelCase($this->sanitizeContentTypeGroupIdentifier($contentTypeGroup)));
}
public function fieldDefinitionField(FieldDefinition $fieldDefinition)
{
return lcfirst($this->toCamelCase($fieldDefinition->identifier));
}
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)
*
* @param ContentTypeGroup $contentTypeGroup
* @return string
*/
protected function sanitizeContentTypeGroupIdentifier(ContentTypeGroup $contentTypeGroup): string
{
return str_replace(' ', '_', $contentTypeGroup->identifier);
}
}