Action.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Notification;
  24. use OCP\Notification\IAction;
  25. class Action implements IAction {
  26. protected string $label;
  27. protected string $labelParsed;
  28. protected string $link;
  29. protected string $requestType;
  30. protected string $icon;
  31. protected bool $primary;
  32. public function __construct() {
  33. $this->label = '';
  34. $this->labelParsed = '';
  35. $this->link = '';
  36. $this->requestType = '';
  37. $this->primary = false;
  38. }
  39. /**
  40. * @param string $label
  41. * @return $this
  42. * @throws \InvalidArgumentException if the label is invalid
  43. * @since 8.2.0
  44. */
  45. public function setLabel(string $label): IAction {
  46. if ($label === '' || isset($label[32])) {
  47. throw new \InvalidArgumentException('The given label is invalid');
  48. }
  49. $this->label = $label;
  50. return $this;
  51. }
  52. /**
  53. * @return string
  54. * @since 8.2.0
  55. */
  56. public function getLabel(): string {
  57. return $this->label;
  58. }
  59. /**
  60. * @param string $label
  61. * @return $this
  62. * @throws \InvalidArgumentException if the label is invalid
  63. * @since 8.2.0
  64. */
  65. public function setParsedLabel(string $label): IAction {
  66. if ($label === '') {
  67. throw new \InvalidArgumentException('The given parsed label is invalid');
  68. }
  69. $this->labelParsed = $label;
  70. return $this;
  71. }
  72. /**
  73. * @return string
  74. * @since 8.2.0
  75. */
  76. public function getParsedLabel(): string {
  77. return $this->labelParsed;
  78. }
  79. /**
  80. * @param $primary bool
  81. * @return $this
  82. * @since 9.0.0
  83. */
  84. public function setPrimary(bool $primary): IAction {
  85. $this->primary = $primary;
  86. return $this;
  87. }
  88. /**
  89. * @return bool
  90. * @since 9.0.0
  91. */
  92. public function isPrimary(): bool {
  93. return $this->primary;
  94. }
  95. /**
  96. * @param string $link
  97. * @param string $requestType
  98. * @return $this
  99. * @throws \InvalidArgumentException if the link is invalid
  100. * @since 8.2.0
  101. */
  102. public function setLink(string $link, string $requestType): IAction {
  103. if ($link === '' || isset($link[256])) {
  104. throw new \InvalidArgumentException('The given link is invalid');
  105. }
  106. if (!in_array($requestType, [
  107. self::TYPE_GET,
  108. self::TYPE_POST,
  109. self::TYPE_PUT,
  110. self::TYPE_DELETE,
  111. self::TYPE_WEB,
  112. ], true)) {
  113. throw new \InvalidArgumentException('The given request type is invalid');
  114. }
  115. $this->link = $link;
  116. $this->requestType = $requestType;
  117. return $this;
  118. }
  119. /**
  120. * @return string
  121. * @since 8.2.0
  122. */
  123. public function getLink(): string {
  124. return $this->link;
  125. }
  126. /**
  127. * @return string
  128. * @since 8.2.0
  129. */
  130. public function getRequestType(): string {
  131. return $this->requestType;
  132. }
  133. /**
  134. * @return bool
  135. */
  136. public function isValid(): bool {
  137. return $this->label !== '' && $this->link !== '';
  138. }
  139. /**
  140. * @return bool
  141. */
  142. public function isValidParsed(): bool {
  143. return $this->labelParsed !== '' && $this->link !== '';
  144. }
  145. }