Completed
Push — master ( 1857e6...d96bec )
by Joao Gilberto
03:02
created

ClassAnnotations::getClassNamespace()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 5
eloc 13
nc 3
nop 0
1
<?php
2
3
namespace ByJG\AnyDataset\Model;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
use stdClass;
8
9
class ClassAnnotations extends Annotations
10
{
11
    protected $classRefl;
12
    protected $className;
13
    protected $classGetter;
14
    protected $classPropertyPattern;
15
    protected $classWriteEmpty;
16
    protected $classDocType;
17
    protected $classRdfType;
18
    protected $classRdfAbout;
19
    protected $classDefaultPrefix;
20
    protected $classIsRDF;
21
    protected $classIgnoreAllClass;
22
    protected $classNamespace;
23
    protected $classDontCreateClassNode;
24
25
    protected $propertyList;
26
    protected $propertyAnnotations = [];
27
28
    protected $model;
29
    protected $forcePropName;
30
31
    public function __construct($model, $config = "object", $forcePropName = "")
32
    {
33
        $this->model = $model;
34
        $this->config = $config;
35
        $this->forcePropName = $forcePropName;
36
37
        $this->setClassRefl(new \ReflectionClass(new \stdClass));
38
        $this->annotations = [];
39
40
        if (is_array($model)) {
41
            $this->model = new stdClass();
42
            foreach ($model as $key => $value) {
43
                if (is_numeric($key)) {
44
                    $key = "__{$key}__";
45
                }
46
                $this->model->{$key} = $value;
47
            }
48
        }
49
50
        if (!$this->model instanceof stdClass) {
51
            $class = new ReflectionClass($this->model);
52
53
            $aux = null;
54
            preg_match_all('/@(?P<param>\S+)\s*(?P<value>\S+)?\r?\n/', $class->getDocComment(), $aux);
55
            $this->annotations = $this->adjustParams($aux);
56
            $this->setClassRefl($class);
57
        }
58
    }
59
60
    public function getPropertyList()
61
    {
62
        if (!$this->propertyList) {
63
            $this->propertyList = [];
64
            if ($this->model instanceof stdClass) {
65
                $this->propertyList = get_object_vars($this->model);
66
            } else {
67
                $this->propertyList = $this->getClassRefl()->getProperties(
68
                    ReflectionProperty::IS_PROTECTED |
69
                    ReflectionProperty::IS_PRIVATE   |
70
                    ReflectionProperty::IS_PUBLIC
71
                );
72
            }
73
        }
74
75
        return $this->propertyList;
76
    }
77
78
    /**
79
     *
80
     * @param ReflectionProperty $property
81
     * @param string $keyProp
82
     * @return PropertyAnnotations
83
     */
84
    public function getPropertyAnnotation($property, $keyProp = null)
85
    {
86
        $propName = ($property instanceof \ReflectionProperty) ? $property->getName() : $keyProp;
87
        if (!isset($this->propertyAnnotations[$propName])) {
88
            $this->propertyAnnotations[$propName] = new PropertyAnnotations($this, $this->config, $property, $propName);
89
        }
90
91
        return $this->propertyAnnotations[$propName];
92
    }
93
94
    public function getClassInstance()
95
    {
96
        return $this->model;
97
    }
98
99
    public function getClassRefl()
100
    {
101
        return $this->classRefl;
102
    }
103
104
    public function getClassName()
105
    {
106
        if (!$this->className) {
107
            $this->className = $this->forcePropName;
108
            if (empty($this->className)) {
109
                $this->className = $this->getAnnotations("nodename", get_class($this->model));
110
            }
111
        }
112
        return $this->className;
113
    }
114
115
    public function getClassGetter()
116
    {
117
        if (!$this->classGetter) {
118
            $this->classGetter = $this->getAnnotations("getter", "get");
119
        }
120
        return $this->classGetter;
121
    }
122
123
    public function getClassPropertyPattern($id = null)
124
    {
125
        if (!$this->classPropertyPattern) {
126
            $this->classPropertyPattern = $this->getAnnotations("propertypattern", ['/([^a-zA-Z0-9])/', '']);
127
        }
128
129
        if (!is_null($id)) {
130
            return $this->classPropertyPattern[$id];
131
        }
132
133
        return $this->classPropertyPattern;
134
    }
135
136
    public function getClassWriteEmpty()
137
    {
138
        if (!$this->classWriteEmpty) {
139
            $this->classWriteEmpty = $this->getAnnotations("writeempty", false);
140
        }
141
        return $this->classWriteEmpty;
142
    }
143
144
    public function getClassDocType()
145
    {
146
        if (!$this->classDocType) {
147
            $this->classDocType = $this->getAnnotations("doctype", "xml");
148
        }
149
        return $this->classDocType;
150
    }
151
152 View Code Duplication
    public function getClassRdfType()
0 ignored issues
show
Duplication introduced by Joao Gilberto Magalhaes
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if (!$this->classRdfType) {
155
            $rdfType = $this->getAnnotations("rdftype", "{HOST}/rdf/class/{CLASS}");
156
            $this->classRdfType = $this->replaceVars($this->getClassName(), $rdfType);
157
        }
158
        return $this->classRdfType;
159
    }
