Completed
Push — master ( 8f238b...4d49a8 )
by Konstantinos
03:51
created

Model   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.91%
Metric Value
wmc 29
lcom 1
cbo 3
dl 0
loc 212
ccs 50
cts 55
cp 0.9091
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A isActive() 0 4 1
A isDeleted() 0 8 3
A getStatus() 0 8 2
B isSameAs() 0 9 5
A getActiveStatuses() 0 4 1
A arrayIdToModel() 0 9 2
A mapToIDs() 0 6 1
A updateProperty() 0 15 3
A getType() 0 4 1
A getTypeForHumans() 0 4 1
A inject() 0 8 2
A toSnakeCase() 0 11 3
A escape() 0 4 1
A createFromDatabaseResults() 0 13 2
1
<?php
2
/**
3
 * This file contains the skeleton for all of the database objects
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * A database object (e.g. A player or a team)
11
 * @package    BZiON\Models
12
 */
13
abstract class Model extends CachedModel
14
{
15
    /**
16
     * Generates a string with the object's type and ID
17
     */
18 2
    public function __toString()
19
    {
20 2
        return get_class($this) . " #" . $this->getId();
21
    }
22
23
    /**
24
     * Find if the model is in the trash can (or doesn't exist)
25
     *
26
     * @return bool True if the model has been deleted
27
     */
28 1
    public function isDeleted()
29
    {
30 1
        if (!$this->isValid() || $this->getStatus() == 'deleted') {
31 1
            return true;
32
        }
33
34 1
        return false;
35
    }
36
37
    /**
38
     * Find if the model is active (i.e. visible to everyone)
39
     *
40
     * @return bool
41
     */
42 1
    public function isActive()
43
    {
44 1
        return in_array($this->getStatus(), $this->getActiveStatuses());
45
    }
46
47
    /**
48
     * Get the models's status
49
     *
50
     * @return string
51
     */
52 1
    public function getStatus()
53
    {
54 1
        if (!isset($this->status)) {
55
            return 'active';
56
        }
57
58 1
        return $this->status;
0 ignored issues
show
Bug introduced by
The property status does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
    }
60
61
    /**
62
     * Find if two objects represent the same model
63
     *
64
     * @param  Model   $model The model to compare
65
     * @return boolean
66
     */
67 2
    public function isSameAs(Model $model) {
68 2
        if (!$this->isValid() || !$model->isValid()) {
69 1
            return false;
70
        }
71
72 2
        $sameType = $this instanceof $model || $model instanceof $this;
73
74
        return $sameType && $this->getId() === $model->getId();
75
    }
76
77
    /**
78
     * Get the possible statuses representing an active model (visible to everyone)
79
     *
80
     * @return string[]
81
     */
82
    public static function getActiveStatuses()
83
    {
84
        return array('active');
85
    }
86
87
    /**
88
     * Converts an array of IDs to an array of Models
89
     * @param  int[] $idArray The list of IDs
90 39
     * @return array An array of models
91
     */
92 39
    public static function arrayIdToModel($idArray)
93 39
    {
94 39
        $return = array();
95
        foreach ($idArray as $id) {
96
            $return[] = static::get($id);
97 39
        }
98
99
        return $return;
100
    }
101
102
    /**
103
     * Converts an array of Models to an array of IDs
104
     *
105
     * All model type information is lost
106
     *
107
     * @param  ModelInterface[] $modelArray The list of models
108
     * @return int[] An array of IDs
109
     */
110
    public static function mapToIDs($modelArray)
111
    {
112
        return array_map(function (ModelInterface $model) {
113
            return $model->getId();
114
        }, $modelArray);
115
    }
116
117
    /**
118
     * Update a property and the corresponding database column
119
     *
120
     * @param  mixed  $property The protected class property to update
121
     * @param  string $dbColumn The name of the database column to update
122
     * @param  mixed  $value    The value to insert
123
     * @param  string $type     The mysqli type of the value (s, i, d, b)
124 3
     * @return self   Returns the model itself to allow method chaining
125
     */
126
    protected function updateProperty(&$property, $dbColumn, $value, $type = 'i')
127 3
    {
128 3
        // Don't waste time with mysql if there aren't any changes
129
        if ($property != $value) {
130 3
            $property = $value;
131 1
132
            if ($value instanceof TimeDate) {
133
                $value = $value->toMysql();
134 3
            }
135
136
            $this->update($dbColumn, $value, $type);
137 3
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * Gets the type of the model
144 2
     * @return string The type of the model, e.g. "server"
145
     */
146 2
    public static function getType()
147
    {
148
        return self::toSnakeCase(get_called_class());
149
    }
150
151
    /**
152
     * Gets a human-readable format of the model's type
153 1
     * @return string
154
     */
155 1
    public static function getTypeForHumans()
156
    {
157
        return self::getType();
158
    }
159
160
    /**
161
     * Change a parameter if a model is not valid
162
     *
163
     * Useful for form validation
164
     *
165
     * @param  string $property The name of the property to change
166
     * @param  mixed  $value    The value of the property
167 1
     * @return self
168
     */
169 1
    protected function inject($property, $value)
170 1
    {
171
        if (!$this->isValid()) {
172
            $this->{$property} = $value;
173 1
        }
174
175
        return $this;
176
    }
177
178
    /**
179
     * Takes a CamelCase string and converts it to a snake_case one
180
     * @param  string $input The string to convert
181 2
     * @return string
182
     */
183 2
    private static function toSnakeCase($input)
184 2
    {
185
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
186 2
        $ret = $matches[0];
187 2
188
        foreach ($ret as &$match) {
189
            $match = ($match == strtoupper($match)) ? strtolower($match) : lcfirst($match);
190 2
        }
191
192
        return implode('_', $ret);
193
    }
194
195
    /**
196
     * Escape special HTML characters from a string
197
     * @param  string  $string
198 2
     * @return $string
0 ignored issues
show
Documentation introduced by
The doc-type $string could not be parsed: Unknown type name "$string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
199
     */
200 2
    public static function escape($string)
201
    {
202
        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
203
    }
204
205
    /**
206
     * Create model objects, given their MySQL entries
207
     *
208
     * @param  array $results The MySQL rows of the model
209 1
     * @return static[]
210
     */
211 1
    public static function createFromDatabaseResults(&$results)
212
    {
213 1
        $models = array();
214 1
215 1
        foreach ($results as $result) {
216
            $model = new static($result['id'], $result);
217 1
            $model->storeInCache();
218
219
            $models[] = $model;
220 1
        }
221
222
        return $models;
223
    }
224
}
225