Completed
Push — master ( 5ae7b2...20e1fc )
by Ivan
02:21
created

TableQueryMapped::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
crap 2
nc 1
nop 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 14
    public function __construct(DBInterface $db, $table)
18
    {
19 14
        parent::__construct($db, $table);
20 14
        if (!isset(static::$mappers[spl_object_hash($db)])) {
21 2
            static::$mappers[spl_object_hash($db)] = new Mapper($db);
22
        }
23 14
        $this->mapper = static::$mappers[spl_object_hash($db)];
24 14
    }
25 12
    public function iterator(array $fields = null) : \Iterator
26
    {
27 12
        return $this->mapper->collection(parent::iterator($fields), $this->definition);
28
    }
29
    public function create(array $data = [])
30
    {
31
        return $this->mapper->entity($this->definition, $data, true);
32
    }
33
}