4
0

ViewMode.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Represents a view mode option for the reading configuration
  5. */
  6. final readonly class FreshRSS_ViewMode {
  7. public const DEFAULT_VIEW_MODE = 'normal';
  8. private string $id;
  9. private string $name;
  10. private string $controller;
  11. private string $action;
  12. public function __construct(string $id, string $name, string $controller = 'index', string $action = '') {
  13. $this->id = $id;
  14. $this->name = $name;
  15. $this->controller = $controller;
  16. $this->action = $action ?: $id;
  17. }
  18. public function id(): string {
  19. return $this->id;
  20. }
  21. public function name(): string {
  22. return $this->name;
  23. }
  24. public function controller(): string {
  25. return $this->controller;
  26. }
  27. public function action(): string {
  28. return $this->action;
  29. }
  30. /**
  31. * @return array<string,FreshRSS_ViewMode> Mode ID => FreshRSS_ViewMode
  32. */
  33. public static function getDefaultModes(): array {
  34. return [
  35. 'normal' => new self(id: 'normal', name: _t('conf.reading.view.normal'), controller: 'index', action: 'normal'),
  36. 'reader' => new self(id: 'reader', name: _t('conf.reading.view.reader'), controller: 'index', action: 'reader'),
  37. 'global' => new self(id: 'global', name: _t('conf.reading.view.global'), controller: 'index', action: 'global'),
  38. ];
  39. }
  40. /**
  41. * @return array<string,FreshRSS_ViewMode> Mode ID => FreshRSS_ViewMode
  42. */
  43. public static function getAllModes(): array {
  44. $modes = self::getDefaultModes();
  45. // Allow extensions to add their own view modes
  46. $extensionModes = Minz_ExtensionManager::callHook(Minz_HookType::ViewModes, []);
  47. if (is_array($extensionModes)) {
  48. foreach ($extensionModes as $mode) {
  49. if ($mode instanceof FreshRSS_ViewMode) {
  50. $modes[$mode->id()] = $mode;
  51. }
  52. }
  53. }
  54. return $modes;
  55. }
  56. public static function exists(string $mode): bool {
  57. return isset(self::getAllModes()[$mode]);
  58. }
  59. }