Completed
Push — master ( 106d8f...62bec2 )
by Hong
02:35
created

ErrorAwareTrait::copyError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Error;
16
17
/**
18
 * ErrorAwareTrait
19
 *
20
 * Implementation of ErrorAwareInterface
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     ErrorAwareInterface
25
 * @version 2.0.22
26
 * @since   2.0.22 added
27
 */
28
trait ErrorAwareTrait
29
{
30
    /**
31
     * error message
32
     *
33
     * @var    string
34
     * @access protected
35
     */
36
    protected $error = '';
37
38
    /**
39
     * error code
40
     *
41
     * @var    string
42
     * @access protected
43
     */
44
    protected $error_code = '';
45
46
    /**
47
     * 0 or '0000' etc means no error, = ''
48
     *
49
     * @var    bool
50
     * @access protected
51
     */
52
    protected $zero_empty = true;
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function hasError()/*# : bool */
58
    {
59
        return '' === $this->error_code;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function getError()/*# : string */
66
    {
67
        return $this->error;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function getErrorCode()/*# : int */
74
    {
75
        return $this->error_code;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function setError(
82
        /*# string */ $message = '',
83
        /*# string */ $code = ''
84
    )/*# : bool */ {
85
        $this->error = (string) $message;
86
87
        // zero ?
88
        $zcode = (string) $code;
89
        if ($this->zero_empty && '' === str_replace('0', '', $zcode)) {
90
            $this->error_code = '';
91
        } else {
92
            $this->error_code = $zcode;
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    public function copyError(ErrorAwareInterface $obj)
102
    {
103
        if ($obj->hasError()) {
104
            $this->setError($obj->getError(), $obj->getErrorCode());
105
        }
106
    }
107
}
108