Completed
Push — master ( c0984a...820d33 )
by Andrey
02:04
created

Owner::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Itstructure\MFUploader\models;
4
5
use yii\db\ActiveQuery;
6
use yii\base\InvalidArgumentException;
7
8
/**
9
 * This is the base class for owners.
10
 *
11
 * @property int $ownerId Owner id.
12
 * @property string $owner Owner name (post, article, page e.t.c.).
13
 * @property string $ownerAttribute Owner attribute (thumbnail, image e.t.c.).
14
 *
15
 * @package Itstructure\MFUploader\models
16
 *
17
 * @author Andrey Girnik <[email protected]>
18
 */
19
class Owner extends \yii\db\ActiveRecord
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function rules()
25
    {
26
        return [
27
            [
28
                [
29
                    'ownerId',
30
                    'owner',
31
                    'ownerAttribute',
32
                ],
33
                'required',
34
            ],
35
            [
36
                'ownerId',
37
                'integer',
38
            ],
39
            [
40
                [
41
                    'owner',
42
                    'ownerAttribute',
43
                ],
44
                'string',
45
                'max' => 255,
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function attributeLabels()
54
    {
55
        return [
56
            'ownerId' => 'Owner ID',
57
            'owner' => 'Owner',
58
            'ownerAttribute' => 'Owner Attribute',
59
        ];
60
    }
61
62
    /**
63
     * Get Id's by owner.
64
     *
65
     * @param string $nameId
66
     * @param array $args It can be an array of the next params: owner{string}, ownerId{int}, ownerAttribute{string}.
67
     *
68
     * @throws InvalidArgumentException
69
     *
70
     * @return ActiveQuery
71
     */
72
    protected static function getEntityIdsQuery(string $nameId, array $args): ActiveQuery
73
    {
74
        $conditions = [];
75
76
        if (isset($args['owner'])) {
77
            if (!is_string($args['owner']) || empty($args['owner'])) {
78
                throw new InvalidArgumentException('Parameter owner must be a string.');
79
            }
80
            $conditions['owner'] = $args['owner'];
81
82
            if (isset($args['ownerId'])) {
83
                if (!is_numeric($args['ownerId'])) {
84
                    throw new InvalidArgumentException('Parameter ownerId must be numeric.');
85
                }
86
                $conditions['ownerId'] = $args['ownerId'];
87
            }
88
        }
89
90
        if (isset($args['ownerAttribute'])) {
91
            if (!is_string($args['ownerAttribute']) || empty($args['ownerAttribute'])) {
92
                throw new InvalidArgumentException('Parameter ownerAttribute must be a string.');
93
            }
94
            $conditions['ownerAttribute'] = $args['ownerAttribute'];
95
        }
96
97
        $query = static::find()
98
            ->select($nameId);
99
100
        if (count($conditions) > 0) {
101
            return $query->where($conditions);
102
        }
103
104
        return $query;
105
    }
106
}
107