|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Utils\ArrayHash; |
|
6
|
|
|
use App\Models\BlockModel; |
|
7
|
|
|
use App\Models\ProgramModel; |
|
8
|
|
|
use Nette\Database\Table\ActiveRow; |
|
9
|
|
|
|
|
10
|
|
|
class AnnotationService |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var BlockModel |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $blockModel; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ProgramModel |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $programModel; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param BlockModel $block |
|
25
|
|
|
* @param ProgramModel $program |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(BlockModel $block, ProgramModel $program) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setBlockModel($block); |
|
30
|
|
|
$this->setProgramModel($program); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $id |
|
|
|
|
|
|
35
|
|
|
* @param string $type |
|
36
|
|
|
* @return Row |
|
37
|
|
|
*/ |
|
38
|
|
|
public function findByType(string $guid, string $type) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->getModelByType($type)->findBy('guid', $guid); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ActiveRow $annotation |
|
45
|
|
|
* @return ActiveRow |
|
46
|
|
|
*/ |
|
47
|
|
|
public function findParentProgram(ActiveRow $annotation): ActiveRow |
|
48
|
|
|
{ |
|
49
|
|
|
if(array_key_exists('block', $annotation->toArray())) { |
|
50
|
|
|
$parentProgram = $this->getBlockModel()->find($annotation->block); |
|
51
|
|
|
} else { |
|
52
|
|
|
$parentProgram = $annotation; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $parentProgram; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $type |
|
60
|
|
|
* @param ArrayHash $annotation |
|
61
|
|
|
* @return Row |
|
62
|
|
|
*/ |
|
63
|
|
|
public function updateByType(string $type, ArrayHash $annotation) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->getModelByType($type)->updateBy('guid', $annotation->guid, (array) $annotation); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $type |
|
70
|
|
|
* @return ProgramModel | BLockModel |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getModelByType(string $type) |
|
73
|
|
|
{ |
|
74
|
|
|
switch ($type) { |
|
75
|
|
|
case 'block': |
|
76
|
|
|
$model = $this->getBlockModel(); |
|
|
|
|
|
|
77
|
|
|
break; |
|
78
|
|
|
case 'program': |
|
79
|
|
|
$model = $this->getProgramModel(); |
|
80
|
|
|
break; |
|
81
|
|
|
default: |
|
82
|
|
|
throw new Exception('Annotation model not found!'); |
|
83
|
|
|
break; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $model; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return ProgramModel |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getProgramModel() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->programModel; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param ProgramModel $model |
|
99
|
|
|
* @return self |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function setProgramModel(ProgramModel $model): self |
|
102
|
|
|
{ |
|
103
|
|
|
$this->programModel = $model; |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return BlockModel |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getBlockModel(): BlockModel |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->blockModel; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param BlockModel $model |
|
118
|
|
|
* @return self |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function setBlockModel(BlockModel $model): self |
|
121
|
|
|
{ |
|
122
|
|
|
$this->blockModel = $model; |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.