RelationshipMetadata::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 4
1
<?php
2
3
namespace As3\Modlr\Metadata;
4
5
use As3\Modlr\Exception\MetadataException;
6
7
/**
8
 * Defines metadata for a relationship field.
9
 * Should be loaded using the MetadataFactory, not instantiated directly.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
class RelationshipMetadata extends FieldMetadata
14
{
15
    /**
16
     * The entity type this is related to.
17
     *
18
     * @var string
19
     */
20
    public $entityType;
21
22
    /**
23
     * The relationship type: one or many
24
     *
25
     * @var string
26
     */
27
    public $relType;
28
29
    /**
30
     * Determines if this is an inverse (non-owning) relationship.
31
     *
32
     * @var bool
33
     */
34
    public $isInverse = false;
35
36
    /**
37
     * The inverse field.
38
     *
39
     * @var bool
40
     */
41
    public $inverseField;
42
43
    /**
44
     * Determines if the relationship related to a polymorphic entity.
45
     *
46
     * @var bool
47
     */
48
    public $polymorphic = false;
49
50
    /**
51
     * Child entity types the related entity owns.
52
     * Only used for polymorphic relationships.
53
     */
54
    public $ownedTypes = [];
55
56
    /**
57
     * Constructor.
58
     *
59
     * @param   string  $key        The relationship field key.
60
     * @param   string  $relType    The relationship type.
61
     * @param   string  $entityType The entity type key.
62
     * @param   bool    $mixin
63
     */
64
    public function __construct($key, $relType, $entityType, $mixin = false)
65
    {
66
        parent::__construct($key, $mixin);
67
        $this->setRelType($relType);
68
        $this->entityType = $entityType;
69
    }
70
71
    /**
72
     * Gets the entity type that this field is related to.
73
     *
74
     * @return  string
75
     */
76
    public function getEntityType()
77
    {
78
        return $this->entityType;
79
    }
80
81
    /**
82
     * Gets the relationship type.
83
     *
84
     * @return  string
85
     */
86
    public function getRelType()
87
    {
88
        return $this->relType;
89
    }
90
91
    /**
92
     * Determines if this is a one (single) relationship.
93
     *
94
     * @return  bool
95
     */
96
    public function isOne()
97
    {
98
        return 'one' === $this->getRelType();
99
    }
100
101
    /**
102
     * Determines if this is a many relationship.
103
     *
104
     * @return bool
105
     */
106
    public function isMany()
107
    {
108
        return 'many' === $this->getRelType();
109
    }
110
111
    /**
112
     * Determines whether the relationship is polymorphic.
113
     *
114
     * @return  bool
115
     */
116
    public function isPolymorphic()
117
    {
118
        return $this->polymorphic;
119
    }
120
121
    public function setPolymorphic($bit = true)
122
    {
123
        $this->polymorphic = (Boolean) $bit;
124
        return $this;
125
    }
126
127
    /**
128
     * Sets the relationship type: one or many.
129
     *
130
     * @param   string  $relType
131
     * @return  self
132
     */
133
    public function setRelType($relType)
134
    {
135
        $relType = strtolower($relType);
136
        $this->validateType($relType);
137
        $this->relType = $relType;
138
        return $this;
139
    }
140
141
    /**
142
     * Validates the relationship type.
143
     *
144
     * @param   string  $relType
145
     * @return  bool
146
     * @throws  MetadataException
147
     */
148 View Code Duplication
    protected function validateType($relType)
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...
149
    {
150
        $valid = ['one', 'many'];
151
        if (!in_array($relType, $valid)) {
152
            throw MetadataException::invalidRelType($relType, $valid);
153
        }
154
        return true;
155
    }
156
}
157