Completed
Push — master ( 20e1fc...2f76e5 )
by Ivan
02:17
created

TableQueryMapped   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 33
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A iterator() 0 4 1
A create() 0 4 1
1
<?php
2
namespace vakata\database\schema;
3
4
use vakata\collection\Collection;
5
use vakata\database\DBInterface;
6
use vakata\database\DBException;
7
use vakata\database\ResultInterface;
8
9
/**
10
 * A database query class with mapping
11
 */
12
class TableQueryMapped extends TableQuery
13
{
14
    protected static $mappers = [];
15
    protected $mapper;
16
17 36
    public function __construct(DBInterface $db, $table)
18
    {
19 36
        parent::__construct($db, $table);
20 36
        if (!isset(static::$mappers[spl_object_hash($db)])) {
21 4
            static::$mappers[spl_object_hash($db)] = new Mapper($db);
22
        }
23 36
        $this->mapper = static::$mappers[spl_object_hash($db)];
24 36
    }
25
    /**
26
     * Perform the actual fetch
27
     * @param  array|null $fields optional array of columns to select (related columns can be used too)
28
     * @return Collection               the query result as a mapped Collection
29
     */
30 32
    public function iterator(array $fields = null)
31
    {
32 32
        return $this->mapper->collection(parent::iterator($fields), $this->definition);
33
    }
34
    /**
35
     * Create an empty entity for the queried table.
36
     *
37
     * @param array $data optional array of data to populate with
38
     * @return object
39
     */
40 1
    public function create(array $data = [])
41
    {
42 1
        return $this->mapper->entity($this->definition, $data, true);
43
    }
44
}