Passed
Push — master ( 27c83c...3b806d )
by Loban
03:03
created

DataModel   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 36
c 0
b 0
f 0
dl 0
loc 110
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewValue() 0 4 1
A getData() 0 3 1
C formattedValue() 0 32 12
A getValue() 0 3 1
A setData() 0 4 1
A setFormat() 0 4 1
A init() 0 3 1
A getOldValue() 0 4 1
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-activity-logger
4
 * @copyright Copyright (c) 2017 LAV45
5
 * @author Aleksey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\activityLogger\module\models;
10
11
use yii\base\BaseObject;
12
use yii\di\Instance;
13
use yii\helpers\Html;
14
use yii\helpers\ArrayHelper;
15
use yii\i18n\Formatter;
16
17
class DataModel extends BaseObject
18
{
19
    /**
20
     * @var array
21
     */
22
    private $data;
23
    /**
24
     * @var string|\Closure
25
     */
26
    private $format;
27
    /**
28
     * @var string|array|Formatter
29
     */
30
    public $formatter = 'formatter';
31
32
    public function init(): void
33
    {
34
        $this->formatter = Instance::ensure($this->formatter, Formatter::class);
35
    }
36
37
    /**
38
     * @param array $value
39
     * @return $this
40
     */
41
    public function setData(array $value)
42
    {
43
        $this->data = $value;
44
        return $this;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getData()
51
    {
52
        return $this->data;
53
    }
54
55
    /**
56
     * @param string|\Closure $value
57
     * @return $this
58
     */
59
    public function setFormat($value)
60
    {
61
        $this->format = $value;
62
        return $this;
63
    }
64
65
    /**
66
     * @return null|string
67
     */
68
    public function getOldValue()
69
    {
70
        $values = $this->getValue('old');
71
        return $this->formattedValue($values);
72
    }
73
74
    /**
75
     * @return null|string
76
     */
77
    public function getNewValue()
78
    {
79
        $values = $this->getValue('new');
80
        return $this->formattedValue($values);
81
    }
82
83
    /**
84
     * @param mixed $value
85
     * @return string
86
     */
87
    protected function formattedValue($value)
88
    {
89
        if ($this->format && is_string($this->format)) {
90
            return $this->formatter->format($value, $this->format);
91
        }
92
        if ($this->format && is_callable($this->format)) {
93
            $value = call_user_func($this->format, $value);
94
            return $value ?? $this->formatter->nullDisplay;
95
        }
96
        if (null === $value) {
97
            return $this->formatter->nullDisplay;
98
        }
99
        if (is_numeric($value)) {
100
            return $value;
101
        }
102
        if (filter_var($value, FILTER_VALIDATE_URL)) {
103
            return Html::a(Html::encode($value), $value, ['target' => '_blank']);
104
        }
105
        if (is_string($value)) {
106
            if (empty($value)) {
107
                return $this->formatter->nullDisplay;
108
            }
109
            return $this->formatter->asNtext($value);
110
        }
111
        if (is_bool($value)) {
112
            return $this->formatter->asBoolean($value);
113
        }
114
        if (is_array($value)) {
115
            $value = json_encode($value, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
116
            return Html::tag('pre', $value);
117
        }
118
        return $value;
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    protected function getValue(string $tag)
125
    {
126
        return ArrayHelper::getValue($this->data, [$tag, 'value']);
127
    }
128
}
129