Skip to content

Instantly share code, notes, and snippets.

@staabm
Last active December 17, 2015 01:09
Show Gist options
  • Save staabm/5526027 to your computer and use it in GitHub Desktop.
Save staabm/5526027 to your computer and use it in GitHub Desktop.
watson
<?php
// baseclass
public abstract class AbstractWatsonSearcher {
/**
* @return string[] an array of supported keywords
* /
abstract function keywords();
/**
* Search for the given SearchTerm
*
* @return SearchResult
* /
abstract function search(SearchTerm $search);
}
public class SearchTerm {
private $keyword;
private $input;
// .. constructor mit $keyword & $input
// getter für keyword+searchTerm
}
public class SearchResult {
// was immer du alles brauchst für die ergebnisliste
}
<?php
// Phase1
/** @var $searcher AbstractWatsonSearcher[] */
$searchers = rex_register_extension_point('WATSON_SEARCHER');
// Phase2
// User Eingabe parsen in $keyword & $input
$searchTerm = new SearchTerm($keyword, $input);
// Eingabe an vorher registrierte Search übergeben und Ergebnisse einsammeln
/** @var $searchResults SearchResult[] */
$searchResults = array();
foreach($searchers as $searcher) {
if (in_array($searcher->getKeywords(), $searchTerm->getKeyword()) {
$searchResults[] = $searcher->search($searchTerm);
}
}
// irgendwo später Ergebnis rendern
foreach ($searchResults as $searchResult) {
// render json/html whatever
}
<?php
class Searcher2 extends AbstractWatsonSearcher {
public function keywords() {
return array('ep', 'xyz');
}
public function search(SearchTerm $searchTerm) {
$result = new SearchResult();
// suche in einem Array von EPs
// ergebnisse in $result einfügen
return $result;
}
}
<?php
class Searcher1 extends AbstractWatsonSearcher {
public function keywords() {
return array('maggus', 'thomas');
}
public function search(SearchTerm $searchTerm) {
$result = new SearchResult();
// suche via SQL in der DB
// ergebnisse in $result einfügen
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment