1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Sirius\Orm\CodeGenerator\Observer\Relation; |
5
|
|
|
|
6
|
|
|
use Nette\PhpGenerator\ClassType; |
7
|
|
|
use Sirius\Orm\Blueprint\Mapper; |
8
|
|
|
use Sirius\Orm\Contract\Relation\ToManyInterface; |
9
|
|
|
use Sirius\Orm\Helpers\Str; |
10
|
|
|
|
11
|
|
|
class ManyToManyObserver extends OneToManyObserver implements ToManyInterface |
12
|
|
|
{ |
13
|
|
|
public function observe(string $key, $object) |
14
|
|
|
{ |
15
|
|
|
if ($key === $this->relation->getForeignMapper() . '_base_entity') { |
16
|
|
|
return $this->observeLinkedBaseEntity($object); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return parent::observe($key, $object); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function observeLinkedBaseEntity(ClassType $class) |
23
|
|
|
{ |
24
|
|
|
$throughColumns = $this->relation->getThroughColumns(); |
|
|
|
|
25
|
|
|
if (empty($throughColumns)) { |
26
|
|
|
return $class; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
foreach ($throughColumns as $column) { |
30
|
|
|
$comment = 'unsed only for relations with ' . $this->relation->getMapper()->getName(); |
31
|
|
|
if ($this->relation->getMapper()->getEntityStyle() === Mapper::ENTITY_STYLE_PROPERTIES) { |
32
|
|
|
$class->addComment(sprintf('@property mixed $%s - %s', $column, $comment)); |
33
|
|
|
} else { |
34
|
|
|
$setter = $class->addMethod(Str::methodName($column, 'set')); |
35
|
|
|
$setter->setVisibility(ClassType::VISIBILITY_PUBLIC); |
36
|
|
|
$setter->addParameter('value'); |
37
|
|
|
$setter->addBody('$this->set(\'' . $column . '\', $value);'); |
38
|
|
|
$setter->setComment($comment); |
39
|
|
|
|
40
|
|
|
$getter = $class->addMethod(Str::methodName($column, 'get')); |
41
|
|
|
$getter->setVisibility(ClassType::VISIBILITY_PUBLIC); |
42
|
|
|
$getter->addBody('return $this->get(\'' . $column . '\');'); |
43
|
|
|
$setter->setComment($comment); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $class; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.