1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Builders\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
|
7
|
|
|
trait WhereBetweenSplitter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* whereBetween clause extension that accepts a string value instead of array. |
11
|
|
|
* |
12
|
|
|
* @param string $column DB column |
13
|
|
|
* @param string|mixed $values Two 'between' values with a separator |
14
|
|
|
* @param string $sep separator character |
15
|
|
|
* @param bool $allow_where Allow for standard 'where' clause $values cannot be split |
16
|
|
|
* @param string $where_operator |
17
|
|
|
* @return WhereBetweenSplitter|Builder |
18
|
|
|
*/ |
19
|
|
|
public function whereBetweenSplitter(string $column, $values, string $sep = '-', bool $allow_where = true, $where_operator = '=') |
20
|
|
|
{ |
21
|
|
|
// Use standard whereBetween if $values is an array |
22
|
|
|
if (self::isArrayValue($values)) { |
23
|
|
|
$this->whereBetween($column, $values); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Determine if the $value is splitable |
27
|
|
|
elseif (! self::isSplitableValue($values, $sep) && $allow_where) { |
28
|
|
|
$this->where($column, $where_operator, $values); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// $values is a string and that $sep is in $values |
32
|
|
|
else { |
33
|
|
|
$this->whereBetween($column, array_map(function ($str) { |
34
|
|
|
return trim($str); |
35
|
|
|
}, explode($sep, $values))); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Determine if the values to be split are 'splitable'. |
43
|
|
|
* |
44
|
|
|
* Value is splittable if... |
45
|
|
|
* - A standard 'where' clause can be used |
46
|
|
|
* - $values is not a string |
47
|
|
|
* - $sep is found not in $values |
48
|
|
|
* |
49
|
|
|
* @param $values |
50
|
|
|
* @param string $sep |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
private static function isSplitableValue($values, string $sep = '-') |
54
|
|
|
{ |
55
|
|
|
return is_string($values) && inString($values, $sep); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Determine if the values are valid for use in standard 'whereBetween' clause. |
60
|
|
|
* |
61
|
|
|
* @param $values |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
private static function isArrayValue($values) |
65
|
|
|
{ |
66
|
|
|
return is_array($values) && count($values) > 1; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.