Completed
Push — master ( cdc4c0...532c28 )
by Paweł
02:43
created

Transaction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 2.

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.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
}