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
|
|
|
abstract class Relationship { |
30
|
|
|
|
31
|
|
|
const BELONGS_TO = 'BelongsTo'; |
32
|
|
|
const HAS_MANY = 'HasMany'; |
33
|
|
|
const MANY_HAVE_MANY = 'ManyHaveMany'; |
34
|
|
|
|
35
|
|
|
protected $options = []; |
36
|
|
|
protected $type; |
37
|
|
|
protected $setupName; |
38
|
|
|
protected $setupTable; |
39
|
|
|
protected $setupSchema; |
40
|
|
|
protected $setupPrimaryKey; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var \ntentan\panie\Container |
45
|
|
|
*/ |
46
|
|
|
protected $container; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var ORMContext |
51
|
|
|
*/ |
52
|
|
|
protected $context; |
53
|
|
|
|
54
|
|
|
private $setup = false; |
55
|
|
|
|
56
|
28 |
|
public function setOptions($options) { |
57
|
28 |
|
$this->options = $options; |
58
|
28 |
|
} |
59
|
|
|
|
60
|
|
|
public function getOptions() { |
61
|
|
|
return $this->options; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets an instance of the related model accessed through this class. |
66
|
|
|
* @return RecordWrapper |
67
|
|
|
*/ |
68
|
12 |
|
public function getModelInstance() { |
69
|
12 |
|
if (!$this->setup) { |
70
|
12 |
|
$this->runSetup(); |
71
|
12 |
|
$this->setup = true; |
72
|
|
|
} |
73
|
12 |
|
return $this->container->resolve($this->context->getClassName($this->options['model'], $this->type)); |
74
|
|
|
} |
75
|
|
|
|
76
|
28 |
|
public function setup($name, $schema, $table, $primaryKey) { |
77
|
28 |
|
$this->setupName = $name; |
78
|
28 |
|
$this->setupTable = $table; |
79
|
28 |
|
$this->setupPrimaryKey = $primaryKey; |
80
|
28 |
|
$this->setupSchema = $schema; |
81
|
28 |
|
} |
82
|
|
|
|
83
|
|
|
abstract public function getQuery($data); |
|
|
|
|
84
|
|
|
|
85
|
|
|
abstract public function runSetup(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
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.