1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 ekow. |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace ntentan\nibii; |
28
|
|
|
|
29
|
|
|
use ntentan\panie\InjectionContainer; |
30
|
|
|
|
31
|
|
|
abstract class Relationship |
32
|
|
|
{ |
33
|
|
|
const BELONGS_TO = 'BelongsTo'; |
34
|
|
|
const HAS_MANY = 'HasMany'; |
35
|
|
|
const MANY_HAVE_MANY = 'ManyHaveMany'; |
36
|
|
|
|
37
|
|
|
protected $options = []; |
38
|
|
|
protected $type; |
39
|
|
|
protected $setupName; |
40
|
|
|
protected $setupTable; |
41
|
|
|
protected $setupPrimaryKey; |
42
|
|
|
|
43
|
|
|
private $setup = false; |
44
|
|
|
|
45
|
28 |
|
public function setOptions($options) |
46
|
|
|
{ |
47
|
28 |
|
$this->options = $options; |
48
|
28 |
|
} |
49
|
|
|
|
50
|
|
|
public function getOptions() |
51
|
|
|
{ |
52
|
|
|
return $this->options; |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
public function getModelInstance() |
56
|
|
|
{ |
57
|
12 |
|
if(!$this->setup) { |
58
|
12 |
|
$this->runSetup(); |
59
|
12 |
|
$this->setup = true; |
60
|
|
|
} |
61
|
12 |
|
return InjectionContainer::resolve(Nibii::getClassName($this->options['model'], $this->type)); |
62
|
|
|
} |
63
|
|
|
|
64
|
28 |
|
public function setup($name, $table, $primaryKey) |
65
|
|
|
{ |
66
|
28 |
|
$this->setupName = $name; |
67
|
28 |
|
$this->setupTable = $table; |
68
|
28 |
|
$this->setupPrimaryKey = $primaryKey; |
69
|
28 |
|
} |
70
|
|
|
|
71
|
|
|
abstract public function getQuery($data); |
|
|
|
|
72
|
|
|
abstract public function runSetup(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.