Prop::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 5
1
<?php
2
3
namespace Saxulum\Accessor;
4
5
use Saxulum\Hint\Hint;
6
7
class Prop
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * @var string|null
16
     */
17
    protected $hint;
18
19
    /**
20
     * @var bool|null
21
     */
22
    protected $nullable;
23
24
    /**
25
     * @var null|string
26
     */
27
    protected $mappedBy;
28
29
    /**
30
     * @var null|string
31
     */
32
    protected $mappedType;
33
34
    const REMOTE_ONE = 'one';
35
    const REMOTE_MANY = 'many';
36
37
    /**
38
     * @var string[]
39
     */
40
    protected $accessorPrefixes = array();
41
42
    /**
43
     * @param $name
44
     * @param string|null $hint
45
     * @param bool|null   $nullable
46
     * @param string|null $mappedBy
47
     * @param string|null $mappedType
48
     */
49
    public function __construct($name, $hint = null, $nullable = null, $mappedBy = null, $mappedType = null)
50
    {
51
        $this->name = $name;
52
        $this->hint = $hint;
53
        $this->nullable = $nullable;
54
        $this->mappedBy = $mappedBy;
55
        $this->mappedType = $mappedType;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getName()
62
    {
63
        return $this->name;
64
    }
65
66
    /**
67
     * @return null|string
68
     */
69
    public function getHint()
70
    {
71
        return $this->hint;
72
    }
73
74
    /**
75
     * @return bool|null
76
     */
77
    public function getNullable()
78
    {
79
        return $this->nullable;
80
    }
81
82
    /**
83
     * @return null|string
84
     */
85
    public function getMappedBy()
86
    {
87
        return $this->mappedBy;
88
    }
89
90
    /**
91
     * @return null|string
92
     */
93
    public function getMappedType()
94
    {
95
        return $this->mappedType;
96
    }
97
98
    /**
99
     * @param  string $accessorPrefix
100
     * @return self
101
     */
102
    public function method($accessorPrefix)
103
    {
104
        if (!in_array($accessorPrefix, $this->accessorPrefixes, true)) {
105
            $this->accessorPrefixes[] = $accessorPrefix;
106
        }
107
108
        return $this;
109
    }
110
111
    /**
112
     * @param  string $accessorPrefix
113
     * @return bool
114
     */
115
    public function hasMethod($accessorPrefix)
116
    {
117
        return in_array($accessorPrefix, $this->accessorPrefixes, true);
118
    }
119
120
    /**
121
     * @param  string $namespace
122
     * @return string
123
     */
124
    public function generatePhpDoc($namespace)
125
    {
126
        $phpdoc = '';
127
        foreach ($this->accessorPrefixes as $accessorPrefix) {
128
            $accessor = AccessorRegistry::getAccessor($accessorPrefix);
129
            $phpdoc .= $accessor->generatePhpDoc($this, $namespace). "\n";
130
        }
131
132
        return $phpdoc;
133
    }
134
135
    /**
136
     * @param  string $namespace
137
     * @return string
138
     */
139
    public function getPhpDocHint($namespace)
140
    {
141
        if (null === $this->getHint()) {
142
            return '';
143
        }
144
145
        $hint = Hint::NUMERIC !== $this->getHint() ? $this->getHint() : Hint::FLOAT;
146
147
        return $this->reduceHint($hint, $namespace);
148
    }
149
150
    /**
151
     * @param  string $hint
152
     * @param  string $namespace
153
     * @return string
154
     */
155
    protected function reduceHint($hint, $namespace)
156
    {
157
        $pos = strrpos($hint, '\\');
158
159
        if (false !== $pos && 0 !== $pos && substr($hint, 0, $pos) === $namespace) {
160
            return substr($hint, $pos + 1);
161
        }
162
163
        return (string) $hint;
164
    }
165
}
166