Completed
Pull Request — 4.2 (#140)
by David
08:44 queued 03:20
created

DefaultNamingStrategy::setBeanSuffix()   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
19
    /**
20
     * Sets the string prefix to any bean class name.
21
     *
22
     * @param string $beanPrefix
23
     */
24
    public function setBeanPrefix(string $beanPrefix)
25
    {
26
        $this->beanPrefix = $beanPrefix;
27
    }
28
29
    /**
30
     * Sets the string suffix to any bean class name.
31
     *
32
     * @param string $beanSuffix
33
     */
34
    public function setBeanSuffix(string $beanSuffix)
35
    {
36
        $this->beanSuffix = $beanSuffix;
37
    }
38
39
    /**
40
     * Sets the string prefix to any base bean class name.
41
     *
42
     * @param string $baseBeanPrefix
43
     */
44
    public function setBaseBeanPrefix(string $baseBeanPrefix)
45
    {
46
        $this->baseBeanPrefix = $baseBeanPrefix;
47
    }
48
49
    /**
50
     * Sets the string suffix to any base bean class name.
51
     *
52
     * @param string $baseBeanSuffix
53
     */
54
    public function setBaseBeanSuffix(string $baseBeanSuffix)
55
    {
56
        $this->baseBeanSuffix = $baseBeanSuffix;
57
    }
58
59
    /**
60
     * Sets the string prefix to any DAO class name.
61
     *
62
     * @param string $daoPrefix
63
     */
64
    public function setDaoPrefix(string $daoPrefix)
65
    {
66
        $this->daoPrefix = $daoPrefix;
67
    }
68
69
    /**
70
     * Sets the string suffix to any DAO class name.
71
     *
72
     * @param string $daoSuffix
73
     */
74
    public function setDaoSuffix(string $daoSuffix)
75
    {
76
        $this->daoSuffix = $daoSuffix;
77
    }
78
79
    /**
80
     * Sets the string prefix to any base DAO class name.
81
     *
82
     * @param string $baseDaoPrefix
83
     */
84
    public function setBaseDaoPrefix(string $baseDaoPrefix)
85
    {
86
        $this->baseDaoPrefix = $baseDaoPrefix;
87
    }
88
89
    /**
90
     * Sets the string suffix to any base DAO class name.
91
     *
92
     * @param string $baseDaoSuffix
93
     */
94
    public function setBaseDaoSuffix(string $baseDaoSuffix)
95
    {
96
        $this->baseDaoSuffix = $baseDaoSuffix;
97
    }
98
99
100
    /**
101
     * Returns the bean class name from the table name (excluding the namespace).
102
     *
103
     * @param string $tableName
104
     * @return string
105
     */
106
    public function getBeanClassName(string $tableName): string
107
    {
108
        return $this->beanPrefix.self::toSingularCamelCase($tableName).$this->beanSuffix;
109
    }
110
111
    /**
112
     * Returns the base bean class name from the table name (excluding the namespace).
113
     *
114
     * @param string $tableName
115
     * @return string
116
     */
117
    public function getBaseBeanClassName(string $tableName): string
118
    {
119
        return $this->baseBeanPrefix.self::toSingularCamelCase($tableName).$this->baseBeanSuffix;
120
    }
121
122
    /**
123
     * Returns the name of the DAO class from the table name (excluding the namespace).
124
     *
125
     * @param string $tableName
126
     * @return string
127
     */
128
    public function getDaoClassName(string $tableName): string
129
    {
130
        return $this->daoPrefix.self::toSingularCamelCase($tableName).$this->daoSuffix;
131
    }
132
133
    /**
134
     * Returns the name of the base DAO class from the table name (excluding the namespace).
135
     *
136
     * @param string $tableName
137
     * @return string
138
     */
139
    public function getBaseDaoClassName(string $tableName): string
140
    {
141
        return $this->baseDaoPrefix.self::toSingularCamelCase($tableName).$this->baseDaoSuffix;
142
    }
143
144
    /**
145
     * Tries to put string to the singular form (if it is plural) and camel case form.
146
     * We assume the table names are in english.
147
     *
148
     * @param $str string
149
     *
150
     * @return string
151
     */
152
    private static function toSingularCamelCase(string $str): string
153
    {
154
        $tokens = preg_split("/[_ ]+/", $str);
155
        $tokens = array_map([Inflector::class, 'singularize'], $tokens);
156
157
        $str = '';
158
        foreach ($tokens as $token) {
159
            $str .= ucfirst(Inflector::singularize($token));
160
        }
161
162
        return $str;
163
    }
164
165
    /**
166
     * Returns the class name for the DAO factory.
167
     *
168
     * @return string
169
     */
170
    public function getDaoFactoryClassName(): string
171
    {
172
        return 'DaoFactory';
173
    }
174
}
175