Completed
Push — master ( a37ea8...c06150 )
by Dmitry
14:25
created

Ref::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HiPanel core package.
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\models;
12
13
use Yii;
14
use yii\helpers\ArrayHelper;
15
16
class Ref extends \hiqdev\hiart\ActiveRecord
17
{
18
    use \hipanel\base\ModelTrait;
19
20
    public function rules()
21
    {
22
        return [
23
            [['id', 'no'], 'integer'],
24
            [['name', 'oname', 'label'], 'safe'],
25
        ];
26
    }
27
28
    /**
29
     * Finds models by specified $name and $options parameters,
30
     * transforms them to key-value structure.
31
     * Name: attribute `name`
32
     * Value: attribute `label`.
33
     * You can change name of returning attributes `name` and `label`
34
     * You should define `mapOptions` in `options` array
35
     * and define keys `from` for Name and `to` for Value.
36
     *
37
     * @param string $name
38
     * @param string|false $translate
39
     * @param array $options
40
     * @return array
41
     */
42
    public static function getList($name, $translate = null, $options = [])
43
    {
44
        $mapOptions = ArrayHelper::remove($options, 'mapOptions');
45
        $models = static::findCached($name, $translate, $options);
46
47
        $from = ArrayHelper::remove($mapOptions, 'from', 'name');
48
        $to = ArrayHelper::remove($mapOptions, 'to', 'label');
49
        $group = ArrayHelper::remove($mapOptions, 'group', null);
50
51
        return ArrayHelper::map($models, $from, $to, $group);
52
    }
53
54
    public static function getListRecursively($name, $translate = null, $options = [])
55
    {
56
        return self::getList($name, $translate, array_merge($options, ['with_recursive' => true]));
57
    }
58
59
    public static function findCached($name, $translate = null, $options = [])
60
    {
61
        if ($translate === null) {
62
            $translate = 'hipanel';
63
        }
64
65
        $data = Yii::$app->get('cache')->getOrSet([__METHOD__, $name, $options], function () use ($name, $options) {
66
            $conditions = array_merge(['gtype' => $name], $options);
67
            $result = self::find()->where($conditions)->all();
68
69
            return $result;
70
        }, 3600);
71
72
        return array_map(function ($model) use ($translate) {
73
            /** @var self $model */
74
            if ($translate !== false) {
75
                $model->label = Yii::t($translate, $model->label);
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<hipanel\models\Ref>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property label does not exist on object<hipanel\models\Ref>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
76
            }
77
78
            return $model;
79
        }, $data);
80
    }
81
}
82