SearchOption.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /** @var string */
  36. private $name = '';
  37. /** @var string */
  38. private $title = '';
  39. /** @var string */
  40. private $type = '';
  41. /** @var string */
  42. private $size = '';
  43. /** @var string */
  44. private $placeholder = '';
  45. /**
  46. * *
  47. *
  48. * The array can be empty in case no search options are available.
  49. * The format of the array must be like this:
  50. *
  51. * [
  52. * 'panel' => [
  53. * 'options' => [
  54. * OPTION1,
  55. * OPTION2,
  56. * OPTION3
  57. * ]
  58. * ],
  59. * 'navigation' => [
  60. * 'icon' => 'css-class-of-the-icon',
  61. * 'options' => [
  62. * OPTION1,
  63. * OPTION2,
  64. * OPTION3
  65. * ]
  66. * ]
  67. * ]
  68. *
  69. * - PANEL contains entries that will be displayed in the app itself, when
  70. * a search is initiated.
  71. * - NAVIGATION contains entries that will be available when using the
  72. * FullTextSearch navigation page
  73. * - OPTION is an element that define each option available to the user.
  74. *
  75. * The format for the options must be like this:
  76. *
  77. * [
  78. * 'name' => 'name_of_the_option',
  79. * 'title' => 'Name displayed in the panel',
  80. * 'type' => '',
  81. * 'size' => '' (optional),
  82. * 'placeholder' => '' (optional)
  83. * ]
  84. *
  85. * - NAME is the variable name that is sent to the IFullTextSearchProvider
  86. * when a ISearchRequest is requested. (keys in the array returned by the
  87. * ISearchRequest->getOptions())
  88. * - TYPE can be 'input' or 'checkbox'
  89. * - SIZE is only used in case TYPE='input', default is 'large' but can be
  90. * 'small'
  91. * - PLACEHOLDER is only used in case TYPE='input', default is empty.
  92. */
  93. /**
  94. * ISearchOption constructor.
  95. *
  96. * Some value can be setduring the creation of the object.
  97. *
  98. * @since 15.0.0
  99. *
  100. * @param string $name
  101. * @param string $title
  102. * @param string $type
  103. * @param string $size
  104. * @param string $placeholder
  105. */
  106. public function __construct(string $name = '', string $title = '', string $type = '', string $size = '', string $placeholder = '') {
  107. $this->name = $name;
  108. $this->title = $title;
  109. $this->type = $type;
  110. $this->size = $size;
  111. $this->placeholder = $placeholder;
  112. }
  113. /**
  114. * Set the name/key of the option.
  115. * The string should only contains alphanumerical chars and underscore.
  116. * The key can be retrieve when using ISearchRequest::getOption
  117. *
  118. * @see ISearchRequest::getOption
  119. *
  120. * @since 15.0.0
  121. *
  122. * @param string $name
  123. *
  124. * @return ISearchOption
  125. */
  126. public function setName(string $name): ISearchOption {
  127. $this->name = $name;
  128. return $this;
  129. }
  130. /**
  131. * Get the name/key of the option.
  132. *
  133. * @since 15.0.0
  134. *
  135. * @return string
  136. */
  137. public function getName(): string {
  138. return $this->name;
  139. }
  140. /**
  141. * Set the title/display name of the option.
  142. *
  143. * @since 15.0.0
  144. *
  145. * @param string $title
  146. *
  147. * @return ISearchOption
  148. */
  149. public function setTitle(string $title): ISearchOption {
  150. $this->title = $title;
  151. return $this;
  152. }
  153. /**
  154. * Get the title of the option.
  155. *
  156. * @since 15.0.0
  157. *
  158. * @return string
  159. */
  160. public function getTitle(): string {
  161. return $this->title;
  162. }
  163. /**
  164. * Set the type of the option.
  165. * $type can be ISearchOption::CHECKBOX or ISearchOption::INPUT
  166. *
  167. * @since 15.0.0
  168. *
  169. * @param string $type
  170. *
  171. * @return ISearchOption
  172. */
  173. public function setType(string $type): ISearchOption {
  174. $this->type = $type;
  175. return $this;
  176. }
  177. /**
  178. * Get the type of the option.
  179. *
  180. * @since 15.0.0
  181. *
  182. * @return string
  183. */
  184. public function getType(): string {
  185. return $this->type;
  186. }
  187. /**
  188. * In case of Type is INPUT, set the size of the input field.
  189. * Value can be ISearchOption::INPUT_SMALL or not defined.
  190. *
  191. * @since 15.0.0
  192. *
  193. * @param string $size
  194. *
  195. * @return ISearchOption
  196. */
  197. public function setSize(string $size): ISearchOption {
  198. $this->size = $size;
  199. return $this;
  200. }
  201. /**
  202. * Get the size of the INPUT.
  203. *
  204. * @since 15.0.0
  205. *
  206. * @return string
  207. */
  208. public function getSize(): string {
  209. return $this->size;
  210. }
  211. /**
  212. * In case of Type is , set the placeholder to be displayed in the input
  213. * field.
  214. *
  215. * @since 15.0.0
  216. *
  217. * @param string $placeholder
  218. *
  219. * @return ISearchOption
  220. */
  221. public function setPlaceholder(string $placeholder): ISearchOption {
  222. $this->placeholder = $placeholder;
  223. return $this;
  224. }
  225. /**
  226. * Get the placeholder.
  227. *
  228. * @since 15.0.0
  229. *
  230. * @return string
  231. */
  232. public function getPlaceholder(): string {
  233. return $this->placeholder;
  234. }
  235. /**
  236. * @since 15.0.0
  237. *
  238. * @return array
  239. */
  240. public function jsonSerialize(): array {
  241. return [
  242. 'name' => $this->getName(),
  243. 'title' => $this->getTitle(),
  244. 'type' => $this->getType(),
  245. 'size' => $this->getSize(),
  246. 'placeholder' => $this->getPlaceholder()
  247. ];
  248. }
  249. }