ActionErrors::getBasicActionWarnings()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the fangface/yii2-concord package
4
 *
5
 * For the full copyright and license information, please view
6
 * the file LICENSE.md that was distributed with this source code.
7
 *
8
 * @package fangface/yii2-concord
9
 * @author Fangface <[email protected]>
10
 * @copyright Copyright (c) 2014 Fangface <[email protected]>
11
 * @license https://github.com/fangface/yii2-concord/blob/master/LICENSE.md MIT License
12
 *
13
 */
14
15
namespace fangface\base\traits;
16
17
use fangface\Tools;
18
19
trait ActionErrors {
20
21
    protected $actionErrors             = array();
22
    protected $actionWarnings           = array();
23
24
    /**
25
     * Returns the number of action errors (those errors logged during predetermined actions such
26
     * as saveAll() and deleteFull()
27
     *
28
     * @return integer number of errors
29
     */
30
    public function hasActionErrors()
31
    {
32
        return count($this->actionErrors);
33
    }
34
35
36
    /**
37
     * Returns the action errors  (those errors logged during predetermined actions such
38
     * as saveAll() and deleteFull()
39
     *
40
     * @return array array of action errors message and code
41
     */
42
    public function getActionErrors()
43
    {
44
        return $this->actionErrors;
45
    }
46
47
48
    /**
49
     * Returns the action errors  (those errors logged during predetermined actions such
50
     * as saveAll() and deleteFull() as a simplifed message array
51
     *
52
     * @param boolean $noAttribute Exclude attribute name from error text
53
     * @return array Action errors message and code
54
     */
55
    public function getBasicActionErrors($noAttribute = false)
56
    {
57
        $array = [];
58
        if ($this->actionErrors) {
59
            foreach ($this->actionErrors as $k => $error) {
60
                foreach ($error['message'] as $k2 => $message) {
61
                    $array[] = ($noAttribute ? '' : ($error['attribute'] ? $error['attribute'] . ' - ' : '')) . $message . ($error['code'] ? ' (' . $error['code'] . ')' : '');
62
                }
63
            }
64
        }
65
        return $array;
66
    }
67
68
69
    /**
70
     * Returns the first action error
71
     *
72
     * @return array
73
     * @see getActionErrors()
74
     * @see addActionError()
75
     */
76
    public function getFirstActionError()
77
    {
78
        return ($this->actionErrors ? reset($this->actionErrors) : null);
79
    }
80
81
    /**
82
     * Adds a new action error
83
     * @param string $message new error message
84
     * @param integer $code new error code
85
     * @param string $attribute attribute to which the error applies
86
     * @param string $modelName model to which the error applies
87
     */
88
    public function addActionError($message, $code = 0, $attribute = '', $modelName = null)
89
    {
90
        $message = is_array($message) ? $message : array($message);
91
        $this->actionErrors[] = array(
92
            'message' => $message,
93
            'code' => $code,
94
            'attribute' => $attribute,
95
            'model' => ($modelName !== null ? $modelName : (true ? Tools::getClassName($this) : get_called_class()))
96
        );
97
    }
98
99
100
    /**
101
     * Merge an existing array of action errors into the current models
102
     * action errors
103
     * @param array $errors array of action errors
104
     */
105
    public function mergeActionErrors($errors)
106
    {
107
        if (is_array($errors) && $errors) {
108
            if (!$this->actionErrors) {
109
                $this->actionErrors = $errors;
110
            } else {
111
                $this->actionErrors = array_merge($this->actionErrors, $errors);
112
            }
113
        }
114
    }
115
116
117
    /**
118
     * Reset action errors, typically called ahead of predefined actions such as saveAll() and deleteFull()
119
     */
120
    public function clearActionErrors()
121
    {
122
        $this->actionErrors = [];
123
        $this->actionWarnings = [];
124
    }
125
126
    /**
127
     * Returns the number of action warnings (those warnings logged during predetermined actions such
128
     * as saveAll() and deleteFull()
129
     *
130
     * @return integer number of warnings
131
    */
132
    public function hasActionWarnings()
133
    {
134
        return count($this->actionWarnings);
135
    }
136
137
138
    /**
139
     * Returns the action warnings  (those warnings logged during predetermined actions such
140
     * as saveAll() and deleteFull()
141
     *
142
     * @return array array of action warnings message and code
143
     */
144
    public function getActionWarnings()
145
    {
146
        return $this->actionWarnings;
147
    }
148
149
150
    /**
151
     * Returns the action warnings  (those warnings logged during predetermined actions such
152
     * as saveAll() and deleteFull() as a simplifed message array
153
     *
154
     * @param boolean $noAttribute Exclude attribute name from error text
155
     * @return array array of action warnings message and code
156
     */
157
    public function getBasicActionWarnings($noAttribute = false)
158
    {
159
        $array = [];
160
        if ($this->actionWarnings) {
161
            foreach ($this->actionWarnings as $k => $error) {
162
                foreach ($error['message'] as $k2 => $message) {
163
                    $array[] = ($noAttribute ? '' : ($error['attribute'] ? $error['attribute'] . ' - ' : '')) . $message . ($error['code'] ? ' (' . $error['code'] . ')' : '');
164
                }
165
            }
166
        }
167
        return $array;
168
    }
169
170
171
    /**
172
     * Returns the first action warning
173
     *
174
     * @return array
175
     * @see getActionWarnings()
176
     * @see addActionWarning()
177
     */
178
    public function getFirstActionWarning()
179
    {
180
        return ($this->actionWarnings ? reset($this->actionWarnings) : null);
181
    }
182
183
    /**
184
     * Adds a new action warning
185
     * @param string $message new warning message
186
     * @param integer $code new warning code
187
     * @param string $attribute attribute to which the error applies
188
     * @param string $modelName model to which the error applies
189
     */
190
    public function addActionWarning($message, $code = 0, $attribute = '', $modelName = null)
191
    {
192
        $message = is_array($message) ? $message : array($message);
193
        $this->actionWarnings[] = array(
194
            'message' => $message,
195
            'code' => $code,
196
            'attribute' => $attribute,
197
            'model' => ($modelName !== null ? $modelName : (true ? Tools::getClassName($this) : get_called_class()))
198
        );
199
    }
200
201
202
    /**
203
     * Merge an existing array of action warnings into the current models
204
     * action warnings
205
     * @param array $warnings array of action warnings
206
     */
207
    public function mergeActionWarnings($warnings)
208
    {
209
        if (is_array($warnings) && $warnings) {
210
            if (!$this->actionWarnings) {
211
                $this->actionWarnings = $warnings;
212
            } else {
213
                $this->actionWarnings = array_merge($this->actionWarnings, $warnings);
214
            }
215
        }
216
    }
217
218
}
219