patternphplaravelMinor
A Search Engine Class
Viewed 0 times
engineclasssearch
Problem
I've built a Search Engine class for a website that permits to search companies in base of their locations and their categories (companies pay a plan to be found in the places that they will).
In the constructor you can pass an index array cointaining the parameters or you can set them later with setters. All parameters are optional.
Basically calling
Note: I did not designed the structure of the database, I know that it's not the best (comma separated values etc.) but I cannot touch it.
I would like to know if the design of the class is good or not, and possibily learn how to build it properly.
```
class SearchEngine
{
private $region = null;
private $province = null;
private $zipcode = null;
private $municipality = null;
private $category = null;
private $subcategory = null;
private $b2b = false;
/*
*
* Possible $params keys:
* 'region' --> The Region id
* 'province' --> The Province id
* 'zipcode' --> The Zipcode
* 'municipality' --> The Municipality id
* 'category' --> The category code
* 'subcategory' --> The subcategory code
* 'b2b' --> Find only companies with b2b market type
*
* All values MUST be integer or null except 'b2b', that MUST be a boolean
*
* @author xxxxxxx
* @param array $params
*
*
*/
public function __construct($params = []){
if(!is_array($params)){
throw new \InvalidArgumentException("Params MUST be an array");
}
foreach($params as $key => $value){
if($key == 'b2b'){
$this->b2b = boolval($value);
}
else {
$this->{$key} = intval($value);
}
}
}
/**
* @return null or int
*/
public function getRegion()
{
return $this->region;
In the constructor you can pass an index array cointaining the parameters or you can set them later with setters. All parameters are optional.
Basically calling
getQueryBuilder() method you obtain a Laravel's Query Builder instance and can treat it as you want (for example use Pagination etc.)Note: I did not designed the structure of the database, I know that it's not the best (comma separated values etc.) but I cannot touch it.
I would like to know if the design of the class is good or not, and possibily learn how to build it properly.
```
class SearchEngine
{
private $region = null;
private $province = null;
private $zipcode = null;
private $municipality = null;
private $category = null;
private $subcategory = null;
private $b2b = false;
/*
*
* Possible $params keys:
* 'region' --> The Region id
* 'province' --> The Province id
* 'zipcode' --> The Zipcode
* 'municipality' --> The Municipality id
* 'category' --> The category code
* 'subcategory' --> The subcategory code
* 'b2b' --> Find only companies with b2b market type
*
* All values MUST be integer or null except 'b2b', that MUST be a boolean
*
* @author xxxxxxx
* @param array $params
*
*
*/
public function __construct($params = []){
if(!is_array($params)){
throw new \InvalidArgumentException("Params MUST be an array");
}
foreach($params as $key => $value){
if($key == 'b2b'){
$this->b2b = boolval($value);
}
else {
$this->{$key} = intval($value);
}
}
}
/**
* @return null or int
*/
public function getRegion()
{
return $this->region;
Solution
I really like the structure of your code. That said, I almost never suggest to give class variables default values. That is what the constructor is for. For that matter, you don't need to specify that the object's equal
In regards to optional inputs inside of the array, make sure you validate your input. While you are using
Moving further in, I understand the need for a raw query in the
While it is closed as off-topic, this SO Question does give some good resources when looking to implement a PDO connection.
null as they already default to that when created.In regards to optional inputs inside of the array, make sure you validate your input. While you are using
inval and boolval, there's no guarantee that the input is what you need to properly parse the value. If you end up passing a string to intval, you're likely not to get the result intended (either an exception or 0 depending on how you handle it). Same goes for boolval. It should be receiving true, false, 0, or 1.Moving further in, I understand the need for a raw query in the
generateQuery() function, however, it might be smarter to use straight up PDO here, versus the Query Build which calls PDO. My reasoning here is that you are using a raw query statement with an object passed straight into the query. The Laravel Query Builder only protects you from SQL injection if you pass the objects as binds, which can't be done in rawQuery. For that reason, rewriting it as a PDO::PreparedStatement might be smarter as you can then pass a bindings array instead of putting the object right into the SQL.While it is closed as off-topic, this SO Question does give some good resources when looking to implement a PDO connection.
Context
StackExchange Code Review Q#128073, answer score: 6
Revisions (0)
No revisions yet.