1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Foo\Grid\Action; |
6
|
|
|
|
7
|
|
|
use Foo\Grid\Component\Component; |
8
|
|
|
use Foo\Grid\Component\IComponent; |
9
|
|
|
use Foo\Translate\ITranslator; |
10
|
|
|
use Opulence\Orm\IEntity; |
11
|
|
|
|
12
|
|
|
class Button extends Component implements IAction |
13
|
|
|
{ |
14
|
|
|
const TAG_A = 'a'; |
15
|
|
|
const TAG_BUTTON = 'button'; |
16
|
|
|
|
17
|
|
|
const CLASS_PRIMARY = 'btn btn-primary'; |
18
|
|
|
const CLASS_DANGER = 'btn btn-danger'; |
19
|
|
|
const CLASS_SUCCESS = 'btn btn-success'; |
20
|
|
|
const CLASS_INFO = 'btn btn-info'; |
21
|
|
|
const CLASS_WARNING = 'btn btn-warning'; |
22
|
|
|
const CLASS_LINK = 'btn btn-link'; |
23
|
|
|
|
24
|
|
|
/** @var IEntity */ |
25
|
|
|
protected $entity; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
protected $attributeCallbacks = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param IComponent|string $content |
32
|
|
|
* @param string $tag |
33
|
|
|
* @param array $attributes |
34
|
|
|
* @param array $attributeCallbacks |
35
|
|
|
* @param ITranslator|null $translator |
36
|
|
|
*/ |
37
|
11 |
|
public function __construct( |
38
|
|
|
$content, |
39
|
|
|
string $tag = self::TAG_A, |
40
|
|
|
array $attributes = [], |
41
|
|
|
array $attributeCallbacks = [], |
42
|
|
|
ITranslator $translator = null |
43
|
|
|
) { |
44
|
11 |
|
$this->attributeCallbacks = $attributeCallbacks; |
45
|
|
|
|
46
|
11 |
|
parent::__construct($content, $tag, $attributes, $translator); |
|
|
|
|
47
|
11 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return \Closure |
51
|
|
|
*/ |
52
|
|
|
public static function getDefaultCallback(): \Closure |
53
|
|
|
{ |
54
|
|
|
return function ($attribute, IEntity $entity) { |
55
|
|
|
return sprintf($attribute, $entity->getId()); |
56
|
|
|
}; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param IEntity $entity |
61
|
|
|
*/ |
62
|
|
|
public function setEntity(IEntity $entity) |
63
|
|
|
{ |
64
|
|
|
$this->entity = $entity; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
6 |
|
public function __toString(): string |
71
|
|
|
{ |
72
|
6 |
|
foreach ($this->attributeCallbacks as $attribute => $callback) { |
73
|
3 |
|
$this->attributes[$attribute] = $callback($this->attributes[$attribute], $this->entity); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
return parent::__toString(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return IAction |
81
|
|
|
*/ |
82
|
1 |
|
public function duplicate(): IAction |
83
|
|
|
{ |
84
|
1 |
|
return new Button($this->content, $this->tag, $this->attributes, $this->attributeCallbacks, $this->translator); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.