Completed
Push — master ( 23d1df...87df34 )
by Andreas
16:32
created

midgard_reflection_property::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
use Doctrine\Common\Util\ClassUtils;
9
use midgard\portable\storage\connection;
10
use midgard\portable\mgdschema\translator;
11
12
class midgard_reflection_property
13
{
14
    /**
15
     * @var midgard\portable\mapping\classmetadata
16
     */
17
    private $cm;
18
19 12
    public function __construct($mgdschema_class)
20
    {
21
        // we might get a proxy class, so we need to translate
22 12
        $classname = ClassUtils::getRealClass($mgdschema_class);
23 12
        $cmf = connection::get_em()->getMetadataFactory();
24 12
        if (!$cmf->hasMetadataFor($classname)) {
25 6
            $classname = 'midgard:' . $mgdschema_class;
26 6
        }
27 12
        $this->cm = $cmf->getMetadataFor($classname);
28 12
    }
29
30
    /**
31
     * @param string $property The property name
32
     * @param boolean $metadata Check metadata properties instead
33
     * @return boolean Indicating existence
34
     */
35 1
    public function property_exists($property, $metadata = false)
36
    {
37 1
        if ($metadata) {
38 1
            $property = 'metadata_' . $property;
39 1
        }
40 1
        return ($this->cm->hasField($property) || $this->cm->hasAssociation($property) || array_key_exists($property, $this->cm->midgard['field_aliases']));
41
    }
42
43
    /**
44
     * Returns field's description, if any
45
     *
46
     * @param string $property
47
     * @return string|NULL
48
     */
49 1
    public function description($property)
50
    {
51 1
        if (!$this->cm->hasField($property)) {
52 1
            return null;
53
        }
54 1
        $mapping = $this->cm->getFieldMapping($property);
55 1
        return $mapping['midgard:description'];
56
    }
57
58 9
    public function get_mapping($property)
59
    {
60 9
        if (!$this->cm->hasField($property)) {
61 8
            return null;
62
        }
63 4
        return $this->cm->getFieldMapping($property);
64
    }
65
66
    /**
67
     * Is this field a link or not
68
     *
69
     * @param string $property
70
     * @return boolean
71
     */
72 7
    public function is_link($property)
73
    {
74 7
        if ($this->cm->hasAssociation($property)) {
75 6
            return true;
76
        }
77 2
        return $this->is_special_link($property);
78
    }
79
80 7
    public function is_special_link($property)
81
    {
82 7
        $mapping = $this->get_mapping($property);
83 7
        if ($this->cm->hasAssociation($property) || is_null($mapping)) {
84 6
            return false;
85
        }
86 2
        return isset($mapping["noidlink"]);
87
    }
88
89
    /**
90
     * Returns the classname for the link target
91
     *
92
     * @param string $property
93
     * @return string|NULL
94
     */
95 7 View Code Duplication
    public function get_link_name($property)
0 ignored issues
show
Duplication introduced by
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...
96
    {
97 7
        if ($this->cm->hasAssociation($property)) {
98 6
            $mapping = $this->cm->getAssociationMapping($property);
99 6
            return $mapping['midgard:link_name'];
100
        }
101 2
        $mapping = $this->get_mapping($property);
102 2
        if (is_null($mapping)) {
103 1
            return null;
104
        }
105 2
        if (isset($mapping["noidlink"]["target"])) {
106 1
            return $mapping["noidlink"]["target"];
107
        }
108 1
        return null;
109
    }
110
111
    /**
112
     * Returns the target field name
113
     *
114
     * @param string $property
115
     * @return string|NULL
116
     */
117 2 View Code Duplication
    public function get_link_target($property)
0 ignored issues
show
Duplication introduced by
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...
118
    {
119 2
        if ($this->cm->hasAssociation($property)) {
120 1
            $mapping = $this->cm->getAssociationMapping($property);
121 1
            return $mapping['midgard:link_target'];
122
        }
123 2
        $mapping = $this->get_mapping($property);
124 2
        if (is_null($mapping)) {
125 1
            return null;
126
        }
127 2
        if (isset($mapping["noidlink"]["field"])) {
128 1
            return $mapping["noidlink"]["field"];
129
        }
130 1
        return null;
131
    }
132
133
    /**
134
     * Returns field type constant
135
     *
136
     * @param string $property
137
     * @return integer
138
     */
139 1
    public function get_midgard_type($property)
140
    {
141 1
        if ($this->cm->hasField($property)) {
142 1
            $mapping = $this->cm->getFieldMapping($property);
143 1
            return $mapping['midgard:midgard_type'];
144 1
        } elseif ($this->cm->hasAssociation($property)) {
145
            // for now, only PK fields are supported, which are always IDs, so..
146 1
            return translator::TYPE_UINT;
147
        }
148 1
        return translator::TYPE_NONE;
149
    }
150
}
151