Provider.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }