Passed
Branch v1.4.0 (ac3196)
by Wanderson
01:13
created

Insert::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Database\Sql\Queries;
4
5
use Win\Database\Orm;
6
use Win\Database\Sql\Query;
7
8
/**
9
 * INSERT INTO
10
 */
11
class Insert extends Query
12
{
13
	/** @var mixed[] */
14
	protected $values;
15
16
	public function __construct(Orm $orm)
17
	{
18
		parent::__construct($orm);
19
		$this->values = $orm->getRowValues();
20
	}
21
22
	/** @return string */
23
	public function toString()
24
	{
25
		$columns = array_keys($this->values);
26
		$params = $this->getParams();
27
28
		return 'INSERT INTO ' .
29
				$this->table .
30
				' (' . implode(',', $columns) . ')' .
31
				' VALUES (' . implode(', ', $params) . ')';
32
	}
33
34
	/**
35
	 * @return string[]
36
	 * @example return ['?','?','?']
37
	 */
38
	protected function getParams()
39
	{
40
		return str_split(str_repeat('?', count($this->values)));
41
	}
42
43
	/** @return mixed[] */
44
	public function getValues()
45
	{
46
		return array_values($this->values);
47
	}
48
49
	/** @return bool */
50
	public function execute()
51
	{
52
		return $this->connection->query($this, $this->getValues());
53
	}
54
}
55