Completed
Push — master ( 4c707a...1e14b3 )
by Zhmayev
01:23
created

WSException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
* Vtiger Web Services PHP Client Library
4
*
5
* The MIT License (MIT)
6
*
7
* Copyright (c) 2015, Zhmayev Yaroslav <[email protected]>
8
*
9
* Permission is hereby granted, free of charge, to any person obtaining a copy
10
* of this software and associated documentation files (the "Software"), to deal
11
* in the Software without restriction, including without limitation the rights
12
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
* copies of the Software, and to permit persons to whom the Software is
14
* furnished to do so, subject to the following conditions:
15
*
16
* The above copyright notice and this permission notice shall be included in
17
* all copies or substantial portions of the Software.
18
*
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
* THE SOFTWARE.
26
*
27
* @author    Zhmayev Yaroslav <[email protected]>
28
* @copyright 2015-2016 Zhmayev Yaroslav
29
* @license   The MIT License (MIT)
30
*/
31
32
namespace Salaros\Vtiger\VTWSCLib;
33
34
use \Exception;
35
use \IteratorAggregate;
36
37
/**
38
* Vtiger Web Services PHP Client Exception class
39
*
40
* Class WSException
41
* @package Salaros\Vtiger\VTWSCLib
42
*/
43
class WSException extends Exception implements IteratorAggregate
44
{
45
    protected $message;
46
    protected $code;
47
    
48
    // Redefine the exception so message isn't optional
49
    public function __construct($message, $code = 'UNKNOWN', Exception $previous = null)
50
    {
51
        $this->message = $message;
52
        $this->code = $code;
53
        
54
        // make sure everything is assigned properly
55
        parent::__construct($this->message, 0, $previous);
56
    }
57
    
58
    // custom string representation of object
59
    public function __toString()
60
    {
61
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
62
    }
63
    
64
    /**
65
    * Retrieve an external iterator
66
    *
67
    * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
68
    * @return \Traversable
69
    *  An instance of an object implementing \Traversable
70
    */
71
    public function getIterator()
72
    {
73
        $properties = $this->getAllProperties();
74
        $iterator = new \ArrayIterator($properties);
75
        
76
        return $iterator;
77
    }
78
    
79
    /**
80
    * Get all the properties of the object
81
    *
82
    * @return array
83
    */
84
    private function getAllProperties()
85
    {
86
        $allProperties = get_object_vars($this);
87
        
88
        $properties = array();
89
        while (list ($fullName, $value) = each($allProperties)) {
90
            $fullNameComponents = explode("\0", $fullName);
91
            $propertyName = array_pop($fullNameComponents);
92
            if ($propertyName && isset($value)) {
93
                $properties[$propertyName] = $value;
94
            }
95
        }
96
        
97
        return $properties;
98
    }
99
}
100