1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleCrud; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to create instances of fields. |
7
|
|
|
*/ |
8
|
|
|
class FieldFactory implements FieldFactoryInterface |
9
|
|
|
{ |
10
|
|
|
protected $namespaces = ['SimpleCrud\\Fields\\']; |
11
|
|
|
protected $defaultType = 'Field'; |
12
|
|
|
|
13
|
|
|
protected $nameMap = [ |
14
|
|
|
'id' => 'Integer', |
15
|
|
|
'active' => 'Boolean', |
16
|
|
|
'pubdate' => 'Datetime', |
17
|
|
|
'file' => 'File', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
protected $regexMap = [ |
21
|
|
|
//relation fields (post_id) |
22
|
|
|
'/_id$/' => 'Integer', |
23
|
|
|
|
24
|
|
|
//flags (isActive, inHome) |
|
|
|
|
25
|
|
|
'/^(is|has)[A-Z]/' => 'Boolean', |
26
|
|
|
|
27
|
|
|
//time related (createdAt, publishedAt) |
28
|
|
|
'/[a-z]At$/' => 'Datetime', |
29
|
|
|
|
30
|
|
|
//time related (createdAt, publishedAt) |
31
|
|
|
'/[a-z]File$/' => 'File', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
protected $typeMap = [ |
35
|
|
|
'bigint' => 'Integer', |
36
|
|
|
'boolean' => 'Boolean', |
37
|
|
|
'date' => 'Date', |
38
|
|
|
'datetime' => 'Datetime', |
39
|
|
|
'float' => 'Decimal', |
40
|
|
|
'mediumint' => 'Integer', |
41
|
|
|
'set' => 'Set', |
42
|
|
|
'smallint' => 'Integer', |
43
|
|
|
'tinyint' => 'Integer', |
44
|
|
|
'year' => 'Integer', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the namespace for the fields classes. |
49
|
|
|
* |
50
|
|
|
* @param string $namespace |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
|
|
public function addNamespace($namespace) |
55
|
|
|
{ |
56
|
|
|
array_unshift($this->namespaces, $namespace); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Map names with field types. |
63
|
|
|
* |
64
|
|
|
* @param array $map |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
|
|
public function mapNames(array $map) |
69
|
|
|
{ |
70
|
|
|
$this->nameMap = $map + $this->nameMap; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Map names with field types using regexp. |
77
|
|
|
* |
78
|
|
|
* @param array $map |
79
|
|
|
* |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
|
|
public function mapRegex(array $map) |
83
|
|
|
{ |
84
|
|
|
$this->regexMap = $map + $this->regexMap; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Map db field types with classes. |
91
|
|
|
* |
92
|
|
|
* @param array $map |
93
|
|
|
* |
94
|
|
|
* @return self |
95
|
|
|
*/ |
96
|
|
|
public function mapTypes(array $map) |
97
|
|
|
{ |
98
|
|
|
$this->mapTypes = $map + $this->mapTypes; |
|
|
|
|
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @see FieldFactoryInterface |
105
|
|
|
* |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function get(Table $table, $name) |
109
|
|
|
{ |
110
|
|
|
$scheme = $table->getScheme()['fields']; |
111
|
|
|
|
112
|
|
|
if (!isset($scheme[$name])) { |
113
|
|
|
throw new SimpleCrudException("The field '{$name}' does not exist in the table {$table->name}"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
$className = $this->getClassName($name, $scheme[$name]['type']) ?: $this->defaultType; |
118
|
|
|
|
119
|
|
|
return $this->getInstance($table, $className, $name); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @see FieldFactoryInterface |
124
|
|
|
* |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function getInstance(Table $table, $className, $name) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
foreach ($this->namespaces as $namespace) { |
130
|
|
|
$class = $namespace.$className; |
131
|
|
|
|
132
|
|
|
if (class_exists($class)) { |
133
|
|
|
return new $class($table, $name); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
throw new SimpleCrudException("No field class found for '{$className}'"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the field class name. |
142
|
|
|
* |
143
|
|
|
* @param string $name |
144
|
|
|
* @param string $type |
145
|
|
|
* |
146
|
|
|
* @return string|null |
147
|
|
|
*/ |
148
|
|
|
protected function getClassName($name, $type) |
149
|
|
|
{ |
150
|
|
|
if (isset($this->nameMap[$name])) { |
151
|
|
|
return $this->nameMap[$name]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
foreach ($this->regexMap as $regex => $class) { |
155
|
|
|
if (preg_match($regex, $name)) { |
156
|
|
|
return $class; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (isset($this->typeMap[$type])) { |
161
|
|
|
return $this->typeMap[$type]; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.