|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\generator\name; |
|
3
|
|
|
|
|
4
|
|
|
use keeko\tools\model\Relationship; |
|
5
|
|
|
use keeko\framework\utils\NameUtils; |
|
6
|
|
|
|
|
7
|
|
|
class RelationshipMethodNameGenerator extends AbstractNameGenerator { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Returns the method name in studly case to prefix with add/get/set/remove |
|
11
|
|
|
* and call it on a model class |
|
12
|
|
|
* |
|
13
|
|
|
* @param Relationship $relationship |
|
14
|
|
|
* @return string |
|
15
|
|
|
*/ |
|
16
|
|
|
public function generateMethodName(Relationship $relationship) { |
|
17
|
|
|
// for many-to-many where model and foreign are the same |
|
18
|
|
|
if ($relationship->getType() == Relationship::MANY_TO_MANY && $relationship->isReflexive()) { |
|
19
|
|
|
$lk = $relationship->getLocalKey(); |
|
|
|
|
|
|
20
|
|
|
$foreign = $relationship->getForeign(); |
|
21
|
|
|
return $foreign->getPhpName() . 'RelatedBy' . $lk->getLocalColumn()->getPhpName(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// else |
|
25
|
|
|
return $relationship->getRelatedName(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the plural method name in studly case to prefix with add/get/set/remove |
|
30
|
|
|
* and call it on a model class |
|
31
|
|
|
* |
|
32
|
|
|
* @param Relationship $relationship |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function generatePluralMethodName(Relationship $relationship) { |
|
36
|
|
|
// for many-to-many where model and foreign are the same |
|
37
|
|
|
if ($relationship->getType() == Relationship::MANY_TO_MANY && $relationship->isReflexive()) { |
|
38
|
|
|
$lk = $relationship->getLocalKey(); |
|
|
|
|
|
|
39
|
|
|
$foreign = $relationship->getForeign(); |
|
40
|
|
|
return NameUtils::pluralize($foreign->getPhpName()) . 'RelatedBy' . $lk->getLocalColumn()->getPhpName(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// else |
|
44
|
|
|
return $relationship->getRelatedPluralName(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the reverse method name in studly case to prefix with add/get/set/remove |
|
49
|
|
|
* and call it on a model class |
|
50
|
|
|
* |
|
51
|
|
|
* @param Relationship $relationship |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function generateReverseMethodName(Relationship $relationship) { |
|
55
|
|
|
// for many-to-many where model and foreign are the same |
|
56
|
|
|
if ($relationship->getType() == Relationship::MANY_TO_MANY && $relationship->isReflexive()) { |
|
57
|
|
|
$fk = $relationship->getForeignKey(); |
|
58
|
|
|
$foreign = $relationship->getForeign(); |
|
59
|
|
|
return $foreign->getPhpName() . 'RelatedBy' . $fk->getLocalColumn()->getPhpName(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// else |
|
63
|
|
|
return $relationship->getReverseRelatedName(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the reverse plural method name in studly case to prefix with add/get/set/remove |
|
68
|
|
|
* and call it on a model class |
|
69
|
|
|
* |
|
70
|
|
|
* @param Relationship $relationship |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function generateReversePluralMethodName(Relationship $relationship) { |
|
74
|
|
|
// for many-to-many where model and foreign are the same |
|
75
|
|
|
if ($relationship->getType() == Relationship::MANY_TO_MANY && $relationship->isReflexive()) { |
|
76
|
|
|
$fk = $relationship->getForeignKey(); |
|
77
|
|
|
$foreign = $relationship->getForeign(); |
|
78
|
|
|
return NameUtils::pluralize($foreign->getPhpName()) . 'RelatedBy' . $fk->getLocalColumn()->getPhpName(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// else |
|
82
|
|
|
return $relationship->getReverseRelatedPluralName(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: