Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for SphinxQL escapes and helpers #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions src/mnshankar/Sphinxql/Sphinxql.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
namespace mnshankar\Sphinxql;
class Sphinxql
{
protected $library;
protected $library;
protected $hits;
public function __construct(\Foolz\SphinxQL\SphinxQL $library)
{
$this->library = $library;
$this->library = $library;
}
/**
* return the library SphinxQL object for chaining other calls
* @return \Foolz\SphinxQL\SphinxQL
*/
public function query()
{
return $this->library->forge($this->library->getConnection());
return $this->library->forge($this->library->getConnection());
}
/**
* return the library SphinxQL helper object
* @return \Foolz\SphinxQL\Helper
*/
public function helper()
{
return \Foolz\SphinxQL\Helper::create($this->library->getConnection());
}
/**
* set the hits array
* @param array $hits - the array returned by executing the SphinxQL
* @param array $hits - the array returned by executing the SphinxQL
* @return \mnshankar\Sphinxql\Sphinxql
*/
public function with($hits)
Expand All @@ -27,36 +35,54 @@ public function with($hits)
return $this;
}
/**
* if name is null, return id's
* if name is null, return id's
* if name is class (model) return model->get()
* if name is table return table->get()
* @param string $name
* @param string $key, column name that maps to matched id returned by sphinx
* @return mixed (either array or eloquentcollection)
*/
public function get($name=null, $key='id')
{
$matchids = array_pluck($this->hits, $key);
{
$matchids = array_pluck($this->hits, $key);
if ($name===null)
{
return $matchids;
}
}
if (class_exists($name))
{
$result = call_user_func_array($name . "::whereIn", array($key, $matchids))->get();
{
$result = call_user_func_array($name . "::whereIn", array($key, $matchids))->get();
}
else
else
{
$result = \DB::table($name)->whereIn($key, $matchids)->get();
}
}
return $result;
}
}
/**
* Execute raw query against the sphinx server
* Execute raw query against the sphinx server
* @param string $query
*/
public function raw( $query )
{
return $this->library->getConnection()->query($query);
return $this->library->getConnection()->query($query);
}
/**
* avoids having the expressions escaped
* @param string $string the string to keep unaltered
* @return \Foolz\SphinxQL\Expression the new Expression
*/
public static function expr($string)
{
return \Foolz\SphinxQL\SphinxQL::expr($string);
}
/**
* escapes the input with \MySQLi::real_escape_string
* @param string $value the string to escape
* @return string the escaped string
*/
public function escape($value)
{
return $this->library->getConnection()->escape($value);
}
}
}