160
161 View Code Duplication
    public function getClassRdfAbout()
0 ignored issues
show
Duplication introduced by Joao Gilberto Magalhaes
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        if (!$this->classRdfAbout) {
164
            $rdfAbout = $this->getAnnotations("rdfabout", "{HOST}/rdf/instance/{CLASS}/{GetID()}");
165
            $this->classRdfAbout = $this->replaceVars($this->getClassName(), $rdfAbout);
166
        }
167
        return $this->classRdfAbout;
168
    }
169
170
    public function getClassDefaultPrefix()
171
    {
172
        if (!$this->classDefaultPrefix) {
173
            $this->classDefaultPrefix = $this->getAnnotations("defaultprefix", "");
174
        }
175
        return $this->classDefaultPrefix;
176
    }
177
178
    public function getClassIsRDF()
179
    {
180
        if (!$this->classIsRDF) {
181
            $this->classIsRDF = ($this->getClassDocType() == "rdf");
182
        }
183
        return $this->classIsRDF;
184
    }
185
186
    public function getClassIgnoreAllClass()
187
    {
188
        if (!$this->classIgnoreAllClass) {
189
            $this->classIgnoreAllClass = $this->getAnnotations("ignore", false);
190
        }
191
        return $this->classIgnoreAllClass;
192
    }
193
194
    public function getClassNamespace()
195
    {
196
        if (!$this->classNamespace) {
197
            $this->classNamespace = $this->getAnnotations("namespace", []);
198
            if (!is_array($this->classNamespace) && !empty($this->classNamespace)) {
199
                $this->classNamespace = [ $this->classNamespace];
200
            }
201
202
            foreach ($this->classNamespace as $key => $value) {
203
                $prefix = strtok($value, "!");
204
                $uri = str_replace($prefix . "!", "", $value);
205
206
                $this->classNamespace[$key] = [
207
                    $this->classNamespace[$key],
208
                    "prefix" => $prefix,
209
                    "uri" => $this->replaceVars($this->classNamespace[$key], $uri)
210
                ];
211
            }
212
        }
213
        return $this->classNamespace;
214
    }
215
216
    public function getClassDontCreateClassNode()
217
    {
218
        if (!$this->classDontCreateClassNode) {
219
            $this->classDontCreateClassNode = $this->getAnnotations("dontcreatenode", false);
220
        }
221
        return $this->classDontCreateClassNode;
222
    }
223
224
225
    public function setClassRefl($classRefl)
226
    {
227
        $this->classRefl = $classRefl;
228
    }
229
230
}
231