/
PdoArticleRepository.php
154 lines (135 loc) · 3.89 KB
/
PdoArticleRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
* This file is part of pmg/three-repositories
*
* Copyright (c) PMG <https://www.pmg.com>
*
* For full copyright information see the LICENSE file distributed
* with this source code.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace PMG\ThreeRepositories;
/**
* An `ArticleRepository` implementation backed by plain old PDO and embedded
* SQL.
*
* @since 0.1
*/
final class PdoArticleRepository implements ArticleRepository
{
const TABLE = 'articles';
/**
* @var PDO
*/
private $conn;
public function __construct(\PDO $conn)
{
$this->conn = $conn;
}
/**
* {@inheritdoc}
*/
public function find($id)
{
$stm = $this->conn->prepare($this->getSelect().' WHERE id = :id LIMIT 1');
$stm->bindValue(':id', $id, \PDO::PARAM_INT);
$stm->setFetchMode(\PDO::FETCH_ASSOC);
$stm->execute();
$res = $stm->fetch();
$stm->closeCursor();
return $res ? $this->toObject($res) : null;
}
/**
* {@inheritdoc}
*/
public function findAll()
{
$stm = $this->conn->query(
$this->getSelect().' ORDER BY publish_year DESC, title ASC'
);
$out = $this->statementToObjects($stm);
$stm->closeCursor();
return $out;
}
/**
* {@inheritdoc}
*/
public function findByYear($year)
{
$stm = $this->conn->prepare(
$this->getSelect().' WHERE publish_year = :year ORDER BY publish_year DESC, title ASC'
);
$stm->bindValue(':year', $year, \PDO::PARAM_INT);
$stm->execute();
$out = $this->statementToObjects($stm);
$stm->closeCursor();
return $out;
}
/**
* {@inheritdoc}
*/
public function add(Article $article)
{
$id = $article->getIdentifier();
$params = [
':title' => $article->getTitle(),
':body' => $article->getBody(),
':year' => $article->getYear(),
];
$bind = [
':year' => \PDO::PARAM_INT,
];
if ($id) {
$sql = 'UPDATE '.self::TABLE.' SET title = :title, body = :body, publish_year = :year WHERE id = :id';
$params[':id'] = $id;
$bind[':id'] = \PDO::PARAM_INT;
} else {
$sql = 'INSERT INTO '.self::TABLE.' (title, body, publish_year) VALUES (:title, :body, :year)';
}
$stm = $this->conn->prepare($sql);
foreach ($params as $name => $val) {
$stm->bindValue(
$name,
$val,
isset($bind[$name]) ? $bind[$name] : \PDO::PARAM_STR
);
}
$stm->execute();
return $id ? $id : intval($this->conn->lastInsertId());
}
/**
* {@inheritdoc}
*/
public function remove($article)
{
$stm = $this->conn->prepare('DELETE FROM '.self::TABLE.' WHERE id = :id');
$stm->bindValue(
':id',
$article instanceof Article ? $article->getIdentifier() : $article,
\PDO::PARAM_INT
);
$stm->execute();
}
// may make more sense to use a factory object here, but
// let's keep this somewhat simple.
private function toObject(array $row)
{
$article = new SimpleArticle($row['id']);
$article->setTitle($row['title']);
$article->setBody($row['body']);
$article->setYear($row['publish_year']);
return $article;
}
private function statementToObjects(\PDOStatement $stm)
{
// could very easily use an iterator or a generator here
// with big data sets, again, let's keep it simple.
$stm->setFetchMode(\PDO::FETCH_ASSOC);
return array_map([$this, 'toObject'], $stm->fetchAll());
}
private function getSelect()
{
return 'SELECT id, title, body, publish_year FROM '.self::TABLE;
}
}