completed functionality and docs update

This commit is contained in:
glmdev
2016-12-30 23:05:11 +00:00
parent 529f22c4b9
commit 68165c948a
8 changed files with 154 additions and 179 deletions

View File

@@ -6,22 +6,39 @@
* Time: 6:27 PM
*/
namespace Element\Search;
namespace Glmdev\Search;
class Search {
/**
* Searches the collection of given $model type for
* Searches the collection of given $modelClass type for
* $string and returns the results.
*
* @param SearchableContract $model
* @param string $modelClass
* @param $string
* @return \Illuminate\Support\Collection
*/
public function search( SearchableContract $model, $string ){
/* @var $model \Illuminate\Database\Eloquent\Model */
$query = $this->formatQuery( $string );
public static function search( $modelClass, $string ){
// define the model container in the correct scope
$model = null;
// check if the provided class name is searchable
if ( new $modelClass() instanceof SearchableContract ){
// set the model
$model = new $modelClass();
}
else {
throw new \Exception('Cannot attempt to search non-searchable class.');
return;
}
// sanitize and format the query
$query = self::formatQuery( $string );
// get all the models
$models = $model->all();
// initialize working arrays
$returns = [];
$toOpt = [];
$optHits = [];
@@ -129,7 +146,7 @@ class Search {
* @param $string
* @return array
*/
public function formatQuery( $string ){
public static function formatQuery( $string ){
$words = explode(' ', $string);
$return = [];
foreach ( $words as $word ){