Completed
Branch develop (17e8cf)
by Nate
10:01
created

Association::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 38
cp 0
rs 9.312
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://www.flipboxfactory.com/software/element-lists/license
6
 * @link       https://www.flipboxfactory.com/software/element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\records;
10
11
use Craft;
12
use flipbox\craft\ember\records\ActiveRecord;
13
use flipbox\craft\ember\records\FieldAttributeTrait;
14
use flipbox\craft\ember\records\SiteAttributeTrait;
15
use flipbox\craft\ember\records\SortableTrait;
16
use flipbox\craft\element\lists\queries\AssociationQuery;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 *
22
 * @property int $targetId
23
 * @property int $sourceId
24
 */
25
class Association extends ActiveRecord
26
{
27
    use SiteAttributeTrait,
28
        FieldAttributeTrait,
29
        SortableTrait;
30
31
    /**
32
     * @inheritdoc
33
     */
34
    const TABLE_ALIAS = 'elementlist';
35
36
    /**
37
     * @inheritdoc
38
     *
39
     * @deprecated
40
     */
41
    const TARGET_ATTRIBUTE = 'targetId';
42
43
    /**
44
     * @inheritdoc
45
     *
46
     * @deprecated
47
     */
48
    const SOURCE_ATTRIBUTE = 'sourceId';
49
50
    /**
51
     * @inheritdoc
52
     */
53
    protected $getterPriorityAttributes = ['fieldId', 'siteId'];
54
55
    /**
56
     * @noinspection PhpDocMissingThrowsInspection
57
     * @return AssociationQuery
58
     */
59
    public static function find(): AssociationQuery
60
    {
61
        /** @noinspection PhpIncompatibleReturnTypeInspection */
62
        /** @noinspection PhpUnhandledExceptionInspection */
63
        return Craft::createObject(AssociationQuery::class, [get_called_class()]);
64
    }
65
66
    /*******************************************
67
     * RULES
68
     *******************************************/
69
70
    /**
71
     * @return array
72
     */
73
    public function rules(): array
74
    {
75
        return array_merge(
76
            parent::rules(),
77
            $this->siteRules(),
78
            $this->fieldRules(),
79
            [
80
                [
81
                    [
82
                        'targetId',
83
                        'sourceId',
84
                        'fieldId',
85
                    ],
86
                    'required'
87
                ],
88
                [
89
                    'targetId',
90
                    'unique',
91
                    'targetAttribute' => [
92
                        'targetId',
93
                        'sourceId',
94
                        'fieldId',
95
                        'siteId'
96
                    ]
97
                ],
98
                [
99
                    [
100
                        'targetId',
101
                        'sourceId',
102
                    ],
103
                    'safe',
104
                    'on' => [
105
                        static::SCENARIO_DEFAULT
106
                    ]
107
                ]
108
            ]
109
        );
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function beforeSave($insert)
116
    {
117
        $this->ensureSortOrder(
118
            [
119
                'targetId' => $this->targetId,
120
                'fieldId' => $this->fieldId,
121
                'siteId' => $this->siteId
122
            ]
123
        );
124
125
        return parent::beforeSave($insert);
126
    }
127
128
    /**
129
     * @inheritdoc
130
     * @throws \yii\db\Exception
131
     */
132
    public function afterSave($insert, $changedAttributes)
133
    {
134
        $this->autoReOrder(
135
            'sourceId',
136
            [
137
                'targetId' => $this->targetId,
138
                'fieldId' => $this->fieldId,
139
                'siteId' => $this->siteId
140
            ]
141
        );
142
143
        parent::afterSave($insert, $changedAttributes);
144
    }
145
146
    /**
147
     * @inheritdoc
148
     * @throws \yii\db\Exception
149
     */
150
    public function afterDelete()
151
    {
152
        $this->sequentialOrder(
153
            'sourceId',
154
            [
155
                'targetId' => $this->targetId,
156
                'fieldId' => $this->fieldId,
157
                'siteId' => $this->siteId
158
            ]
159
        );
160
161
        parent::afterDelete();
162
    }
163
}
164