provider.php 2.2 KB

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