[TASK] Make LocalConfiguration.php PSR-2 standard compliant
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / Components / ButtonBar.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template\Components;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
18 use TYPO3\CMS\Backend\Template\Components\Buttons\FullyRenderedButton;
19 use TYPO3\CMS\Backend\Template\Components\Buttons\InputButton;
20 use TYPO3\CMS\Backend\Template\Components\Buttons\LinkButton;
21 use TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Bar holding the buttons
26 */
27 class ButtonBar
28 {
29 /**
30 * Identifier for the left button bar
31 */
32 const BUTTON_POSITION_LEFT = 'left';
33
34 /**
35 * Identifier for the right button bar
36 */
37 const BUTTON_POSITION_RIGHT = 'right';
38
39 /**
40 * Internal array of all registered buttons
41 *
42 * @var array
43 */
44 protected $buttons = [];
45
46 /**
47 * Add button
48 *
49 * @param ButtonInterface $button The Button Object to add
50 * @param string $buttonPosition Position of the button (left/right)
51 * @param int $buttonGroup Buttongroup of the button
52 *
53 * @throws \InvalidArgumentException In case a button is not valid
54 *
55 * @return $this
56 */
57 public function addButton(
58 ButtonInterface $button,
59 $buttonPosition = self::BUTTON_POSITION_LEFT,
60 $buttonGroup = 1
61 ) {
62 if (!$button->isValid($button)) {
63 throw new \InvalidArgumentException('Button "' . $button->getType() . '" is not valid', 1441706370);
64 }
65 // We make the button immutable here
66 $this->buttons[$buttonPosition][$buttonGroup][] = clone $button;
67 return $this;
68 }
69
70 /**
71 * Creates a new button of the given type
72 *
73 * @param string $button ButtonClass to invoke. Must implement ButtonInterface
74 *
75 * @throws \InvalidArgumentException In case a ButtonClass does not implement
76 * ButtonInterface
77 *
78 * @return ButtonInterface
79 */
80 public function makeButton($button)
81 {
82 if (!in_array(ButtonInterface::class, class_implements($button), true)) {
83 throw new \InvalidArgumentException('A Button must implement ButtonInterface', 1441706378);
84 }
85 return GeneralUtility::makeInstance($button);
86 }
87
88 /**
89 * Creates a new InputButton
90 *
91 * @return InputButton
92 */
93 public function makeInputButton()
94 {
95 return GeneralUtility::makeInstance(InputButton::class);
96 }
97
98 /**
99 * Creates a new SplitButton
100 *
101 * @return SplitButton
102 */
103 public function makeSplitButton()
104 {
105 return GeneralUtility::makeInstance(SplitButton::class);
106 }
107
108 /**
109 * Creates a new LinkButton
110 *
111 * @return LinkButton
112 */
113 public function makeLinkButton()
114 {
115 return GeneralUtility::makeInstance(LinkButton::class);
116 }
117
118 /**
119 * Creates a new FullyRenderedButton
120 *
121 * @return FullyRenderedButton
122 */
123 public function makeFullyRenderedButton()
124 {
125 return GeneralUtility::makeInstance(FullyRenderedButton::class);
126 }
127
128 /**
129 * Returns an associative array of all buttons in the form of
130 * ButtonPosition > ButtonGroup > Button
131 *
132 * @return array
133 */
134 public function getButtons()
135 {
136 // here we need to call the sorting methods and stuff.
137 foreach ($this->buttons as $position => $_) {
138 ksort($this->buttons[$position]);
139 }
140 // @todo do we want to provide a hook here?
141 return $this->buttons;
142 }
143 }