DataMapper::applyValues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Palladium\Component;
4
5
use PDO;
6
7
abstract class DataMapper
8
{
9
10
    protected $connection;
11
    protected $table;
12
13
14
    /**
15
     * Creates new mapper instance
16
     *
17
     * @param PDO $connection
18
     * @param string $table A list of table name aliases
19
     *
20
     * @codeCoverageIgnore
21
     */
22
    public function __construct(PDO $connection, string $table)
23
    {
24
        $this->connection = $connection;
25
        $this->table = $table;
26
    }
27
28
29
    /**
30
     * Method for populating the given instance with values from the array via setters
31
     *
32
     * @param object $instance The object to be populated with values
33
     * @param array $parameters A key-value array, that will be matched to setters
34
     */
35 18
    public function applyValues($instance, array $parameters)
36
    {
37 18
        foreach ($parameters as $key => $value) {
38 18
            $method = 'set' . str_replace('_', '', $key);
39 18
            if (method_exists($instance, $method)) {
40 18
                $instance->{$method}($value);
41
            }
42
        }
43 18
    }
44
45
}
46