midgard_reflection_property::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
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
use midgard\portable\mapping\classmetadata;
12
13
class midgard_reflection_property
14
{
15
    private readonly classmetadata $cm;
16
17 12
    public function __construct(string $mgdschema_class)
18
    {
19
        // we might get a proxy class, so we need to translate
20 12
        $classname = ClassUtils::getRealClass($mgdschema_class);
21 12
        $cmf = connection::get_em()->getMetadataFactory();
22 12
        if (!$cmf->hasMetadataFor($classname)) {
23 6
            $classname = connection::get_fqcn($mgdschema_class);
24
        }
25 12
        $this->cm = $cmf->getMetadataFor($classname);
0 ignored issues
show
Bug introduced by
The property cm is declared read-only in midgard_reflection_property.
Loading history...
26
    }
27
28
    /**
29
     * @param string $property The property name
30
     * @param boolean $metadata Check metadata properties instead
31
     * @return boolean Indicating existence
32
     */
33 1
    public function property_exists(string $property, bool $metadata = false) : bool
34
    {
35 1
        if ($metadata) {
36 1
            $property = 'metadata_' . $property;
37
        }
38 1
        return $this->cm->hasField($property) || $this->cm->hasAssociation($property) || array_key_exists($property, $this->cm->midgard['field_aliases']);
39
    }
40
41
    /**
42
     * Returns field's description, if any
43
     */
44 1
    public function description(string $property) : ?string
45
    {
46 1
        return $this->get_mapping($property)['midgard:description'] ?? null;
47
    }
48
49 6
    private function get_mapping(string $property) : ?array
50
    {
51 6
        if (!$this->cm->hasField($property)) {
52 4
            return null;
53
        }
54 6
        return $this->cm->getFieldMapping($property);
55
    }
56
57
    /**
58
     * Is this field a link or not
59
     */
60 7
    public function is_link(string $property) : bool
61
    {
62 7
        return $this->cm->hasAssociation($property) || $this->is_special_link($property);
63
    }
64
65 7
    public function is_special_link(string $property) : bool
66
    {
67 7
        if ($this->cm->hasAssociation($property)) {
68 5
            return false;
69
        }
70 2
        return !empty($this->get_mapping($property)["noidlink"]);
71
    }
72
73
    /**
74
     * Returns the classname for the link target
75
     */
76 7
    public function get_link_name(string $property) : ?string
77
    {
78 7
        if ($this->cm->hasAssociation($property)) {
79 6
            $mapping = $this->cm->getAssociationMapping($property);
80 6
            return $mapping['midgard:link_name'];
81
        }
82
83 2
        return $this->get_mapping($property)["noidlink"]["target"] ?? null;
84
    }
85
86
    /**
87
     * Returns the target field name
88
     */
89 2
    public function get_link_target(string $property) : ?string
90
    {
91 2
        if ($this->cm->hasAssociation($property)) {
92 1
            $mapping = $this->cm->getAssociationMapping($property);
93 1
            return $mapping['midgard:link_target'];
94
        }
95
96 2
        return $this->get_mapping($property)["noidlink"]["field"] ?? null;
97
    }
98
99
    /**
100
     * Returns field type constant
101
     */
102 1
    public function get_midgard_type(string $property) : int
103
    {
104 1
        if ($this->cm->hasField($property)) {
105 1
            return $this->get_mapping($property)['midgard:midgard_type'];
106
        }
107 1
        if ($this->cm->hasAssociation($property)) {
108
            // for now, only PK fields are supported, which are always IDs, so..
109 1
            return translator::TYPE_UINT;
110
        }
111 1
        return translator::TYPE_NONE;
112
    }
113
}
114