Skip to content

Instantly share code, notes, and snippets.

@dusta
Last active December 30, 2019 08:29
Show Gist options
  • Save dusta/df9eb40c48004faced8d34cce98ab0e9 to your computer and use it in GitHub Desktop.
Save dusta/df9eb40c48004faced8d34cce98ab0e9 to your computer and use it in GitHub Desktop.
Dframe/Database SearchBuilderWhereChunk
<?php
class Search
{
/**
* @var array
*/
public $search = [];
/**
* Search constructor.
*
* @param $search
*/
public function __construct($search)
{
$inArray = ['type'];
foreach ($search as $key => $value) {
if (in_array($key, $inArray)) {
$this->build($key, $this->$key($value));
}
}
}
/**
* @return array
*/
public function getWhere()
{
$where = [];
foreach ($this->search as $type => $params) {
$sql = null;
$arr = [];
foreach ($params['value'] as $key => $condition) {
$sql .= $params['column'] . ' ' . $condition['op'] . ' ? OR ';
$arr[] = $condition['value'];
}
if (!empty($arr)) {
$sql = rtrim($sql, ' OR ');
$where[] = new WhereStringChunk($sql, $arr);
}
}
return $where;
}
/**
* @param $search
*
* @return array
*/
protected function type($search)
{
$data = [
'column' => 'type',
'value' => []
];
$values = ['a', 'b'];
foreach ($search as $key => $value) {
if (in_array($value, $values)) {
$data['value'][] = ['op' => '=', 'value' => $value];
}
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment