Completed
Push — master ( c5c126...29bff4 )
by Edson
01:37
created

Model::only()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 extends AR
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 only(array $keys)
28
    {
29
        $sql = "SELECT ".implode(', ', $keys). " FROM ".self::getTable();
30
        $consulta = AR::execute($sql);
31
        return is_null($consulta) ? [] : self::bind($consulta);
32
    }
33
34
    public static function select(array $keys, string $cond = '', array $values = []): array
35
    {
36
        $q = "SELECT ".implode(', ', $keys)." FROM ".self::getTable()." $cond";
37
        $consulta = AR::execute($q, $values);
38
        return is_null($consulta) ? [] : self::bind($consulta);
39
    }
40
41
    public function save(): int
42
    {
43
        $this->getProperties($keys, $values);
44
45
        $q = "INSERT INTO `".self::getTable()."` (`".implode('`, `', $keys)."`) VALUES (";
46
        for ($i = 0; $i < count($keys) - 1; $i++) {
47
            $q .= "?, ";
48
        }
49
        $q .= "?)";
50
51
        return AR::execute($q, $values)->rowCount();
52
    }
53
54
    public function update(): int
55
    {
56
        $this->getProperties($keys, $values);
57
        $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...
58
59
        $q = "UPDATE `".self::getTable()."` SET " . "`";
60
        $q .= implode('` = ?, `', $keys);
61
        $q .= "` = ? WHERE `id` = ?";
62
63
        return AR::execute($q, $values)->rowCount();
64
    }
65
66
    public function delete(): int
67
    {
68
        return AR::execute("DELETE FROM `".self::getTable()."` WHERE `id` = ?", [$this->id])->rowCount();
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...
69
    }
70
71
    public static function deleteAll(): int
72
    {
73
        return AR::execute("DELETE FROM `".self::getTable()."`")->rowCount();
74
    }
75
76
    private static function getTable(): string
77
    {
78
        $class = new ReflectionClass(get_called_class());
79
        return Inflect::pluralize(strtolower($class->getShortName()));
80
    }
81
82
    private function getProperties(&$keys, &$values)
83
    {
84
        $properties = (new ReflectionClass(get_called_class()))->getProperties();
85
86
        foreach ($properties as $property) {
87
            $keys[]   = $property->name;
88
            $values[] = $this->{$property->name};
89
        }
90
    }
91
92
    private static function bind(PDOStatement $exec): array
93
    {
94
        $resp  = [];
95
        $class = get_called_class();
96
97
        foreach ($exec->fetchAll(PDO::FETCH_OBJ) as $i => $fetch) {
98
            $resp[$i] = new $class;
99
            foreach ($fetch as $k => $v) {
100
                $resp[$i]->$k = $v;
101
            }
102
        }
103
104
        return $resp;
105
    }
106
}
107