Passed
Push — master ( 9f58b4...83392d )
by Rougin
03:13
created

Relation::field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rougin\Windstorm\Relation;
4
5
use Rougin\Windstorm\QueryInterface;
6
7
/**
8
 * Relation
9
 *
10
 * @package Windstorm
11
 * @author  Rougin Gutib <[email protected]>
12
 */
13
abstract class Relation
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $alias = array();
19
20
    /**
21
     * @var array
22
     */
23
    protected $fields = array();
24
25
    /**
26
     * @var string|null
27
     */
28
    protected $foreign = null;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $primary = null;
34
35
    /**
36
     * @var \Rougin\Windstorm\QueryInterface
37
     */
38
    protected $query;
39
40
    /**
41
     * Initializes the relation interface.
42
     *
43
     * @param \Rougin\Windstorm\QueryInterface $query
44
     */
45 9
    public function __construct(QueryInterface $query)
46
    {
47 9
        $this->fields[] = array();
48
49 9
        $this->fields[] = array();
50
51 9
        $this->query = $query;
52 9
    }
53
54
    /**
55
     * Sets the field/column of a foreign/primary table.
56
     *
57
     * @param  integer $type
58
     * @param  string $name
59
     * @return self
60
     */
61 9
    public function field($type, $name)
62
    {
63 9
        $this->fields[$type][] = $name;
64 9
    }
65
66
    /**
67
     * Sets the fields of a foreign/primary table.
68
     *
69
     * @param  integer  $type
70
     * @param  string[] $values
71
     * @return self
72
     */
73 3
    public function fields($type, array $values)
74
    {
75 3
        $this->fields[$type] = array();
76
77 3
        foreach ($values as $field)
78
        {
79 3
            $this->field($type, $field);
80 2
        }
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * Sets the foreign table.
87
     *
88
     * @param  string      $table
89
     * @param  string|null $alias
90
     * @return self
91
     */
92 9
    public function foreign($table, $alias = null)
93
    {
94 9
        $this->foreign = $table;
95
96 9
        $this->alias[1] = $alias ?: $table[0];
97 9
    }
98
99
    /**
100
     * Sets the primary table.
101
     *
102
     * @param  string      $table
103
     * @param  string|null $alias
104
     * @return self
105
     */
106 9
    public function primary($table, $alias = null)
107
    {
108 9
        $this->primary = $table;
109
110 9
        $this->alias[0] = $alias ?: $table[0];
111 9
    }
112
113
    /**
114
     * Parses specified fields as table columns.
115
     *
116
     * @param  integer $type
117
     * @param  boolean $prefix
118
     * @return array
119
     */
120 9
    protected function columns($type, $prefix = true)
121
    {
122 9
        $alias = $this->alias[$type];
123
124 9
        $columns = array();
125
126 9
        foreach ($this->fields[$type] as $value)
127
        {
128 9
            if ($type === 1 && $prefix)
129 6
            {
130 3
                $column = $alias . '_' . $value;
131
132 3
                $value .= " as $column";
133 2
            }
134
135 9
            $columns[] = $alias . '.' . $value;
136 6
        }
137
138 9
        return (array) $columns;
139
    }
140
}
141