WhereBetweenSplitter::isArrayValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Sfneal\Builders\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sfneal\Helpers\Strings\StringHelpers;
7
8
trait WhereBetweenSplitter
9
{
10
    /**
11
     * whereBetween clause extension that accepts a string value instead of array.
12
     *
13
     * @param  string  $column  DB column
14
     * @param  string|mixed  $values  Two 'between' values with a separator
15
     * @param  string  $sep  separator character
16
     * @param  bool  $allow_where  Allow for standard 'where' clause $values cannot be split
17
     * @param  string  $where_operator
18
     * @return WhereBetweenSplitter|Builder
19
     */
20
    public function whereBetweenSplitter(string $column, $values, string $sep = '-', bool $allow_where = true, $where_operator = '=')
21
    {
22
        // Use standard whereBetween if $values is an array
23
        if (self::isArrayValue($values)) {
24
            $this->whereBetween($column, $values);
0 ignored issues
show
Bug introduced by
The method whereBetween() does not exist on Sfneal\Builders\Traits\WhereBetweenSplitter. Did you maybe mean whereBetweenSplitter()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            $this->/** @scrutinizer ignore-call */ 
25
                   whereBetween($column, $values);

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.

Loading history...
25
        }
26
27
        // Determine if the $value is splitable
28
        elseif (! self::isSplitableValue($values, $sep) && $allow_where) {
29
            $this->where($column, $where_operator, $values);
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            $this->/** @scrutinizer ignore-call */ 
30
                   where($column, $where_operator, $values);
Loading history...
30
        }
31
32
        // $values is a string and that $sep is in $values
33
        else {
34
            $this->whereBetween($column, array_map(function ($str) {
35
                return trim($str);
36
            }, explode($sep, $values)));
37
        }
38
39
        return $this;
40
    }
41
42
    /**
43
     * Determine if the values to be split are 'splitable'.
44
     *
45
     * Value is splittable if...
46
     *  - A standard 'where' clause can be used
47
     *  - $values is not a string
48
     *  - $sep is found not in $values
49
     *
50
     * @param  $values
51
     * @param  string  $sep
52
     * @return bool
53
     */
54
    private static function isSplitableValue($values, string $sep = '-'): bool
55
    {
56
        return is_string($values) && (new StringHelpers($values))->inString($sep);
57
    }
58
59
    /**
60
     * Determine if the values are valid for use in standard 'whereBetween' clause.
61
     *
62
     * @param  $values
63
     * @return bool
64
     */
65
    private static function isArrayValue($values): bool
66
    {
67
        return is_array($values) && count($values) > 1;
68
    }
69
}
70