Last active
December 30, 2019 08:29
-
-
Save dusta/df9eb40c48004faced8d34cce98ab0e9 to your computer and use it in GitHub Desktop.
Dframe/Database SearchBuilderWhereChunk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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