You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.3 KiB

3 years ago
  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\ISearchRequestSimpleQuery;
  27. /**
  28. * @since 17.0.0
  29. *
  30. * Class SearchRequestSimpleQuery
  31. *
  32. * @package OC\FullTextSearch\Model
  33. */
  34. final class SearchRequestSimpleQuery implements ISearchRequestSimpleQuery, JsonSerializable {
  35. /** @var int */
  36. private $type = 0;
  37. /** @var string */
  38. private $field = '';
  39. /** @var array */
  40. private $values = [];
  41. /**
  42. * SearchRequestQuery constructor.
  43. *
  44. * @param $type
  45. * @param $field
  46. *
  47. * @since 17.0.0
  48. */
  49. public function __construct(string $field, int $type) {
  50. $this->field = $field;
  51. $this->type = $type;
  52. }
  53. /**
  54. * Get the compare type of the query
  55. *
  56. * @return int
  57. * @since 17.0.0
  58. */
  59. public function getType(): int {
  60. return $this->type;
  61. }
  62. /**
  63. * Get the field to apply query
  64. *
  65. * @return string
  66. * @since 17.0.0
  67. */
  68. public function getField(): string {
  69. return $this->field;
  70. }
  71. /**
  72. * Set the field to apply query
  73. *
  74. * @param string $field
  75. *
  76. * @return ISearchRequestSimpleQuery
  77. * @since 17.0.0
  78. */
  79. public function setField(string $field): ISearchRequestSimpleQuery {
  80. $this->field = $field;
  81. return $this;
  82. }
  83. /**
  84. * Get the value to compare (string)
  85. *
  86. * @return array
  87. * @since 17.0.0
  88. */
  89. public function getValues(): array {
  90. return $this->values;
  91. }
  92. /**
  93. * Add value to compare (string)
  94. *
  95. * @param string $value
  96. *
  97. * @return ISearchRequestSimpleQuery
  98. * @since 17.0.0
  99. */
  100. public function addValue(string $value): ISearchRequestSimpleQuery {
  101. $this->values[] = $value;
  102. return $this;
  103. }
  104. /**
  105. * Add value to compare (int)
  106. *
  107. * @param int $value
  108. *
  109. * @return ISearchRequestSimpleQuery
  110. * @since 17.0.0
  111. */
  112. public function addValueInt(int $value): ISearchRequestSimpleQuery {
  113. $this->values[] = $value;
  114. return $this;
  115. }
  116. /**
  117. * Add value to compare (array)
  118. *
  119. * @param array $value
  120. *
  121. * @return ISearchRequestSimpleQuery
  122. * @since 17.0.0
  123. */
  124. public function addValueArray(array $value): ISearchRequestSimpleQuery {
  125. $this->values[] = $value;
  126. return $this;
  127. }
  128. /**
  129. * Add value to compare (bool)
  130. *
  131. * @param bool $value
  132. *
  133. * @return ISearchRequestSimpleQuery
  134. * @since 17.0.0
  135. */
  136. public function addValueBool(bool $value): ISearchRequestSimpleQuery {
  137. $this->values[] = $value;
  138. return $this;
  139. }
  140. /**
  141. * @return array|mixed
  142. * @since 17.0.0
  143. */
  144. public function jsonSerialize() {
  145. return [
  146. 'type' => $this->getType(),
  147. 'field' => $this->getField(),
  148. 'values' => $this->getValues()
  149. ];
  150. }
  151. }