Completed
Push — master ( a2c643...e562f7 )
by Nelson
04:00
created

IntString   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 67.68%

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 202
rs 10
c 0
b 0
f 0
ccs 44
cts 65
cp 0.6768
wmc 21

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 3 1
A parse() 0 39 5
A getIntValue() 0 3 1
A equals() 0 11 4
A compareTo() 0 17 5
A __construct() 0 38 4
A getStringValue() 0 3 1
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Class definition:  [NelsonMartell]  IntString
7
 *
8
 * Copyright © 2015-2017 Nelson Martell (http://nelson6e65.github.io)
9
 *
10
 * Licensed under The MIT License (MIT)
11
 * For full copyright and license information, please see the LICENSE
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright 2015-2017 Nelson Martell
15
 * @link      http://nelson6e65.github.io/php_nml/
16
 * @since     0.1.1
17
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
18
 * */
19
20
namespace NelsonMartell;
21
22
use NelsonMartell\Extensions\Text;
23
use \InvalidArgumentException;
24
25
/**
26
 * Representa un elemento mixto, compuesto por un entero y una cadena unidos
27
 * (en ese orden).
28
 * El método IntString::toString obtiene esa cadena compuesta.
29
 *
30
 * @author Nelson Martell <[email protected]>
31
 * @since 0.1.1
32
 * */
33
class IntString extends StrictObject implements IEquatable, IComparable
34
{
35
    /**
36
     * Creates a new IntString instance.
37
     *
38
     * @param int          $intValue    Integer part. Default: ``0`` (zero).
39
     * @param string|null  $stringValue String part. Default: ``''`` (empty).
40
     */
41 49
    public function __construct($intValue = 0, $stringValue = '')
42
    {
43 49
        unset($this->IntValue, $this->StringValue);
44
45 49
        if (!(is_integer($intValue) || $intValue === null)) {
0 ignored issues
show
introduced by
The condition is_integer($intValue) is always true.
Loading history...
46
            $args = [
47
                'position' => '1st',
48
                'expected' => typeof(0).'" or "'.typeof(null),
49
                'actual'   => typeof($intValue),
50
            ];
51
52
            $msg = msg('Invalid argument type.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53
            $msg .= msg(
54
                ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.',
55
                $args
56
            );
57
58
            throw new InvalidArgumentException($msg);
59
        }
60
61 49
        if (!typeof($stringValue)->canBeString()) {
62
            $args = [
63 2
                'position' => '2nd',
64 2
                'expected' => typeof('string').'", "'.typeof(null).'" or "any object convertible to string',
1 ignored issue
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
65 2
                'actual'   => typeof($stringValue),
66
            ];
67
68 2
            $msg = msg('Invalid argument type.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69 2
            $msg .= msg(
70 2
                ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.',
71 2
                $args
72
            );
73
74 2
            throw new InvalidArgumentException($msg);
75
        }
76
77 47
        $this->intValue = (integer) $intValue;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
78 47
        $this->stringValue = (string) $stringValue;
79 47
    }
80
81
    /**
82
     * Convert the object to an instance of ``IntString``.
83
     *
84
     * @param string|IntString $obj Object to convert to ``IntString``.
85
     *
86
     * @return IntString
87
     * @throws InvalidArgumentException if object is not a string or format is invalid.
88
     */
89 36
    public static function parse($obj)
90
    {
91 36
        if ($obj instanceof IntString) {
92
            return $obj;
93
        }
94
95 36
        if (is_integer($obj)) {
0 ignored issues
show
introduced by
The condition is_integer($obj) is always false.
Loading history...
96 19
            return new VersionComponent($obj);
97
        }
98
99
        try {
100 18
            $intValue = (integer) Text::ensureIsString($obj);
101 1
        } catch (InvalidArgumentException $e) {
102
            $args = [
103 1
                'position' => '1st',
104 1
                'expected' => 'string" or "integer',
105 1
                'actual'   => typeof($obj),
106
            ];
107
108 1
            $msg = msg('Invalid argument type.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
109 1
            $msg .= msg(
110 1
                ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.',
111 1
                $args
112
            );
113
114 1
            throw new InvalidArgumentException($msg, 1, $e);
115
        }
116
117 17
        $stringValue = ltrim($obj, "$intValue");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $intValue instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
118
119
        // Validate that 0 (zero) is not interpreted as '' (empty string)
120 17
        if ($stringValue === $obj) {
121 3
            $msg = msg('Invalid argument value.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
122 3
            $msg .= msg(' "{0}" (string) must to start with an integer.', $obj);
123
124 3
            throw new InvalidArgumentException($msg);
125
        }
126
127 14
        return new IntString($intValue, $stringValue);
128
    }
129
130
    /**
131
     * Integer part of the instance.
132
     *
133
     * @var int
134
     */
135
    protected $intValue;
136
137
    /**
138
     * String part of the instance.
139
     *
140
     * @var string
141
     */
142
    protected $stringValue;
143
144
    /**
145
     * Gets the integer part of the instance.
146
     *
147
     * @var int
148
     */
149
    public $IntValue;
150
151
    /**
152
     * Getter for $indValue property.
153
     *
154
     * @return int
155
     */
156 14
    public function getIntValue()
157
    {
158 14
        return $this->intValue;
159
    }
160
161
    /**
162
     * Gets the string part of the instance.
163
     *
164
     * @var string
165
     */
166
    public $StringValue;
167
168
    /**
169
     * Getter for $stringValue property.
170
     *
171
     * @return int
172
     */
173 101
    public function getStringValue()
174
    {
175 101
        return $this->stringValue;
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181 30
    public function toString()
182
    {
183 30
        return $this->IntValue.$this->StringValue;
184
    }
185
186
    /**
187
     * Indica si el objeto especificado es igual a la instancia actual.
188
     *
189
     * @param IntString|mixed $other
190
     *
191
     * @return bool
192
     */
193 15
    public function equals($other)
194
    {
195 15
        if ($other instanceof IntString) {
196
            if ($this->IntValue === $other->IntValue) {
197
                if ($this->StringValue === $other->StringValue) {
198
                    return true;
199
                }
200
            }
201
        }
202
203 15
        return false;
204
    }
205
206
207
    /**
208
     * Determina la posición relativa de esta instancia con respecto al
209
     * objeto especificado.
210
     * Nota: Cualquier objeto que no sea instancia de IntString se
211
     * considerará menor.
212
     *
213
     * @param IntString|mixed $other Objeto con el que se va a comparar.
214
     *
215
     * @return int Cero (0), si esta instancia es igual a $other; mayor
216
     *   a cero (>0), si es mayor a $other; menor a cero (<0), si es menor.
217
     * */
218
    public function compareTo($other)
219
    {
220
        $r = $this->equals($other) ? 0 : 9999;
221
222
        if ($r != 0) {
223
            if ($other instanceof IntString) {
224
                $r = $this->IntValue - $other->IntValue;
225
226
                if ($r == 0) {
227
                    $r = strnatcmp($this->StringValue, $other->StringValue);
228
                }
229
            } else {
230
                $r = 1;
231
            }
232
        }
233
234
        return $r;
235
    }
236
}
237