Passed
Push — master ( 9878bf...07917c )
by Robson
01:09
created

DataLayer::findById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace CoffeeCode\DataLayer;
4
5
use PDO;
6
use PDOException;
7
use stdClass;
8
9
class DataLayer
10
{
11
    private $entity;
12
    private $primary;
13
    private $required;
14
15
    protected $statement;
16
17
    protected $params;
18
    protected $order;
19
    protected $limit;
20
    protected $offset;
21
22
    protected $fail;
23
    protected $data;
24
25
26
    public function __construct(string $entity, array $requiredFileds, string $primaryKey = 'id')
27
    {
28
        $this->entity = $entity;
29
        $this->primary = $primaryKey;
30
        $this->required = $requiredFileds;
31
    }
32
33
    public function __set($name, $value)
34
    {
35
        if (empty($this->data)) {
36
            $this->data = new stdClass();
37
        }
38
39
        $this->data->$name = $value;
40
    }
41
42
    public function __isset($name)
43
    {
44
        return isset($this->data->$name);
45
    }
46
47
    public function __get($name)
48
    {
49
        return ($this->data->$name ?? null);
50
    }
51
52
    public function data(): ?object
53
    {
54
        return $this->data;
55
    }
56
57
    public function fail(): ?PDOException
58
    {
59
        return $this->fail;
60
    }
61
62
    public function find(?string $terms = null, ?string $params = null, string $columns = "*"): DataLayer
63
    {
64
        if ($terms) {
65
            $this->statement = "SELECT {$columns} FROM {$this->entity} WHERE {$terms}";
66
            parse_str($params, $this->params);
67
            return $this;
68
        }
69
70
        $this->statement = "SELECT {$columns} FROM {$this->entity}";
71
        return $this;
72
    }
73
74
    public function findById(int $id, string $columns = "*"): ?DataLayer
75
    {
76
        $find = $this->find($this->primary . " = :id", "id={$id}", $columns);
77
        return $find->fetch();
78
    }
79
80
    public function order(string $columnOrder): ?DataLayer
81
    {
82
        $this->order = " ORDER BY {$columnOrder}";
83
        return $this;
84
    }
85
86
    public function limit(int $limit): ?DataLayer
87
    {
88
        $this->limit = " LIMIT {$limit}";
89
        return $this;
90
    }
91
92
    public function offset(int $offset): ?DataLayer
93
    {
94
        $this->offset = " OFFSET {$offset}";
95
        return $this;
96
    }
97
98
    public function fetch(bool $all = false)
99
    {
100
        try {
101
            $stmt = Connect::getInstance()->prepare($this->statement . $this->order . $this->limit . $this->offset);
102
            $stmt->execute($this->params);
103
104
            if (!$stmt->rowCount()) {
105
                return null;
106
            }
107
108
            if ($all) {
109
                return $stmt->fetchAll(PDO::FETCH_CLASS, static::class);
110
            }
111
112
            return $stmt->fetchObject(static::class);
113
        } catch (PDOException $exception) {
114
            $this->fail = $exception;
115
            return null;
116
        }
117
    }
118
119
    public function count(): int
120
    {
121
        $stmt = Connect::getInstance()->prepare($this->statement);
122
        $stmt->execute($this->params);
123
        return $stmt->rowCount();
124
    }
125
}