Passed
Push — master ( 2fc2d6...8a960a )
by Nelson
02:43 queued 01:00
created

StrictObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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