|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ipl\Web\Common; |
| 4 | + |
| 5 | +use ipl\Html\Form; |
| 6 | +use ipl\Stdlib\Events; |
| 7 | +use ipl\Web\Control\ViewModeSwitcher; |
| 8 | +use ipl\Web\Url; |
| 9 | +use Psr\Http\Message\ServerRequestInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Provides factory methods to prepare reusable web controls and allows to handle their requests |
| 13 | + * |
| 14 | + * {@see static::handleControls()} can be called to call {@see Form::handleRequest()} for all Controls created by |
| 15 | + * this trait. |
| 16 | + */ |
| 17 | +trait Controls |
| 18 | +{ |
| 19 | + use Events; |
| 20 | + |
| 21 | + /** @var string Event emitted when the view mode has been changed */ |
| 22 | + public const ON_VIEW_MODE_CHANGE = 'view-mode-change'; |
| 23 | + |
| 24 | + /** @var string Event emmitted when the created {@see ViewModeSwitcher} is populated with a view mode */ |
| 25 | + public const ON_VIEW_MODE_SET = 'view-mode-set'; |
| 26 | + |
| 27 | + /** @var array<Form> Controls for which {@see self::handleControls()} must call {@see Form::handleRequest()} */ |
| 28 | + private array $trackedControls = []; |
| 29 | + |
| 30 | + /** |
| 31 | + * Redirect to the given Url |
| 32 | + * |
| 33 | + * @param Url|string $url |
| 34 | + * |
| 35 | + * @return never |
| 36 | + */ |
| 37 | + abstract protected function redirectNow($url); |
| 38 | + |
| 39 | + /** |
| 40 | + * Create a {@see ViewModeSwitcher} control |
| 41 | + * |
| 42 | + * On {@see ViewModeSwitcher::ON_REQUEST} the created instance is populated with the view mode from the url |
| 43 | + * and {@see static::ON_VIEW_MODE_SET} is emmitted. |
| 44 | + * |
| 45 | + * On {@see ViewModeSwitcher::ON_SUBMIT}, if the view mode was changed, {@see static::ON_VIEW_MODE_CHANGE} |
| 46 | + * is emmitted with the {@see ViewModeSwitcher}, its previous view mode and a redirect url, which listeners |
| 47 | + * may modify. |
| 48 | + * |
| 49 | + * @return ViewModeSwitcher |
| 50 | + */ |
| 51 | + public function createViewModeSwitcher(): ViewModeSwitcher |
| 52 | + { |
| 53 | + $viewModeSwitcher = new ViewModeSwitcher(); |
| 54 | + |
| 55 | + $this->trackControl($viewModeSwitcher); |
| 56 | + |
| 57 | + $viewModeSwitcher->on( |
| 58 | + ViewModeSwitcher::ON_REQUEST, |
| 59 | + function ( |
| 60 | + ServerRequestInterface $request, |
| 61 | + ViewModeSwitcher $viewModeSwitcher |
| 62 | + ) { |
| 63 | + $viewModeSwitcher->populate([ |
| 64 | + $viewModeSwitcher->getViewModeParam() |
| 65 | + => $request->getQueryParams()[$viewModeSwitcher->getViewModeParam()] ?? null |
| 66 | + ]); |
| 67 | + |
| 68 | + $this->emit(static::ON_VIEW_MODE_SET, [$viewModeSwitcher]); |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + $viewModeSwitcher->on( |
| 73 | + ViewModeSwitcher::ON_SUBMIT, |
| 74 | + function ( |
| 75 | + ViewModeSwitcher $viewModeSwitcher |
| 76 | + ): void { |
| 77 | + $previousViewModeName = |
| 78 | + $viewModeSwitcher->getRequest()->getQueryParams()[$viewModeSwitcher->getViewModeParam()] |
| 79 | + ?? $viewModeSwitcher->getDefaultViewMode()->getName(); |
| 80 | + $previousViewMode = $viewModeSwitcher->getViewModes()[$previousViewModeName]; |
| 81 | + |
| 82 | + if ($viewModeSwitcher->getViewMode()->getName() !== $previousViewMode->getName()) { |
| 83 | + $redirectUrl = Url::fromRequest(); |
| 84 | + $this->emit(static::ON_VIEW_MODE_CHANGE, [$viewModeSwitcher, $previousViewMode, $redirectUrl]); |
| 85 | + $redirectUrl->setParam( |
| 86 | + $viewModeSwitcher->getViewModeParam(), |
| 87 | + $viewModeSwitcher->getViewMode()->getName() |
| 88 | + ); |
| 89 | + $this->redirectNow($redirectUrl); |
| 90 | + } |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + return $viewModeSwitcher; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Register the given control for lookup by {@see self::getTrackedControl()} and for {@see self::handleControls()} |
| 99 | + * |
| 100 | + * @param Form $control |
| 101 | + * |
| 102 | + * @return $this |
| 103 | + */ |
| 104 | + protected function trackControl(Form $control): static |
| 105 | + { |
| 106 | + $this->trackedControls[] = $control; |
| 107 | + |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get the tracked control of the given type |
| 113 | + * |
| 114 | + * @template TControl of Form |
| 115 | + * |
| 116 | + * @param class-string<TControl> $type |
| 117 | + * |
| 118 | + * @return ?TControl |
| 119 | + */ |
| 120 | + protected function getTrackedControl(string $type): ?Form |
| 121 | + { |
| 122 | + foreach ($this->trackedControls as $control) { |
| 123 | + if ($control instanceof $type) { |
| 124 | + return $control; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Call {@see Form::handleRequest()} on every control in {@see self::$trackedControls}. |
| 133 | + * |
| 134 | + * @param ServerRequestInterface $request |
| 135 | + * |
| 136 | + * @return $this |
| 137 | + */ |
| 138 | + protected function handleControls(ServerRequestInterface $request): static |
| 139 | + { |
| 140 | + foreach ($this->trackedControls as $control) { |
| 141 | + $control->handleRequest($request); |
| 142 | + } |
| 143 | + |
| 144 | + return $this; |
| 145 | + } |
| 146 | +} |
0 commit comments