Passed
Push — 4.3 ( 66bb88...1c6faa )
by David
01:13
created

DefaultNamingStrategy::setExceptions()   A

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 1
1
<?php
2
3
4
namespace Mouf\Database\TDBM\Utils;
5
6
use Doctrine\Common\Inflector\Inflector;
7
8
class DefaultNamingStrategy implements NamingStrategyInterface
9
{
10
    private $beanPrefix = '';
11
    private $beanSuffix = '';
12
    private $baseBeanPrefix = 'Abstract';
13
    private $baseBeanSuffix = '';
14
    private $daoPrefix = '';
15
    private $daoSuffix = 'Dao';
16
    private $baseDaoPrefix = 'Abstract';
17
    private $baseDaoSuffix = 'Dao';
18
    private $exceptions = [];
19
20
    /**
21
     * Sets the string prefix to any bean class name.
22
     *
23
     * @param string $beanPrefix
24
     */
25
    public function setBeanPrefix(string $beanPrefix)
26
    {
27
        $this->beanPrefix = $beanPrefix;
28
    }
29
30
    /**
31
     * Sets the string suffix to any bean class name.
32
     *
33
     * @param string $beanSuffix
34
     */
35
    public function setBeanSuffix(string $beanSuffix)
36
    {
37
        $this->beanSuffix = $beanSuffix;
38
    }
39
40
    /**
41
     * Sets the string prefix to any base bean class name.
42
     *
43
     * @param string $baseBeanPrefix
44
     */
45
    public function setBaseBeanPrefix(string $baseBeanPrefix)
46
    {
47
        $this->baseBeanPrefix = $baseBeanPrefix;
48
    }
49
50
    /**
51
     * Sets the string suffix to any base bean class name.
52
     *
53
     * @param string $baseBeanSuffix
54
     */
55
    public function setBaseBeanSuffix(string $baseBeanSuffix)
56
    {
57
        $this->baseBeanSuffix = $baseBeanSuffix;
58
    }
59
60
    /**
61
     * Sets the string prefix to any DAO class name.
62
     *
63
     * @param string $daoPrefix
64
     */
65
    public function setDaoPrefix(string $daoPrefix)
66
    {
67
        $this->daoPrefix = $daoPrefix;
68
    }
69
70
    /**
71
     * Sets the string suffix to any DAO class name.
72
     *
73
     * @param string $daoSuffix
74
     */
75
    public function setDaoSuffix(string $daoSuffix)
76
    {
77
        $this->daoSuffix = $daoSuffix;
78
    }
79
80
    /**
81
     * Sets the string prefix to any base DAO class name.
82
     *
83
     * @param string $baseDaoPrefix
84
     */
85
    public function setBaseDaoPrefix(string $baseDaoPrefix)
86
    {
87
        $this->baseDaoPrefix = $baseDaoPrefix;
88
    }
89
90
    /**
91
     * Sets the string suffix to any base DAO class name.
92
     *
93
     * @param string $baseDaoSuffix
94
     */
95
    public function setBaseDaoSuffix(string $baseDaoSuffix)
96
    {
97
        $this->baseDaoSuffix = $baseDaoSuffix;
98
    }
99
100
101
    /**
102
     * Returns the bean class name from the table name (excluding the namespace).
103
     *
104
     * @param string $tableName
105
     * @return string
106
     */
107
    public function getBeanClassName(string $tableName): string
108
    {
109
        return $this->beanPrefix.$this->toSingularCamelCase($tableName).$this->beanSuffix;
110
    }
111
112
    /**
113
     * Returns the base bean class name from the table name (excluding the namespace).
114
     *
115
     * @param string $tableName
116
     * @return string
117
     */
118
    public function getBaseBeanClassName(string $tableName): string
119
    {
120
        return $this->baseBeanPrefix.$this->toSingularCamelCase($tableName).$this->baseBeanSuffix;
121
    }
122
123
    /**
124
     * Returns the name of the DAO class from the table name (excluding the namespace).
125
     *
126
     * @param string $tableName
127
     * @return string
128
     */
129
    public function getDaoClassName(string $tableName): string
130
    {
131
        return $this->daoPrefix.$this->toSingularCamelCase($tableName).$this->daoSuffix;
132
    }
133
134
    /**
135
     * Returns the name of the base DAO class from the table name (excluding the namespace).
136
     *
137
     * @param string $tableName
138
     * @return string
139
     */
140
    public function getBaseDaoClassName(string $tableName): string
141
    {
142
        return $this->baseDaoPrefix.$this->toSingularCamelCase($tableName).$this->baseDaoSuffix;
143
    }
144
145
    /**
146
     * Tries to put string to the singular form (if it is plural) and camel case form.
147
     * We assume the table names are in english.
148
     *
149
     * @param $str string
150
     *
151
     * @return string
152
     */
153
    private function toSingularCamelCase(string $str): string
154
    {
155
        // Let's first check if this is not in the exceptions directory.
156
        if (isset($this->exceptions[$str])) {
157
            return $this->exceptions[$str];
158
        }
159
160
        $tokens = preg_split("/[_ ]+/", $str);
161
        $tokens = array_map([Inflector::class, 'singularize'], $tokens);
162
163
        $str = '';
164
        foreach ($tokens as $token) {
165
            $str .= ucfirst(Inflector::singularize($token));
166
        }
167
168
        return $str;
169
    }
170
171
    /**
172
     * Returns the class name for the DAO factory.
173
     *
174
     * @return string
175
     */
176
    public function getDaoFactoryClassName(): string
177
    {
178
        return 'DaoFactory';
179
    }
180
181
    /**
182
     * Sets exceptions in the naming of classes.
183
     * The key is the name of the table, the value the "base" name of beans and DAOs.
184
     *
185
     * This is very useful for dealing with plural to singular translations in non english table names.
186
     *
187
     * For instance if you are dealing with a table containing horses in French ("chevaux" that has a singular "cheval"):
188
     *
189
     * [
190
     *     "chevaux" => "Cheval"
191
     * ]
192
     *
193
     * @param array<string,string> $exceptions
194
     */
195
    public function setExceptions(array $exceptions)
196
    {
197
        $this->exceptions = $exceptions;
198
    }
199
}
200