Completed
Push — master ( 81b65f...3c6407 )
by vistart
07:48
created

BaseEntityModel::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\models;
14
15
use MongoDB\BSON\Binary;
16
use rhosocial\base\helpers\Number;
17
use rhosocial\base\models\queries\BaseEntityQuery;
18
use rhosocial\base\models\traits\EntityTrait;
19
use yii\db\ActiveRecord;
20
use yii\base\NotSupportedException;
21
22
/**
23
 * The abstract BaseEntityModel is used for entity model class which associates
24
 * with relational database table.
25
 * Note: the $idAttribute and $guidAttribute are not be assigned to false
26
 * simultaneously, and you should set at least one of them as primary key.
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
abstract class BaseEntityModel extends ActiveRecord
31
{
32
    use EntityTrait;
33
34
    /**
35
     * Initialize new entity.
36
     */
37 340
    public function init()
38
    {
39 340
        if ($this->skipInit) {
40 20
            return;
41
        }
42 340
        $this->initEntityEvents();
43 340
        $this->checkAttributes();
44 340
        parent::init();
45 340
    }
46
47
    /**
48
     * Check whether all properties meet the standards. If you want to disable
49
     * checking, please override this method and return true directly. This
50
     * method runs when environment is not production or disable debug mode.
51
     * @return boolean true if all checks pass.
52
     * @throws NotSupportedException
53
     */
54 340
    public function checkAttributes()
55
    {
56 340
        if (YII_ENV !== YII_ENV_PROD || YII_DEBUG) {
57 340
            if (!is_string($this->idAttribute) && empty($this->idAttribute) &&
58 340
                !is_string($this->guidAttribute) && empty($this->guidAttribute)) {
59 1
                $errorInfo = 'ID and GUID attributes are not be disabled simultaneously in relational database.';
60 1
                throw new \yii\base\NotSupportedException($errorInfo);
61
            }
62
        }
63 340
        return true;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     * ------------
69
     * This static method will take $queryClass property to query class. If it is
70
     * not a string, The [[BaseEntityQuery]] will be taken.
71
     * This static method will build non-init model first, so you wouldn't build it any more.
72
     * @return BaseEntityQuery the newly created [[BaseEntityQuery]]
73
     * or its extended class instance.
74
     */
75 297
    public static function find()
76
    {
77 297
        $self = static::buildNoInitModel();
78 297
        if (!is_string($self->queryClass)) {
79 20
            $self->queryClass = BaseEntityQuery::class;
80
        }
81 297
        $queryClass = $self->queryClass;
82 297
        return new $queryClass(get_called_class(), ['noInitModel' => $self]);
83
    }
84
    
85
    /**
86
     * 
87
     * @param array $models
88
     */
89 48
    public static function compositeGUIDs($models) {
90 48
        $guids = [];
91 48
        foreach ($models as $model) {
92 46
            if ($model instanceof static) {
93 2
                $guids[] = $model->getGUID();
94
            } elseif ($model instanceof BaseEntityModel) {
95
                $guids[] = new Binary($model->getGUID(), Binary::TYPE_UUID);
96 46
            } elseif (is_string($model) && preg_match(Number::GUID_REGEX, $model)) {
97
                $guids[] = new Binary(Number::guid_bin($model), Binary::TYPE_UUID);
98 46
            } elseif (is_string($model) && strlen($model) == 16) {
99 46
                $guids[] = new Binary($model, Binary::TYPE_UUID);
100
            }
101
        }
102 46
        return $guids;
103
    }
104
}
105