TargetAttributeTrait::targetRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 22
cp 0
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\records;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
use flipbox\craft\ember\records\ActiveRecordTrait;
15
use yii\base\Model;
16
17
/**
18
 * This trait assists w/ managing a 'targetId' attribute and a 'target' object, keeping them
19
 * in sync if one of the other is altered.  In addition, depending on the operation process, a
20
 * newly created object could be set and saved.  Subsequent calls to retrieve the Id would return
21
 * the newly created object Id.
22
 *
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 3.0.0
25
 */
26
trait TargetAttributeTrait
27
{
28
    use ActiveRecordTrait;
29
30
    /**
31
     * @var Element|ElementInterface|null
32
     */
33
    private $target;
34
35
    /**
36
     * @param string $attribute
37
     * @return Element|ElementInterface|null
38
     */
39
    abstract protected function resolveElementFromIdAttribute(string $attribute);
40
41
    /**
42
     * @param mixed $element
43
     * @return Element|ElementInterface|null
44
     */
45
    abstract protected function resolveElement($element = null);
46
47
    /**
48
     * @return bool
49
     */
50
    public function isTargetSet(): bool
51
    {
52
        return null !== $this->target;
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    protected function targetRules(): array
59
    {
60
        return [
61
            [
62
                [
63
                    'targetId'
64
                ],
65
                'number',
66
                'integerOnly' => true
67
            ],
68
            [
69
                [
70
                    'targetId',
71
                    'target'
72
                ],
73
                'safe',
74
                'on' => [
75
                    Model::SCENARIO_DEFAULT
76
                ]
77
            ]
78
        ];
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function targetAttributeLabels(): array
85
    {
86
        return [
87
            'targetId' => Craft::t('app', 'Target Id')
88
        ];
89
    }
90
91
    /**
92
     * @param int|null $id
93
     * @return $this
94
     */
95
    public function setTargetId(int $id = null)
96
    {
97
        $this->setAttribute('targetId', $id);
98
99
        if (null !== $this->target && $id !== $this->target->getId()) {
100
            $this->target = null;
101
        }
102
        return $this;
103
    }
104
105
    /**
106
     * Get associated targetId
107
     *
108
     * @return int|null
109
     */
110
    public function getTargetId()
111
    {
112
        if (null === $this->getAttribute('targetId') && null !== $this->target) {
113
            $this->setTargetId($this->target->getId());
114
        }
115
116
        return $this->getAttribute('targetId');
117
    }
118
119
    /**
120
     * @param mixed $target
121
     * @return $this
122
     */
123
    public function setTarget($target = null)
124
    {
125
        $this->target = null;
126
        $this->setAttribute('targetId', null);
127
128
        if ($target = $this->resolveElement($target)) {
129
            $this->target = $target;
130
            $this->setAttribute('targetId', $target->getId());
131
        }
132
133
        return $this;
134
    }
135
136
    /**
137
     * @return Element|ElementInterface|null
138
     */
139
    public function getTarget()
140
    {
141
        if ($this->target === null) {
142
            $target = $this->resolveElementFromIdAttribute('targetId');
143
            $this->setTarget($target);
144
            return $target;
145
        }
146
147
        $targetId = $this->getAttribute('targetId');
148
        if ($targetId !== null && $targetId !== $this->target->getId()) {
149
            $this->target = null;
150
            return $this->getTarget();
151
        }
152
153
        return $this->target;
154
    }
155
}
156