Completed
Push — master ( c8a423...a23ea0 )
by Song
02:21
created

src/Grid/Filter/Between.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Grid\Filter;
4
5
use Encore\Admin\Admin;
6
use Illuminate\Support\Arr;
7
8
class Between extends AbstractFilter
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    protected $view = 'admin::filter.between';
14
15
    /**
16
     * Format id.
17
     *
18
     * @param string $column
19
     *
20
     * @return array|string
21
     */
22
    public function formatId($column)
23
    {
24
        $id = str_replace('.', '_', $column);
25
26
        return ['start' => "{$id}_start", 'end' => "{$id}_end"];
27
    }
28
29
    /**
30
     * Format two field names of this filter.
31
     *
32
     * @param string $column
33
     *
34
     * @return array
35
     */
36
    protected function formatName($column)
37
    {
38
        $columns = explode('.', $column);
39
40 View Code Duplication
        if (count($columns) == 1) {
41
            $name = $columns[0];
42
        } else {
43
            $name = array_shift($columns);
44
45
            foreach ($columns as $column) {
46
                $name .= "[$column]";
47
            }
48
        }
49
50
        return ['start' => "{$name}[start]", 'end' => "{$name}[end]"];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('start' => ...nd' => "{$name}[end]"); (array) is incompatible with the return type of the parent method Encore\Admin\Grid\Filter...tractFilter::formatName of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
    }
52
53
    /**
54
     * Get condition of this filter.
55
     *
56
     * @param array $inputs
57
     *
58
     * @return mixed
59
     */
60
    public function condition($inputs)
61
    {
62
        if (!Arr::has($inputs, $this->column)) {
63
            return;
64
        }
65
66
        $this->value = Arr::get($inputs, $this->column);
67
68
        $value = array_filter($this->value, function ($val) {
69
            return $val !== '';
70
        });
71
72
        if (empty($value)) {
73
            return;
74
        }
75
76
        if (!isset($value['start'])) {
77
            return $this->buildCondition($this->column, '<=', $value['end']);
78
        }
79
80
        if (!isset($value['end'])) {
81
            return $this->buildCondition($this->column, '>=', $value['start']);
82
        }
83
84
        $this->query = 'whereBetween';
85
86
        return $this->buildCondition($this->column, $this->value);
87
    }
88
89
    /**
90
     * @param array $options
91
     *
92
     * @return $this
93
     */
94
    public function datetime($options = [])
95
    {
96
        $this->view = 'admin::filter.betweenDatetime';
97
98
        $this->setupDatetime($options);
99
100
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Encore\Admin\Grid\Filter\Between) is incompatible with the return type of the parent method Encore\Admin\Grid\Filter\AbstractFilter::datetime of type Encore\Admin\Grid\Filter\Presenter\DateTime.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
101
    }
102
103
    /**
104
     * @param array $options
105
     */
106
    protected function setupDatetime($options = [])
107
    {
108
        $options['format'] = Arr::get($options, 'format', 'YYYY-MM-DD HH:mm:ss');
109
        $options['locale'] = Arr::get($options, 'locale', config('app.locale'));
110
111
        $startOptions = json_encode($options);
112
        $endOptions = json_encode($options + ['useCurrent' => false]);
113
114
        $script = <<<EOT
115
            $('#{$this->id['start']}').datetimepicker($startOptions);
116
            $('#{$this->id['end']}').datetimepicker($endOptions);
117
            $("#{$this->id['start']}").on("dp.change", function (e) {
118
                $('#{$this->id['end']}').data("DateTimePicker").minDate(e.date);
119
            });
120
            $("#{$this->id['end']}").on("dp.change", function (e) {
121
                $('#{$this->id['start']}').data("DateTimePicker").maxDate(e.date);
122
            });
123
EOT;
124
125
        Admin::script($script);
126
    }
127
}
128