Completed
Push — develop ( c73771...8fefbb )
by Stuart
04:15 queued 02:02
created

StringGuarantee::getOrElse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6667
cc 2
eloc 4
nc 2
nop 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\IsStringy;
49
use Traversable;
50
51
class StringGuarantee implements Guarantee
52
{
53
    /**
54
     * the string data that we represent
55
     * @var string
56
     */
57
    private $data = '';
58
59
    /**
60
     * does $data hold a real value?
61
     * @var boolean
62
     */
63
    private $isEmpty = true;
64
65
    /**
66
     * create a read-only, string guarantee from another piece of data
67
     *
68
     * @param mixed $data
69
     *        the data to wrap
70
     */
71
    public function __construct($data)
72
    {
73
        // we only assign our data if we have been given something
74
        // that is string-like
75
        if (IsStringy::check($data)) {
76
            $this->data = (string)$data;
77
            $this->isEmpty = false;
78
        }
79
    }
80
81
    /**
82
     * return the value of this guarantee (if it has one), or $default if
83
     * this guarantee is empty
84
     *
85
     * @param  string $default
86
     *         the value to return if this guarantee is empty
87
     * @return string
88
     */
89
    public function getOrElse($default)
90
    {
91
        // do we have data?
92
        if ($this->isEmpty()) {
93
            return $default;
94
        }
95
96
        return $this->data;
97
    }
98
99
    /**
100
     * does this guarantee contain a value?
101
     *
102
     * @return boolean
103
     *         TRUE if this guarantee does NOT contain a value
104
     *         FALSE otherwise
105
     */
106
    public function isEmpty()
107
    {
108
        return $this->isEmpty;
109
    }
110
111
    /**
112
     * get an iterator to traverse the wrapped data
113
     *
114
     * @return Iterator
115
     */
116
    public function getIterator()
117
    {
118
        // return an array with just one element
119
        return new ArrayIterator([(string)$this]);
120
    }
121
122
    /**
123
     * convert our wrapped data into a JSON string
124
     *
125
     * @return string
126
     */
127
    public function jsonSerialize()
128
    {
129
        // general case
130
        return (string)$this;
131
    }
132
133
    /**
134
     * convert our data into a string
135
     *
136
     * @return string
137
     */
138
    public function __toString()
139
    {
140
        return $this->data;
141
    }
142
}
143