TraversableGuarantee   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 9
c 2
b 0
f 2
lcom 1
cbo 1
dl 0
loc 91
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getOrElse() 0 9 2
A isEmpty() 0 4 1
A getIterator() 0 15 3
A jsonSerialize() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) 2016-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Guarantees/Values
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://code.ganbarodigital.com/php-data-containers
42
 */
43
44
namespace GanbaroDigital\Guarantees\Values;
45
46
use ArrayIterator;
47
use Iterator;
48
use GanbaroDigital\Reflection\Checks\IsTraversable;
49
use JsonSerializable;
50
use stdClass;
51
use Traversable;
52
53
class TraversableGuarantee implements Guarantee
54
{
55
    /**
56
     * the traversable data that we represent
57
     * @var array|Traversable|stdClass
58
     */
59
    private $data = [];
60
61
    /**
62
     * does $data hold real date?
63
     * @var boolean
64
     */
65
    private $isEmpty = true;
66
67
    /**
68
     * create a read-only, traversable guarantee from another piece of data
69
     *
70
     * @param mixed $data
71
     *        the data to wrap
72
     */
73
    public function __construct($data)
74
    {
75
        // we only assign our data if we have been given something
76
        // that is Traversable
77
        if (IsTraversable::check($data)) {
78
            $this->data = $data;
79
            $this->isEmpty = false;
80
        }
81
    }
82
83
    /**
84
     * return the value of this guarantee (if it has one), or $default if
85
     * this guarantee is empty
86
     *
87
     * @param  array|Traversable|stdClass $default
88
     *         the value to return if this guarantee is empty
89
     * @return array|Traversable|stdClass
90
     */
91
    public function getOrElse($default)
92
    {
93
        // do we have data?
94
        if ($this->isEmpty()) {
95
            return $default;
96
        }
97
98
        return $this->data;
99
    }
100
101
    /**
102
     * does this guarantee contain a value?
103
     *
104
     * @return boolean
105
     *         TRUE if this guarantee does NOT contain a value
106
     *         FALSE otherwise
107
     */
108
    public function isEmpty()
109
    {
110
        return $this->isEmpty;
111
    }
112
113
    /**
114
     * get an iterator to traverse the wrapped data
115
     *
116
     * @return Iterator
117
     */
118
    public function getIterator()
119
    {
120
        // general case - we've wrapped a real array
121
        if (is_array($this->data)) {
122
            return new ArrayIterator($this->data);
123
        }
124
125
        // special case - we've wrapped a Traversable object
126
        if (method_exists($this->data, 'getIterator')) {
127
            return $this->data->getIterator();
128
        }
129
130
        // special case - we're wrapped a stdClass
131
        return new ArrayIterator($this->data);
132
    }
133
134
    /**
135
     * convert our wrapped data into a JSON string
136
     *
137
     * @return array|Traversable|stdClass
138
     */
139
    public function jsonSerialize()
140
    {
141
        return $this->data;
142
    }
143
}
144