Completed
Push — master ( e1c233...726cec )
by Igor
07:28
created

Util::collectModelErrors()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 11
nc 6
nop 2
1
<?php
2
3
namespace app\helpers;
4
5
use Yii;
6
use yii\helpers\Html;
7
8
class Util
9
{
10
    /**
11
     * Convert TZ
12
     *
13
     * @param string $date
14
     * @param string $fromTimeZone
15
     * @param string $toTimeZone
16
     * @param string $format
17
     * @return string
18
     */
19
    public static function convertTz($date, $fromTimeZone, $toTimeZone, $format = 'Y-m-d H:i:s')
20
    {
21
        $date = new \DateTime($date, new \DateTimeZone($fromTimeZone));
22
        $date->setTimeZone(new \DateTimeZone($toTimeZone));
23
24
        return $date->format($format);
25
    }
26
27
    /**
28
     * Clear text
29
     * Can use for meta tags
30
     *
31
     * @param string $text
32
     * @return string
33
     */
34
    public static function clearText($text)
35
    {
36
        $text = str_replace('"', '“', $text);
37
        return Html::encode(html_entity_decode(strip_tags($text)));
38
    }
39
40
    /**
41
     * Make page title
42
     *
43
     * @param string $title
44
     * @param string $appendToEnd
45
     * @return string
46
     */
47
    public static function makePageTitle($title = '', $appendToEnd = '')
48
    {
49
        $title = $title ? self::clearText($title) . ' / ' : '';
50
        return $title . $appendToEnd;
51
    }
52
53
   /**
54
    * Collect model errors
55
    *
56
    * @param Model $model the model to be validated
57
    * @param mixed $attributes list of attributes that should be validated.
58
    * @return array the error message array indexed by the attribute IDs.
59
    */
60
    public static function collectModelErrors($model, $attributes = null)
61
    {
62
        $result = [];
63
        if ($attributes instanceof Model) {
64
            // validating multiple models
65
            $models = func_get_args();
66
            $attributes = null;
67
        } else {
68
            $models = [$model];
69
        }
70
        /* @var $model Model */
71
        foreach ($models as $model) {
72
            foreach ($model->getErrors() as $attribute => $errors) {
73
                $result[\yii\helpers\Html::getInputId($model, $attribute)] = $errors;
74
            }
75
        }
76
        return $result;
77
    }
78
}
79