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