Completed
Push — master ( e5ab12...9197f0 )
by Konstantinos
09:09
created

Model   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 56.25%
Metric Value
wmc 26
lcom 1
cbo 3
dl 0
loc 190
ccs 27
cts 48
cp 0.5625
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A isDeleted() 0 8 3
A isActive() 0 4 1
A getStatus() 0 8 2
A isSameAs() 0 7 4
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
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
    public function isDeleted()
29
    {
30
        if (!$this->isValid() || $this->getStatus() == 'deleted') {
31
            return true;
32
        }
33
34
        return false;
35
    }
36
37
    /**
38
     * Find if the model is active (i.e. visible to everyone)
39
     *
40
     * @return bool
41
     */
42
    public function isActive()
43
    {
44
        return in_array($this->getStatus(), $this->getActiveStatuses());
45
    }
46
47
    /**
48
     * Get the models's status
49
     *
50
     * @return string
51
     */
52
    public function getStatus()
53
    {
54
        if (!isset($this->status)) {
55
            return 'active';
56
        }
57
58
        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 1
    public function isSameAs(Model $model) {
68 1
        if (!$this->isValid() || !$model->isValid()) {
69
            return false;
70
        }
71
72 1
        return $this->getType() === $model->getType() && $this->getId() === $model->getId();
73
    }
74
75
    /**
76
     * Get the possible statuses representing an active model (visible to everyone)
77
     *
78
     * @return string[]
79
     */
80
    public static function getActiveStatuses()
81
    {
82
        return array('active');
83
    }
84
85
    /**
86
     * Converts an array of IDs to an array of Models
87
     * @param  int[] $idArray The list of IDs
88
     * @return array An array of models
89
     */
90 39
    public static function arrayIdToModel($idArray)
91
    {
92 39
        $return = array();
93 39
        foreach ($idArray as $id) {
94 39
            $return[] = static::get($id);
95
        }
96
97 39
        return $return;
98
    }
99
100
    /**
101
     * Converts an array of Models to an array of IDs
102
     *
103
     * All model type information is lost
104
     *
105
     * @param  ModelInterface[] $modelArray The list of models
106
     * @return int[] An array of IDs
107
     */
108
    public static function mapToIDs($modelArray)
109
    {
110
        return array_map(function (ModelInterface $model) {
111
            return $model->getId();
112
        }, $modelArray);
113
    }
114
115
    /**
116
     * Update a property and the corresponding database column
117
     *
118
     * @param  mixed  $property The protected class property to update
119
     * @param  string $dbColumn The name of the database column to update
120
     * @param  mixed  $value    The value to insert
121
     * @param  string $type     The mysqli type of the value (s, i, d, b)
122
     * @return self   Returns the model itself to allow method chaining
123
     */
124 2
    protected function updateProperty(&$property, $dbColumn, $value, $type = 'i')
125
    {
126
        // Don't waste time with mysql if there aren't any changes
127 2
        if ($property != $value) {
128 2
            $property = $value;
129
130 2
            if ($value instanceof TimeDate) {
131 1
                $value = $value->toMysql();
132
            }
133
134 2
            $this->update($dbColumn, $value, $type);
135
        }
136
137 2
        return $this;
138
    }
139
140
    /**
141
     * Gets the type of the model
142
     * @return string The type of the model, e.g. "server"
143
     */
144 1
    public static function getType()
145
    {
146 1
        return self::toSnakeCase(get_called_class());
147
    }
148
149
    /**
150
     * Gets a human-readable format of the model's type
151
     * @return string
152
     */
153
    public static function getTypeForHumans()
154
    {
155
        return self::getType();
156
    }
157
158
    /**
159
     * Change a parameter if a model is not valid
160
     *
161
     * Useful for form validation
162
     *
163
     * @param  string $property The name of the property to change
164
     * @param  mixed  $value    The value of the property
165
     * @return self
166
     */
167
    protected function inject($property, $value)
168
    {
169
        if (!$this->isValid()) {
170
            $this->{$property} = $value;
171
        }
172
173
        return $this;
174
    }
175
176
    /**
177
     * Takes a CamelCase string and converts it to a snake_case one
178
     * @param  string $input The string to convert
179
     * @return string
180
     */
181 1
    private static function toSnakeCase($input)
182
    {
183 1
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
184 1
        $ret = $matches[0];
185
186 1
        foreach ($ret as &$match) {
187 1
            $match = ($match == strtoupper($match)) ? strtolower($match) : lcfirst($match);
188
        }
189
190 1
        return implode('_', $ret);
191
    }
192
193
    /**
194
     * Escape special HTML characters from a string
195
     * @param  string  $string
196
     * @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...
197
     */
198 1
    public static function escape($string)
199
    {
200 1
        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
201
    }
202
}
203