1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
declare(strict_types = 1);
|
3
|
|
|
|
4
|
|
|
require_once __DIR__.'/../vendor/autoload.php';
|
5
|
|
|
|
6
|
|
|
// initialisation
|
7
|
|
|
$pdo = new \PDO('sqlite::memory:');
|
8
|
|
|
$databaseAdapter = new \Agares\MicroORM\PDODbAdapter($pdo);
|
9
|
|
|
$entityDefinitionCreator = new \Agares\MicroORM\EntityDefinitionCreator();
|
10
|
|
|
|
11
|
|
|
// create a test table
|
12
|
|
|
$databaseAdapter->executeCommand('CREATE TABLE transactions (product TEXT, price TEXT, price_currency TEXT)');
|
13
|
|
|
|
14
|
|
|
// create some test data
|
15
|
|
|
$transactions = [
|
16
|
|
|
['Wonderful Tea', '9.99', 'GBP'],
|
17
|
|
|
['Lousy Coffee', '10', 'CZK']
|
18
|
|
|
];
|
19
|
|
|
|
20
|
|
|
foreach($transactions as $transaction) {
|
21
|
|
|
$parameters = array(
|
22
|
|
|
':product' => $transaction[0],
|
23
|
|
|
':price' => $transaction[1],
|
24
|
|
|
':price_currency' => $transaction[2]
|
25
|
|
|
);
|
26
|
|
|
|
27
|
|
|
$databaseAdapter->executeCommand('INSERT INTO transactions VALUES(:product, :price, :price_currency)', $parameters);
|
28
|
|
|
}
|
29
|
|
|
|
30
|
|
|
// define a value object
|
31
|
|
|
class Currency
|
|
|
|
|
32
|
|
|
{
|
33
|
|
|
private $amount = '0';
|
34
|
|
|
private $currency = 'XXX';
|
35
|
|
|
|
36
|
|
|
public function __construct(string $amount, string $currency)
|
37
|
|
|
{
|
38
|
|
|
$this->amount = $amount;
|
39
|
|
|
$this->currency = $currency;
|
40
|
|
|
}
|
41
|
|
|
|
42
|
|
|
public function getAmount() : string
|
43
|
|
|
{
|
44
|
|
|
return $this->amount;
|
45
|
|
|
}
|
46
|
|
|
|
47
|
|
|
public function getCurrency() : string
|
48
|
|
|
{
|
49
|
|
|
return $this->currency;
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
public function __toString()
|
53
|
|
|
{
|
54
|
|
|
return sprintf('%s %s', $this->amount, $this->currency);
|
55
|
|
|
}
|
56
|
|
|
}
|
57
|
|
|
|
58
|
|
|
// define an entity
|
59
|
|
|
class Transaction
|
|
|
|
|
60
|
|
|
{
|
61
|
|
|
private $product;
|
62
|
|
|
private $price;
|
63
|
|
|
|
64
|
|
|
public function __construct(string $product, string $price)
|
65
|
|
|
{
|
66
|
|
|
$this->product = $product;
|
67
|
|
|
$this->price = $price;
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
public function getProduct() : string
|
71
|
|
|
{
|
72
|
|
|
return $this->product;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
public function getPrice() : \Currency
|
76
|
|
|
{
|
77
|
|
|
return $this->price;
|
78
|
|
|
}
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
// we need a custom TypeMapper for our value object
|
82
|
|
|
class CurrencyTypeMapper implements \Agares\MicroORM\TypeMapperInterface
|
|
|
|
|
83
|
|
|
{
|
84
|
|
|
public function fromString(string $fieldName, array $fields)
|
85
|
|
|
{
|
86
|
|
|
return new Currency($fields[$fieldName], $fields[$fieldName . '_currency']);
|
87
|
|
|
}
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
// let's try this:
|
91
|
|
|
$typeMappers = [
|
92
|
|
|
'string' => new \Agares\MicroORM\TypeMappers\StringTypeMapper(),
|
93
|
|
|
'int' => new \Agares\MicroORM\TypeMappers\IntegerTypeMapper(),
|
94
|
|
|
'Currency' => new CurrencyTypeMapper()
|
95
|
|
|
];
|
96
|
|
|
|
97
|
|
|
$entityMapper = new \Agares\MicroORM\EntityMapper($typeMappers);
|
98
|
|
|
$queryAdapter = new \Agares\MicroORM\QueryAdapter($databaseAdapter, $entityMapper);
|
99
|
|
|
$transactions = $queryAdapter->executeQuery('SELECT product, price, price_currency FROM transactions', $entityDefinitionCreator->create(Transaction::class));
|
100
|
|
|
foreach($transactions as $transaction) {
|
101
|
|
|
/** @var Transaction $transaction */
|
102
|
|
|
printf('%s (%s)%s', $transaction->getProduct(), (string)$transaction->getPrice(), PHP_EOL);
|
103
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.