completed functionality and docs update
This commit is contained in:
@@ -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 ){
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: 91373
|
||||
* Date: 7/8/2016
|
||||
* Time: 6:31 PM
|
||||
*/
|
||||
|
||||
namespace Element\Search;
|
||||
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class SearchFacade extends Facade
|
||||
{
|
||||
public static function getFacadeAccessor()
|
||||
{
|
||||
return 'e-search';
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Element\Search;
|
||||
|
||||
namespace Glmdev\Search;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class SearchServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(){
|
||||
|
||||
}
|
||||
|
||||
public function boot(){}
|
||||
|
||||
/**
|
||||
* Register the service and alias the Search class.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(){
|
||||
|
||||
$this->app->bind('e-search', function(){
|
||||
return new Search;
|
||||
});
|
||||
|
||||
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
|
||||
$loader->alias('Search', 'Element\Search\SearchFacade');
|
||||
$loader->alias('Search', 'Glmdev\Search\Search');
|
||||
|
||||
}
|
||||
}
|
||||
28
src/SearchableAgreement.php
Normal file
28
src/SearchableAgreement.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Glmdev\Search;
|
||||
|
||||
trait SearchableAgreement {
|
||||
|
||||
/**
|
||||
* returns the searchable fields from the protected $searchable array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getSearchable(){
|
||||
$inst = new self();
|
||||
return $inst->searchable;
|
||||
}
|
||||
|
||||
/**
|
||||
* searches the model for the query and returns the results
|
||||
*
|
||||
* @param string $query
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public static function search( $query ){
|
||||
return \Search::search( self::class, $query );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Element\Search;
|
||||
namespace Glmdev\Search;
|
||||
|
||||
interface SearchableContract {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user