SearchOption.php 5.9 KB

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