Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

Err::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 3
dl 0
loc 12
ccs 0
cts 11
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\base;
13
14
class Err
15
{
16
    public static function is($data, $eq = null)
17
    {
18
        return is_null($eq) ? (is_array($data) ? array_key_exists('_error', $data) : !$data) : (isset($data['_error']) && $data['_error'] === $eq);
19
    }
20
21
    public static function not($data, $eq = null)
22
    {
23
        return !static::is($data, $eq);
24
    }
25
26
    public static function get($data, $df = null)
27
    {
28
        return isset($data['_error']) ? $data['_error'] : $df;
29
    }
30
31
    public static function set($data, $code, $ops = null)
32
    {
33
        if (!is_array($data)) {
34
            $data = ['_error' => $code];
35
        } else {
36
            $data['_error'] = $code;
37
        }
38
        if ($ops) {
39
            $data['_error_ops'] = $ops;
40
        }
41
        return $data;
42
    }
43
44
    public static function setifnot($data, $default = 'unknown error')
45
    {
46
        if (static::not($data)) {
47
            return $data;
48
        }
49
        if (!static::get($data)) {
50
            static::set($data, $default);
51
        }
52
        return $data;
53
    }
54
55
    public static function reduce($res)
56
    {
57
        if (!count($res)) {
58
            return static::set($res, 'nothing was done');
59
        }
60
        $errors = [];
61
        foreach ($res as $k => $v) {
62
            if (static::is($v)) {
63
                $errors[$k] = static::get($v);
64
            }
65
        }
66
        $row_num = count($res);
67
        $err_num = count($errors);
68
        if ($err_num) {
69
            $res['_error'] = $row_num < 2 ? reset($errors) : (($row_num > $err_num) ? 'partially' : 'completely') . " failed ($err_num/$row_num): " . implode(', ', array_unique($errors));
70
        }
71
        /* if ($set_nums) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
            $res['_error_num'] = $err_num;
73
            $res['_total_num'] = $row_num;
74
        }; */
75
        return $res;
76
    }
77
78
    public static function clean($data)
79
    {
80
        if (is_array($data)) {
81
            foreach ($data as $k => $v) {
82
                if (substr($k, 0, 6) === '_error') {
83
                    unset($data[$k]);
84
                }
85
            }
86
        }
87
        return $data;
88
    }
89
90
    public static function isError($data, $eq = null)
91
    {
92
        return is_null($eq) ? (is_array($data) ? array_key_exists('_error', $data) : !$data) : (isset($data['_error']) && $data['_error'] === $eq);
93
    }
94
95
    public static function getError($data, $df = null)
96
    {
97
        return isset($data['_error']) ? $data['_error'] : $df;
98
    }
99
}
100