Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Value   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C forView() 0 52 22
1
<?php
2
namespace Inji;
3
/**
4
 * Value
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Value {
12
13
    public $valueKey = '';
14
    public $model = null;
15
    public $type = 'string';
16
17
    public function __construct($model, $key) {
18
        $this->model = $model;
19
        $this->valueKey = $key;
20
    }
21
22
    public function forView($options = []) {
23
        $modelName = get_class($this->model);
24
        $colInfo = $modelName::getColInfo($this->valueKey);
25
        $type = !empty($colInfo['colParams']['type']) ? $colInfo['colParams']['type'] : 'string';
26
        switch ($type) {
27
            case 'dateTime':
28
            //fall
29
            case 'date':
30
                $yy = (int) substr($this->model->{$this->valueKey}, 0, 4);
31
                $mm = (int) substr($this->model->{$this->valueKey}, 5, 2);
32
                $dd = (int) substr($this->model->{$this->valueKey}, 8, 2);
33
34
                $hours = substr($this->model->{$this->valueKey}, 11, 5);
35
36
                $month = array('января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря');
37
                $yearPosrfix = isset($options['yearPostfix']) ? $options['yearPostfix'] : " г.";
38
                return ($dd > 0 ? $dd . " " : '') . $month[$mm - 1] . " " . $yy . $yearPosrfix . (empty($options['notime']) ? " " . $hours : '');
39
            case 'select':
40
                switch ($colInfo['colParams']['source']) {
41
                    case 'model':
42
                        $sourceValue = false;
43
                        if ($this->model->{$this->valueKey}) {
44
                            $sourceValue = $colInfo['colParams']['model']::get($this->model->{$this->valueKey});
45
                        }
46
                        return $sourceValue ? $sourceValue->name() : 'Не задано';
47
                    case 'array':
48
                        return !empty($colInfo['colParams']['sourceArray'][$this->model->{$this->valueKey}]) ? $colInfo['colParams']['sourceArray'][$this->model->{$this->valueKey}] : 'Не задано';
49
                    case 'method':
50
                        if (!empty($colInfo['colParams']['params'])) {
51
                            $values = call_user_func_array([App::$cur->{$colInfo['colParams']['module']}, $colInfo['colParams']['method']], $colInfo['colParams']['params']);
52
                        } else {
53
                            $values = $colInfo['colParams']['module']->{$colInfo['colParams']['method']}();
54
                        }
55
                        return !empty($values[$this->model->{$this->valueKey}]) ? $values[$this->model->{$this->valueKey}] : 'Не задано';
56
                    case 'relation':
57
                        $relations = $colInfo['modelName']::relations();
58
                        $relValue = $relations[$colInfo['colParams']['relation']]['model']::get($this->model->{$this->valueKey});
59
                        return $relValue ? $relValue->name() : 'Не задано';
60
                }
61
                break;
62
            case 'image':
63
                $file = Files\File::get($this->model->{$this->valueKey});
64
                if ($file) {
65
                    return '<img src="' . $file->path . '?resize=60x120" />';
66
                }
67
                return '<img src="/static/system/images/no-image.png?resize=60x120" />';
68
            case 'bool':
69
                return $this->model->{$this->valueKey} ? 'Да' : 'Нет';
70
            default:
71
                return $this->model->{$this->valueKey};
72
        }
73
    }
74
}