Completed
Push — master ( 131e3d...8b7814 )
by Vitaly
02:40
created

GenericAnalyzer::getValidName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 array Collection of all parsed metadata by identifiers */
19
    public static $metadata = [];
20
21
    /** @var DatabaseInterface */
22
    protected $database;
23
24
    /** @var string Metadata class */
25
    protected $metadataClass = \samsoncms\api\generator\metadata\GenericMetadata::class;
26
27
    /**
28
     * Generator constructor.
29
     * @param DatabaseInterface $database Database instance
30
     */
31
    public function __construct(DatabaseInterface $database)
32
    {
33
        $this->database = $database;
34
    }
35
36
    /**
37
     * @return \samsoncms\api\generator\metadata\GenericMetadata[]
38
     */
39
    public function analyze()
40
    {
41
42
    }
43
44
    /**
45
     * Get correct entity name.
46
     *
47
     * @param string $navigationName Original navigation entity name
48
     * @return string Correct PHP-supported entity name
49
     */
50
    protected function entityName($navigationName)
51
    {
52
        return ucfirst($this->getValidName($this->transliterated($navigationName)));
53
    }
54
55
    /**
56
     * Remove all wrong characters from entity name
57
     *
58
     * @param string $navigationName Original navigation entity name
59
     *
60
     * @return string Correct PHP-supported entity name
61
     */
62
    protected function getValidName($navigationName)
63
    {
64
        return preg_replace('/(^\d*)|([^\w\d_])/', '', $navigationName);
65
    }
66
67
    /**
68
     * Get correct field name.
69
     *
70
     * @param string $fieldName Original field name
71
     *
72
     * @return string Correct PHP-supported field name
73
     */
74
    protected function fieldName($fieldName)
75
    {
76
        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...
77
    }
78
    /**
79
     * Get correct full entity name with name space.
80
     *
81
     * @param string $navigationName Original navigation entity name
82
     * @param string $namespace Namespace
83
     * @return string Correct PHP-supported entity name
84
     */
85
    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...
86
    {
87
        return '\samsoncms\api\generated\\'.$this->entityName($navigationName);
88
    }
89
90
    /**
91
     * Get additional field type in form of Field constant name
92
     * by database additional field type identifier.
93
     *
94
     * @param integer $fieldType Additional field type identifier
95
     * @return string Additional field type constant
96
     */
97
    protected function additionalFieldType($fieldType)
98
    {
99
        return 'Field::'.$this->constantNameByValue($fieldType);
100
    }
101
102
    /**
103
     * Get class constant name by its value.
104
     *
105
     * @param string $value     Constant value
106
     * @param string $className Class name
107
     *
108
     * @return string Full constant name
109
     */
110
    protected function constantNameByValue($value, $className = Field::ENTITY)
111
    {
112
        // Get array where class constants are values and their values are keys
113
        $nameByValue = array_flip((new \ReflectionClass($className))->getConstants());
114
115
        // Try to find constant by its value
116
        if (null !== $nameByValue[$value]) {
117
            // Return constant name
118
            return $nameByValue[$value];
119
        }
120
121
        return '';
122
    }
123
124
    /**
125
     * Transliterate string to english.
126
     *
127
     * @param string $string Source string
128
     * @return string Transliterated string
129
     */
130
    protected function transliterated($string)
131
    {
132
        return str_replace(
133
            ' ',
134
            '',
135
            ucwords(iconv("UTF-8", "UTF-8//IGNORE", strtr($string, array(
136
                            "'" => "",
137
                            "`" => "",
138
                            "-" => " ",
139
                            "_" => " ",
140
                            "а" => "a", "А" => "a",
141
                            "б" => "b", "Б" => "b",
142
                            "в" => "v", "В" => "v",
143
                            "г" => "g", "Г" => "g",
144
                            "д" => "d", "Д" => "d",
145
                            "е" => "e", "Е" => "e",
146
                            "ж" => "zh", "Ж" => "zh",
147
                            "з" => "z", "З" => "z",
148
                            "и" => "i", "И" => "i",
149
                            "й" => "y", "Й" => "y",
150
                            "к" => "k", "К" => "k",
151
                            "л" => "l", "Л" => "l",
152
                            "м" => "m", "М" => "m",
153
                            "н" => "n", "Н" => "n",
154
                            "о" => "o", "О" => "o",
155
                            "п" => "p", "П" => "p",
156
                            "р" => "r", "Р" => "r",
157
                            "с" => "s", "С" => "s",
158
                            "т" => "t", "Т" => "t",
159
                            "у" => "u", "У" => "u",
160
                            "ф" => "f", "Ф" => "f",
161
                            "х" => "h", "Х" => "h",
162
                            "ц" => "c", "Ц" => "c",
163
                            "ч" => "ch", "Ч" => "ch",
164
                            "ш" => "sh", "Ш" => "sh",
165
                            "щ" => "sch", "Щ" => "sch",
166
                            "ъ" => "", "Ъ" => "",
167
                            "ы" => "y", "Ы" => "y",
168
                            "ь" => "", "Ь" => "",
169
                            "э" => "e", "Э" => "e",
170
                            "ю" => "yu", "Ю" => "yu",
171
                            "я" => "ya", "Я" => "ya",
172
                            "і" => "i", "І" => "i",
173
                            "ї" => "yi", "Ї" => "yi",
174
                            "є" => "e", "Є" => "e"
175
                        )
176
                    )
177
                )
178
            )
179
        );
180
    }
181
}
182
//[PHPCOMPRESSOR(remove,end)]