<?php
namespace App\Spotlight; abstract class SourceAbstract { protected $searchString; protected $searchWords = []; protected $lastSearchTotalCount = 0; protected $extractor = null; public function __construct() { $this->extractor = new \App\Utils\BaseParser\Utils\Extractor\SingleOptimized(); } public function setSearchString($searchString) { $this->searchString = $searchString; $this->searchWords = preg_split("/[\s]+/", $searchString); } protected function matchesSearchString($label) { if (!$this->searchWords) return false; foreach($this->searchWords as $word) { if (mb_stripos($label, $word)===false) { return false; } } return true; } public function getRecords($offset, $resultsPerPage) { $records = $this->determineRecords($offset, $resultsPerPage); foreach($records as $record) { if (!($record instanceof Record)) { throw new \Exception('The output of `'.__METHOD__.'` must be an array of `Record` class.'); } } return $records; } abstract protected function determineRecords($offset, $resultsPerPage):array; public function shouldStealFocus() { if ($this->searchString===null) { throw new \Exception('You must set the search string before calling '.__METHOD__); } return false; } public function getLastSearchTotalCount() { return $this->lastSearchTotalCount; } } 