Passed
Branch v1.4.0 (4e3f66)
by Wanderson
01:21
created

Orm   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A orm() 0 3 1
A getConnection() 0 3 1
A getModel() 0 3 1
A getTable() 0 3 1
A setConnection() 0 3 1
A getQuery() 0 3 1
A getRowValues() 0 3 1
A setModel() 0 3 1
A modelExists() 0 3 1
1
<?php
2
3
namespace Win\Database;
4
5
use Win\Database\Orm\Model;
6
use Win\Database\Orm\Traits\DebugTrait;
7
use Win\Database\Orm\Traits\OrderTrait;
8
use Win\Database\Orm\Traits\ReadTrait;
9
use Win\Database\Orm\Traits\WriteTrait;
10
use Win\Database\Sql\Queries\Select;
11
use Win\Singleton\SingletonTrait;
12
13
/**
14
 * Object Relational Mapping
15
 */
16
abstract class Orm implements RepositoryInterface
17
{
18
	use SingletonTrait {
19
		__construct as finalConstruct;
20
	}
21
22
	use OrderTrait;
23
	use DebugTrait;
24
	use ReadTrait;
25
	use WriteTrait;
26
27
	/** @var Connection */
28
	protected static $conn;
29
30
	/** @var string */
31
	protected $table;
32
33
	/** @var Model */
34
	protected $model;
35
36
	/** @var Select */
37
	protected $query;
38
39
	public function __construct()
40
	{
41
		$this->query = new Select($this);
42
	}
43
44
	/** @return Orm */
45
	protected function orm()
46
	{
47
		return $this;
48
	}
49
50
	/** @return Select */
51
	protected function getQuery()
52
	{
53
		return $this->query;
54
	}
55
56
	/** @param Connection $conn */
57
	public static function setConnection(Connection $conn)
58
	{
59
		static::$conn = $conn;
60
	}
61
62
	/** @return Connection */
63
	public function getConnection()
64
	{
65
		return static::$conn;
66
	}
67
68
	/**
69
	 * @param Model $model
70
	 * @return mixed[]
71
	 */
72
	abstract public function mapRow($model);
73
74
	/**
75
	 * @param mixed[] $row
76
	 * @return Model
77
	 */
78
	abstract public function mapModel($row);
79
80
	/** @return mixed[] */
81
	public function getRowValues()
82
	{
83
		return $this->mapRow($this->getModel());
84
	}
85
86
	/** @return string */
87
	public function getTable()
88
	{
89
		return $this->table;
90
	}
91
92
	/** @return Model */
93
	public function getModel()
94
	{
95
		return $this->model;
96
	}
97
98
	/** @return Model */
99
	protected function setModel(Model $model)
100
	{
101
		return $this->model = $model;
102
	}
103
104
	/** @return bool */
105
	public function modelExists()
106
	{
107
		return $this->model->getId() > 0;
108
	}
109
}
110