SearchOption.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\FullTextSearch\Model;
  25. use JsonSerializable;
  26. use OCP\FullTextSearch\Model\ISearchOption;
  27. /**
  28. * @since 15.0.0
  29. *
  30. * Class ISearchOption
  31. *
  32. * @package OC\FullTextSearch\Model
  33. */
  34. final class SearchOption implements ISearchOption, JsonSerializable {
  35. /**
  36. * *
  37. *
  38. * The array can be empty in case no search options are available.
  39. * The format of the array must be like this:
  40. *
  41. * [
  42. * 'panel' => [
  43. * 'options' => [
  44. * OPTION1,
  45. * OPTION2,
  46. * OPTION3
  47. * ]
  48. * ],
  49. * 'navigation' => [
  50. * 'icon' => 'css-class-of-the-icon',
  51. * 'options' => [
  52. * OPTION1,
  53. * OPTION2,
  54. * OPTION3
  55. * ]
  56. * ]
  57. * ]
  58. *
  59. * - PANEL contains entries that will be displayed in the app itself, when
  60. * a search is initiated.
  61. * - NAVIGATION contains entries that will be available when using the
  62. * FullTextSearch navigation page
  63. * - OPTION is an element that define each option available to the user.
  64. *
  65. * The format for the options must be like this:
  66. *
  67. * [
  68. * 'name' => 'name_of_the_option',
  69. * 'title' => 'Name displayed in the panel',
  70. * 'type' => '',
  71. * 'size' => '' (optional),
  72. * 'placeholder' => '' (optional)
  73. * ]
  74. *
  75. * - NAME is the variable name that is sent to the IFullTextSearchProvider
  76. * when a ISearchRequest is requested. (keys in the array returned by the
  77. * ISearchRequest->getOptions())
  78. * - TYPE can be 'input' or 'checkbox'
  79. * - SIZE is only used in case TYPE='input', default is 'large' but can be
  80. * 'small'
  81. * - PLACEHOLDER is only used in case TYPE='input', default is empty.
  82. */
  83. /**
  84. * ISearchOption constructor.
  85. *
  86. * Some value can be set during the creation of the object.
  87. *
  88. * @since 15.0.0
  89. */
  90. public function __construct(
  91. private string $name = '',
  92. private string $title = '',
  93. private string $type = '',
  94. private string $size = '',
  95. private string $placeholder = '',
  96. ) {
  97. }
  98. /**
  99. * Set the name/key of the option.
  100. * The string should only contain alphanumerical chars and underscore.
  101. * The key can be retrieved when using ISearchRequest::getOption
  102. *
  103. * @see ISearchRequest::getOption
  104. *
  105. * @since 15.0.0
  106. */
  107. public function setName(string $name): ISearchOption {
  108. $this->name = $name;
  109. return $this;
  110. }
  111. /**
  112. * Get the name/key of the option.
  113. *
  114. * @since 15.0.0
  115. */
  116. public function getName(): string {
  117. return $this->name;
  118. }
  119. /**
  120. * Set the title/display name of the option.
  121. *
  122. * @since 15.0.0
  123. */
  124. public function setTitle(string $title): ISearchOption {
  125. $this->title = $title;
  126. return $this;
  127. }
  128. /**
  129. * Get the title of the option.
  130. *
  131. * @since 15.0.0
  132. */
  133. public function getTitle(): string {
  134. return $this->title;
  135. }
  136. /**
  137. * Set the type of the option.
  138. * $type can be ISearchOption::CHECKBOX or ISearchOption::INPUT
  139. *
  140. * @since 15.0.0
  141. */
  142. public function setType(string $type): ISearchOption {
  143. $this->type = $type;
  144. return $this;
  145. }
  146. /**
  147. * Get the type of the option.
  148. *
  149. * @since 15.0.0
  150. */
  151. public function getType(): string {
  152. return $this->type;
  153. }
  154. /**
  155. * In case of Type is INPUT, set the size of the input field.
  156. * Value can be ISearchOption::INPUT_SMALL or not defined.
  157. *
  158. * @since 15.0.0
  159. */
  160. public function setSize(string $size): ISearchOption {
  161. $this->size = $size;
  162. return $this;
  163. }
  164. /**
  165. * Get the size of the INPUT.
  166. *
  167. * @since 15.0.0
  168. */
  169. public function getSize(): string {
  170. return $this->size;
  171. }
  172. /**
  173. * In case of Type is , set the placeholder to be displayed in the input
  174. * field.
  175. *
  176. * @since 15.0.0
  177. */
  178. public function setPlaceholder(string $placeholder): ISearchOption {
  179. $this->placeholder = $placeholder;
  180. return $this;
  181. }
  182. /**
  183. * Get the placeholder.
  184. *
  185. * @since 15.0.0
  186. */
  187. public function getPlaceholder(): string {
  188. return $this->placeholder;
  189. }
  190. /**
  191. * @since 15.0.0
  192. */
  193. public function jsonSerialize(): array {
  194. return [
  195. 'name' => $this->getName(),
  196. 'title' => $this->getTitle(),
  197. 'type' => $this->getType(),
  198. 'size' => $this->getSize(),
  199. 'placeholder' => $this->getPlaceholder()
  200. ];
  201. }
  202. }