Completed
Push — master ( e6e2aa...27549f )
by Thomas
09:03
created

ManyRelationship::getLocalKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace keeko\tools\model;
3
4
use Propel\Generator\Model\Table;
5
use Propel\Generator\Model\CrossForeignKeys;
6
use Propel\Generator\Model\ForeignKey;
7
use keeko\framework\utils\NameUtils;
8
9
class ManyRelationship extends Relationship {
10
	
11
	/** @var ForeignKey */
12
	protected $lk;
13
	
14
	/** @var CrossForeignKeys */
15
	protected $cfk;
16
	
17
	public function __construct(Table $model, CrossForeignKeys $cfk) {
18
		$this->model = $model;
19
		$this->cfk = $cfk;
20
		
21
		foreach ($cfk->getMiddleTable()->getForeignKeys() as $fk) {
22
			if ($fk->getForeignTable() != $model) {
23
				$this->fk = $fk;
24
			} else if ($fk->getForeignTable() == $model) {
25
				$this->lk = $fk;
26
			}
27
		}
28
		
29
		$this->foreign = $this->fk->getForeignTable();
30
	}
31
	
32
	/**
33
	 * Returns the type of this relationship
34
	 *
35
	 * @return string
36
	 */
37
	public function getType() {
38
		return 'many';
39
	}
40
	
41
	/**
42
	 * @return Table
43
	 */
44
	public function getMiddle() {
45
		return $this->cfk->getMiddleTable();
46
	}
47
	
48
	/**
49
	 * @return CrossForeignKeys
50
	 */
51
	public function getCrossForeignKeys() {
52
		return $this->cfk;
53
	}
54
	
55
	/**
56
	 * @return ForeignKey
57
	 */
58
	public function getLocalKey() {
59
		return $this->lk;
60
	}
61
	
62
	public function getRelatedName() {
63
		$relatedName = $this->lk->getRefPhpName();
64
		if ($relatedName === null) {
65
			$relatedName = $this->foreign->getPhpName();
66
		}
67
		
68
		return $relatedName;
69
	}
70
71
	public function getRelatedTypeName() {
72
		return NameUtils::dasherize(NameUtils::pluralize($this->getRelatedName()));
73
	}
74
	
75
}