Completed
Push — master ( 14a06b...6322f3 )
by Nelson
05:31
created

StrictObject   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 18.75%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 136
ccs 6
cts 32
cp 0.1875
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A equals() 0 28 3
A compare() 0 3 1
A __toString() 0 3 1
A __construct() 0 2 1
A toString() 0 24 3
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Copyright © 2014-2019 Nelson Martell (http://nelson6e65.github.io)
6
 *
7
 * Licensed under The MIT License (MIT)
8
 * For full copyright and license information, please see the LICENSE
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright 2014-2019 Nelson Martell
12
 * @link      http://nelson6e65.github.io/php_nml/
13
 * @since     0.1.1
14
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
15
 * */
16
17
namespace NelsonMartell;
18
19
use NelsonMartell\Extensions\Text;
20
use NelsonMartell\Extensions\Arrays;
21
use NelsonMartell\Extensions\Numbers;
22
use NelsonMartell\Extensions\Objects;
23
24
/**
25
 * Base class that encapsulates strict properties and other basic features.
26
 *
27
 *
28
 * @author Nelson Martell <[email protected]>
29
 * @see    PropertiesHandler
30
 *
31
 * @since  0.1.1
32
 * @since 1.0.0 Made it `abstract`.
33
 * */
34
abstract class StrictObject implements IComparer, IStrictPropertiesContainer, IConvertibleToString
35
{
36
    use PropertiesHandler;
37
38
    /**
39
     * Constructor.
40
     */
41 348
    public function __construct()
42
    {
43 348
    }
44
45
    /**
46
     * Convierte esta instancia en su representación de cadena.
47
     * Para modificar el funcionamiento de esta función, debe reemplazarse
48
     * la función ObjectClass::toString()
49
     *
50
     * @return string
51
     * @see    StrictObject::toString()
52
     * */
53 29
    final public function __toString()
54
    {
55 29
        return $this->toString();
56
    }
57
58
    /**
59
     * Convierte la instancia actual en su representación de cadena.
60
     *
61
     * @return string
62
     * */
63
    public function toString()
64
    {
65
        $type = typeof($this);
66
67
        if (defined('CODE_ANALYSIS')) {
68
            if ($type->name != 'NelsonMartell\StrictObject') {
69
                $args = [
70
                    'access'     => 'public',
71
                    'base_class' => __CLASS__,
72
                    'class'      => $type->name,
73
                    'function'   => __FUNCTION__,
74
                ];
75
76
                $msg  = msg('Using default "{base_class}::{function}" ({access}) method.', $args);
77
                $msg .= msg(
78
                    ' You can replace (override) its behavior by creating "{class}::{function}" ({access}) method.',
79
                    $args
80
                );
81
82
                trigger_error($msg, E_USER_NOTICE);
83
            }
84
        }
85
86
        return '{ '.$type.' }';
87
    }
88
89
    /**
90
     * Indicates whether the specified object is equal to the current instance.
91
     *
92
     * @param mixed $other Another object to compare equality.
93
     *
94
     * @return bool Retuns default behaviour of `==`. ***This method must be overridden***.
95
     * */
96
    public function equals($other)
97
    {
98
        if (defined('CODE_ANALYSIS')) {
99
            if ($this instanceof IEquatable) {
100
                $type = typeof($this);
101
102
                $args = [
103
                    'access'     => 'public',
104
                    'base_class' => __CLASS__,
105
                    'class'      => $type->name,
106
                    'function'   => __FUNCTION__,
107
                ];
108
109
                $msg = msg(
110
                    'You implemented IEquatable, but using default "{base_class}::{function}" ({access}) method.',
111
                    $args
112
                );
113
114
                $msg .= msg(
115
                    ' You can replace (override) its behavior by creating "{class}::{function}" ({access}) method.',
116
                    $args
117
                );
118
119
                trigger_error($msg, E_USER_NOTICE);
120
            }
121
        }
122
123
        return $this == $other;
124
    }
125
126
    /**
127
     * Determines the relative position of the object on the left with respect to the one on the right.
128
     *
129
     * This method is compatible with core types and other types. You can implement `NelsonMartell\IComparable`
130
     * in order to improve the beaviour for other classes.
131
     *
132
     * This method can be used as sorting function for `usort()` function.
133
     *
134
     * **Notes:**
135
     * - Comparison is made in natural way if they are of the same type. If not, is used the PHP standard
136
     * comparison.
137
     * - If ``$left`` and ``$right`` are arrays, comparison is made by first by 'key' (as strings) and then by
138
     *   'values' (using this method recursively).
139
     *
140
     * **Override:**
141
     * You can override this method to implement a contextual sorting behaviour for `usort()` function.
142
     * If you only need to compare instances of your class with other objects, implement `NelsonMartell\IComparable`
143
     * instead.
144
     *
145
     * @param mixed $left  Left object.
146
     * @param mixed $right Right object.
147
     *
148
     * @return int|null
149
     *   Returns:
150
     *   - ``= 0`` if $left is considered equivalent to $other;
151
     *   - ``> 0`` if $left is considered greater than $other;
152
     *   - ``< 0`` if $left is considered less than $other;
153
     *   - ``null`` if $left can't be compared to $other .
154
     *
155
     * @see \strnatcmp()
156
     * @see \usort()
157
     * @see Arrays::compare()
158
     * @see IComparable
159
     * @see IComparable::compareTo()
160
     * @see IComparer::compare()
161
     * @see Numbers::compare()
162
     * @see Text::compare()
163
     * @see Objects::compare()
164
     *
165
     * @deprecated 1.0.0 Use `{@see Objects::compare()}` instead.
166
     * */
167 22
    public static function compare($left, $right)
168
    {
169 22
        return Objects::compare($left, $right);
170
    }
171
}
172