Passed
Pull Request — master (#116)
by David
03:32
created

isAlternativeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\TDBM\Utils;
5
6
use Doctrine\DBAL\Schema\Table;
7
use Zend\Code\Generator\DocBlock\Tag\ParamTag;
8
use Zend\Code\Generator\MethodGenerator;
9
10
/**
11
 * This class represent a property in a bean (a property has a getter, a setter, etc...).
12
 */
13
abstract class AbstractBeanPropertyDescriptor
14
{
15
    /**
16
     * @var Table
17
     */
18
    protected $table;
19
20
    /**
21
     * Whether to use the more complex name in case of conflict.
22
     *
23
     * @var bool
24
     */
25
    protected $alternativeName = false;
26
    /**
27
     * @var NamingStrategyInterface
28
     */
29
    protected $namingStrategy;
30
31
    /**
32
     * @param Table $table
33
     * @param NamingStrategyInterface $namingStrategy
34
     */
35
    public function __construct(Table $table, NamingStrategyInterface $namingStrategy)
36
    {
37
        $this->table = $table;
38
        $this->namingStrategy = $namingStrategy;
39
    }
40
41
    /**
42
     * Use the more complex name in case of conflict.
43
     */
44
    public function useAlternativeName(): void
45
    {
46
        $this->alternativeName = true;
47
    }
48
49
    /**
50
     * Returns the name of the class linked to this property or null if this is not a foreign key.
51
     *
52
     * @return null|string
53
     */
54
    abstract public function getClassName(): ?string;
55
56
    /**
57
     * Returns the PHP type for the property (it can be a scalar like int, bool, or class names, like \DateTimeInterface, App\Bean\User....)
58
     *
59
     * @return string
60
     */
61
    abstract public function getPhpType(): string;
62
63
    /**
64
     * Returns the param annotation for this property (useful for constructor).
65
     *
66
     * @return ParamTag
67
     */
68
    public function getParamAnnotation(): ParamTag
69
    {
70
        return new ParamTag($this->getVariableName(), [ $this->getPhpType() ]);
71
    }
72
73
    public function getVariableName(): string
74
    {
75
        return $this->namingStrategy->getVariableName($this);
76
    }
77
78
    public function getSetterName(): string
79
    {
80
        return $this->namingStrategy->getSetterName($this);
81
    }
82
83
    public function getGetterName(): string
84
    {
85
        return $this->namingStrategy->getGetterName($this);
86
    }
87
88
    /**
89
     * Returns the PHP code used in the ben constructor for this property.
90
     *
91
     * @return string
92
     */
93
    public function getConstructorAssignCode(): string
94
    {
95
        $str = '$this->%s(%s);';
96
97
        return sprintf($str, $this->getSetterName(), $this->getVariableName());
98
    }
99
100
    /**
101
     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
102
     *
103
     * @return bool
104
     */
105
    abstract public function isCompulsory(): bool;
106
107
    /**
108
     * Returns true if the property has a default value.
109
     *
110
     * @return bool
111
     */
112
    abstract public function hasDefault(): bool;
113
114
    /**
115
     * Returns the code that assigns a value to its default value.
116
     *
117
     * @return string
118
     */
119
    abstract public function assignToDefaultCode(): string;
120
121
    /**
122
     * Returns true if the property is the primary key.
123
     *
124
     * @return bool
125
     */
126
    abstract public function isPrimaryKey(): bool;
127
128
    /**
129
     * @return Table
130
     */
131
    public function getTable(): Table
132
    {
133
        return $this->table;
134
    }
135
136
    /**
137
     * Returns the PHP code for getters and setters.
138
     *
139
     * @return MethodGenerator[]
140
     */
141
    abstract public function getGetterSetterCode(): array;
142
143
    /**
144
     * Returns the part of code useful when doing json serialization.
145
     *
146
     * @return string
147
     */
148
    abstract public function getJsonSerializeCode(): string;
149
150
    /**
151
     * @return bool
152
     */
153
    public function isAlternativeName(): bool
154
    {
155
        return $this->alternativeName;
156
    }
157
158
    /**
159
     * The code to past in the __clone method.
160
     * @return null|string
161
     */
162
    abstract public function getCloneRule(): ?string;
163
164
    /**
165
     * Tells if this property is a type-hintable in PHP (resource isn't for example)
166
     *
167
     * @return bool
168
     */
169
    abstract public function isTypeHintable() : bool;
170
}
171