GenericAnalyzer::fullEntityName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 23.03.16 at 11:45
6
 */
7
namespace samsoncms\api\generator\analyzer;
8
9
use samsonframework\orm\DatabaseInterface;
10
11
/**
12
 * Generic entities metadata analyzer.
13
 *
14
 * @package samsoncms\api\analyzer
15
 */
16
class GenericAnalyzer
17
{
18
    /** @var DatabaseInterface */
19
    protected $database;
20
21
    /** @var string Metadata class */
22
    protected $metadataClass = \samsoncms\api\generator\metadata\GenericMetadata::class;
23
24
    /**
25
     * Generator constructor.
26
     * @param DatabaseInterface $database Database instance
27
     */
28
    public function __construct(DatabaseInterface $database)
29
    {
30
        $this->database = $database;
31
    }
32
33
    /**
34
     * @return \samsoncms\api\generator\metadata\GenericMetadata[]
35
     */
36
    public function analyze()
37
    {
38
39
    }
40
41
    /**
42
     * Get correct field name.
43
     *
44
     * @param string $fieldName Original field name
45
     *
46
     * @return string Correct PHP-supported field name
47
     */
48
    protected function fieldName($fieldName)
49
    {
50
        return $fieldName = lcfirst($this->transliterated($fieldName));
0 ignored issues
show
Unused Code introduced by
$fieldName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
    }
52
53
    /**
54
     * Transliterate string to english.
55
     *
56
     * @param string $string Source string
57
     *
58
     * @return string Transliterated string
59
     */
60
    protected function transliterated($string)
61
    {
62
        return str_replace(
63
            ' ',
64
            '',
65
            ucwords(iconv("UTF-8", "UTF-8//IGNORE", strtr($string, array(
66
                            "'" => "",
67
                            "`" => "",
68
                            "-" => " ",
69
                            "_" => " ",
70
                            "а" => "a", "А" => "a",
71
                            "б" => "b", "Б" => "b",
72
                            "в" => "v", "В" => "v",
73
                            "г" => "g", "Г" => "g",
74
                            "д" => "d", "Д" => "d",
75
                            "е" => "e", "Е" => "e",
76
                            "ж" => "zh", "Ж" => "zh",
77
                            "з" => "z", "З" => "z",
78
                            "и" => "i", "И" => "i",
79
                            "й" => "y", "Й" => "y",
80
                            "к" => "k", "К" => "k",
81
                            "л" => "l", "Л" => "l",
82
                            "м" => "m", "М" => "m",
83
                            "н" => "n", "Н" => "n",
84
                            "о" => "o", "О" => "o",
85
                            "п" => "p", "П" => "p",
86
                            "р" => "r", "Р" => "r",
87
                            "с" => "s", "С" => "s",
88
                            "т" => "t", "Т" => "t",
89
                            "у" => "u", "У" => "u",
90
                            "ф" => "f", "Ф" => "f",
91
                            "х" => "h", "Х" => "h",
92
                            "ц" => "c", "Ц" => "c",
93
                            "ч" => "ch", "Ч" => "ch",
94
                            "ш" => "sh", "Ш" => "sh",
95
                            "щ" => "sch", "Щ" => "sch",
96
                            "ъ" => "", "Ъ" => "",
97
                            "ы" => "y", "Ы" => "y",
98
                            "ь" => "", "Ь" => "",
99
                            "э" => "e", "Э" => "e",
100
                            "ю" => "yu", "Ю" => "yu",
101
                            "я" => "ya", "Я" => "ya",
102
                            "і" => "i", "І" => "i",
103
                            "ї" => "yi", "Ї" => "yi",
104
                            "є" => "e", "Є" => "e"
105
                        )
106
                    )
107
                )
108
            )
109
        );
110
    }
111
112
    /**
113
     * Get correct full entity name with name space.
114
     *
115
     * @param string $navigationName Original navigation entity name
116
     * @param string $namespace      Namespace
117
     *
118
*@return string Correct PHP-supported entity name
119
     */
120
    protected function fullEntityName($navigationName, $namespace = __NAMESPACE__)
0 ignored issues
show
Unused Code introduced by
The parameter $namespace is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        return '\samsoncms\api\generated\\' . $this->entityName($navigationName);
123
    }
124
125
    /**
126
     * Get correct entity name.
127
     *
128
     * @param string $navigationName Original navigation entity name
129
     *
130
*@return string Correct PHP-supported entity name
131
     */
132
    protected function entityName($navigationName)
133
    {
134
        return ucfirst($this->getValidName($this->transliterated($navigationName)));
135
    }
136
137
    /**
138
     * Remove all wrong characters from entity name
139
     *
140
     * @param string $navigationName Original navigation entity name
141
     *
142
     * @return string Correct PHP-supported entity name
143
     */
144
    protected function getValidName($navigationName)
145
    {
146
        return preg_replace('/(^\d*)|([^\w\d_])/', '', $navigationName);
147
    }
148
149
    /**
150
     * Get additional field type in form of Field constant name
151
     * by database additional field type identifier.
152
     *
153
     * @param integer $fieldType Additional field type identifier
154
     *
155
     * @return string Additional field type constant
156
     */
157
    protected function additionalFieldType($fieldType)
158
    {
159
        return 'Field::' . $this->constantNameByValue($fieldType);
160
    }
161
162
    /**
163
     * Get class constant name by its value.
164
     *
165
     * @param string $value     Constant value
166
     * @param string $className Class name
167
     *
168
     * @return string Full constant name
169
     */
170
    protected function constantNameByValue($value, $className = Field::ENTITY)
171
    {
172
        // Get array where class constants are values and their values are keys
173
        $nameByValue = array_flip((new \ReflectionClass($className))->getConstants());
174
175
        // Try to find constant by its value
176
        if (null !== $nameByValue[$value]) {
177
            // Return constant name
178
            return $nameByValue[$value];
179
        }
180
181
        return '';
182
    }
183
}
184
//[PHPCOMPRESSOR(remove,end)]