Provider.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andrew Brown <andrew@casabrown.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCP\Search;
  27. /**
  28. * Provides a template for search functionality throughout ownCloud;
  29. * @since 7.0.0
  30. */
  31. abstract class Provider {
  32. /**
  33. * @since 8.0.0
  34. */
  35. const OPTION_APPS = 'apps';
  36. /**
  37. * List of options
  38. * @var array
  39. * @since 7.0.0
  40. */
  41. protected $options;
  42. /**
  43. * Constructor
  44. * @param array $options as key => value
  45. * @since 7.0.0 - default value for $options was added in 8.0.0
  46. */
  47. public function __construct($options = array()) {
  48. $this->options = $options;
  49. }
  50. /**
  51. * get a value from the options array or null
  52. * @param string $key
  53. * @return mixed
  54. * @since 8.0.0
  55. */
  56. public function getOption($key) {
  57. if (is_array($this->options) && isset($this->options[$key])) {
  58. return $this->options[$key];
  59. } else {
  60. return null;
  61. }
  62. }
  63. /**
  64. * checks if the given apps and the apps this provider has results for intersect
  65. * returns true if the given array is empty (all apps)
  66. * or if this provider does not have a list of apps it provides results for (legacy search providers)
  67. * or if the two above arrays have elements in common (intersect)
  68. * @param string[] $apps
  69. * @return bool
  70. * @since 8.0.0
  71. */
  72. public function providesResultsFor(array $apps = array()) {
  73. $forApps = $this->getOption(self::OPTION_APPS);
  74. return empty($apps) || empty($forApps) || array_intersect($forApps, $apps);
  75. }
  76. /**
  77. * Search for $query
  78. * @param string $query
  79. * @return array An array of OCP\Search\Result's
  80. * @since 7.0.0
  81. */
  82. abstract public function search($query);
  83. }