/
AbstractDatabase.php
73 lines (63 loc) · 1.84 KB
/
AbstractDatabase.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
<?php
namespace DBAL\Database;
use DBAL\Condition;
use \Psr\Log\LoggerAwareInterface;
use \Psr\Log\LoggerInterface;
use \Psr\Log\NullLogger;
abstract class AbstractDatabase implements LoggerAwareInterface, ReadableInterface, WritableInterface
{
private $logger;
/**
* Instantiates the AbstractDatabase. Should be called from inheritors.
*/
public function __construct()
{
$this->logger = new NullLogger();
}
/**
* @param string $table The table that will be accessed and written.
* @param Condition|null $criteria The criteria that will filter the records.
* @param array $options The list of options that will help with finding the records.
* @return array|null One record from table, or null if there aren't any matching records.
*/
public function find($table, Condition $criteria = null, array $options = [])
{
$options['limit'] = 1;
$data = $this->findAll($table, $criteria, $options);
$result = reset($data);
if ($result === false) {
return null;
}
return $result;
}
/**
* Returns the current logger object.
*
* @return LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
/**
* Sets the logger to $value.
*
* @param LoggerInterface $value A logger to be used for this database.
* @return void
*/
public function setLogger(LoggerInterface $value)
{
if (null === $value) {
$value = new NullLogger();
}
$this->logger = $value;
}
/**
* Returns the id of the last record created, or null if no creation took place.
*
* Must be implemented by inheritors.
*
* @return mixed
*/
abstract public function lastIdCreated();
}