Category   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 41
c 1
b 0
f 0
dl 0
loc 95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 5 1
A attributeLabels() 0 5 1
A getCategory() 0 5 2
A getCategories() 0 2 1
1
<?php
2
3
namespace common\models;
4
5
use Yii;
6
use yii\helpers\ArrayHelper as AH;
7
8
/**
9
 * @property integer $id
10
 * @property string $name
11
 */
12
class Category extends \yii\base\BaseObject implements \common\interfaces\CategoryInterface {
13
14
  public static $categories = [
15
    [ "id" => 1, "name" => "Restoration"],
16
    [ "id" => 2, "name" => "Forgetting Priorities"],
17
    [ "id" => 3, "name" => "Anxiety"],
18
    [ "id" => 4, "name" => "Speeding Up"],
19
    [ "id" => 5, "name" => "Ticked Off"],
20
    [ "id" => 6, "name" => "Exhausted"],
21
    [ "id" => 7, "name" => "Relapse/Moral Failure"],
22
  ];
23
24
  public static $colors = [
25
    1 => [
26
      "color" => "#008000",
27
      "highlight" => "#199919"
28
    ],
29
    2 => [
30
      "color" => "#4CA100",
31
      "highlight" => "#61B219"
32
    ],
33
    3 => [
34
      "color" => "#98C300",
35
      "highlight" => "#AACC33"
36
    ],
37
    4 => [
38
      "color" => "#E5E500",
39
      "highlight" => "#E5E533"
40
    ],
41
    5 => [
42
      "color" => "#E59900",
43
      "highlight" => "#E5AA33"
44
    ],
45
    6 => [
46
      "color" => "#E54B00",
47
      "highlight" => "#E56D33"
48
    ],
49
    7 => [
50
      "color" => "#CC0000",
51
      "highlight" => "#CC3333"
52
    ]
53
  ];
54
55
  /**
56
   * @inheritdoc
57
   */
58
  public function rules()
59
  {
60
    return [
61
      ['name', 'required'],
62
      ['name', 'string', 'max' => 255],
63
    ];
64
  }
65
66
  /**
67
   * @inheritdoc
68
   */
69
  public function attributeLabels()
70
  {
71
    return [
72
      'id' => 'ID',
73
      'name' => 'Name',
74
    ];
75
  }
76
77
  /**
78
   * getCategories() returns a array of categories indexed by the category id.
79
   * The format is similar to:
80
   *     [
81
   *        1 => 'Restoration',
82
   *        2 => 'Forgetting Priorities',
83
   *        ...
84
   *      ]
85
   * @return array of categories
86
   */
87
  public static function getCategories() {
88
    return AH::map(\common\models\Category::$categories, 'id', 'name');
89
  }
90
91
  /**
92
   * Given a $key => $value pair, returns the matching category.
93
   * Example:
94
   *     getCategory('id', 1);
95
   * Should return:
96
   *     [ "id" => 1, "name" => "Restoration"]
97
   *
98
   * @param string $key the name of the attribute to filter on
99
   * @param string $val the value of the attribute to filter on
100
   * @return a single category
0 ignored issues
show
Bug introduced by
The type common\models\a was not found. Did you mean a? If so, make sure to prefix the type with \.
Loading history...
101
   */
102
  public static function getCategory($key, $val) {
103
    $ret = array_values(array_filter(self::$categories, function($cat) use ($key, $val) {
104
      return $cat[$key] === $val;
105
    }));
106
    return $ret ? $ret[0] : null;
107
  }
108
}
109