Passed
Push — master ( 2c6473...93bae8 )
by Edson
11:31
created

Model::deleteAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bonfim\ActiveRecord;
6
7
use PDO;
8
use PDOStatement;
9
use ReflectionClass;
10
11
use Bonfim\ActiveRecord\ActiveRecord as AR;
12
13
abstract class Model
14
{
15
    public static function all(): array
16
    {
17
        $consulta = AR::execute("SELECT * FROM `".self::getTable()."`");
18
        return is_null($consulta) ? [] : self::bind($consulta);
19
    }
20
21
    public static function find(string $where, array $values): array
22
    {
23
        $consulta = AR::execute("SELECT * FROM `".self::getTable()."` ".$where, $values);
24
        return is_null($consulta) ? [] : self::bind($consulta);
25
    }
26
27
    public static function select(array $keys, string $cond = '', array $values = []): array
28
    {
29
        $q = "SELECT ".implode(', ', $keys)." FROM ".self::getTable()." $cond";
30
        $consulta = AR::execute($q, $values);
31
        return is_null($consulta) ? [] : self::bind($consulta);
32
    }
33
34
    public function save(): int
35
    {
36
        $this->getProperties($keys, $values);
37
38
        $q = "INSERT INTO `".self::getTable()."` (`".implode('`, `', $keys)."`) VALUES (";
39
        for ($i = 0; $i < count($keys) - 1; $i++) {
40
            $q .= "?, ";
41
        }
42
        $q .= "?)";
43
44
        return AR::execute($q, $values)->rowCount();
45
    }
46
47
    public function update(): int
48
    {
49
        $this->getProperties($keys, $values);
50
        $values[] = $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Bonfim\ActiveRecord\Model. Did you maybe forget to declare it?
Loading history...
51
52
        $q = "UPDATE `".self::getTable()."` SET " . "`";
53
        $q .= implode('` = ?, `', $keys);
54
        $q .= "` = ? WHERE `id` = ?";
55
56
        return AR::execute($q, $values)->rowCount();
57
    }
58
59
    public function delete(): ?PDOStatement
60
    {
61
        return AR::execute("DELETE FROM `".self::getTable()."` WHERE `id` = ?", [$this->id]);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Bonfim\ActiveRecord\Model. Did you maybe forget to declare it?
Loading history...
62
    }
63
64
    public static function deleteAll(): int
65
    {
66
        return AR::execute("DELETE FROM `".self::getTable()."`")->rowCount();
67
    }
68
69
    private static function getTable(): string
70
    {
71
        $class = new ReflectionClass(get_called_class());
72
        return Inflect::pluralize(strtolower($class->getShortName()));
73
    }
74
75
    private function getProperties(&$keys, &$values)
76
    {
77
        $properties = (new ReflectionClass(get_called_class()))->getProperties();
78
79
        foreach ($properties as $property) {
80
            $keys[]   = $property->name;
81
            $values[] = $this->{$property->name};
82
        }
83
    }
84
85
    private static function bind(PDOStatement $exec): array
86
    {
87
        $resp  = [];
88
        $class = get_called_class();
89
90
        foreach ($exec->fetchAll(PDO::FETCH_OBJ) as $i => $fetch) {
91
            $resp[$i] = new $class;
92
            foreach ($fetch as $k => $v) {
93
                $resp[$i]->$k = $v;
94
            }
95
        }
96
97
        return $resp;
98
    }
99
}
100