Completed
Push — master ( 0c5554...b59e75 )
by Igor
07:15
created

Util   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 68
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A clearText() 0 5 1
A makePageTitle() 0 5 2
A collectModelErrors() 0 12 3
A makeUploadedFile() 0 14 1
1
<?php
2
3
namespace app\helpers;
4
5
use yii\helpers\Html;
6
use yii\helpers\FileHelper;
7
use yii\web\UploadedFile;
8
9
class Util
10
{
11
    /**
12
     * Clear text
13
     * Can use for meta tags
14
     *
15
     * @param string $text
16
     * @return string
17
     */
18 202
    public static function clearText($text)
19
    {
20 202
        $text = str_replace('"', '“', $text);
21 202
        return Html::encode(html_entity_decode(strip_tags($text)));
22
    }
23
24
    /**
25
     * Make page title
26
     *
27
     * @param string $title
28
     * @param string $appendToEnd
29
     * @return string
30
     */
31 213
    public static function makePageTitle($title = '', $appendToEnd = '')
32
    {
33 213
        $title = $title ? self::clearText($title) . ' / ' : '';
34 213
        return $title . $appendToEnd;
35
    }
36
37
   /**
38
    * Collect model errors
39
    *
40
    * @param Model $model the model to be validated
41
    * @return array the error message array indexed by the attribute IDs.
42
    */
43 11
    public static function collectModelErrors($model)
44
    {
45 11
        $result = [];
46
        /* @var $model Model */
47 11
        $models = [$model];
48 11
        foreach ($models as $model) {
49 11
            foreach ($model->getErrors() as $attribute => $errors) {
50 11
                $result[Html::getInputId($model, $attribute)] = $errors;
51
            }
52
        }
53 11
        return $result;
54
    }
55
56
    /**
57
     * Create manually UploadedFile instance by file path
58
     *
59
     * @param string $file file path
60
     * @return UploadedFile
61
     */
62 6
    public static function makeUploadedFile($file)
63
    {
64 6
        $tmpFile = tempnam(sys_get_temp_dir(), 'app');
65 6
        file_put_contents($tmpFile, file_get_contents($file));
66
67 6
        $uploadedFile = new UploadedFile();
68 6
        $uploadedFile->name = pathinfo($file, PATHINFO_BASENAME);
69 6
        $uploadedFile->tempName = $tmpFile;
70 6
        $uploadedFile->type = FileHelper::getMimeType($tmpFile);
71 6
        $uploadedFile->size = filesize($tmpFile);
72 6
        $uploadedFile->error = 0;
73
74 6
        return $uploadedFile;
75
    }
76
}
77