Completed
Push — master ( 024746...0b49f1 )
by Song
04:00
created

HasElementNames::getPerPageName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid;
4
5
trait HasElementNames
6
{
7
    /**
8
     * Grid name.
9
     *
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * HTML element names.
16
     *
17
     * @var array
18
     */
19
    protected $elementNames = [
20
        'grid_row'        => 'grid-row',
21
        'grid_select_all' => 'grid-select-all',
22
        'grid_per_page'   => 'grid-per-pager',
23
        'grid_batch'      => 'grid-batch',
24
        'export_selected' => 'export-selected',
25
        'selected_rows'   => 'selectedRows',
26
    ];
27
28
    /**
29
     * Set name to grid.
30
     *
31
     * @param string $name
32
     * @return $this
33
     */
34
    public function setName($name)
35
    {
36
        $this->name = $name;
37
38
        $this->model()->setPerPageName("{$name}_{$this->model()->getPerPageName()}");
0 ignored issues
show
Bug introduced by
It seems like model() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
40
        $this->getFilter()->setName($name);
0 ignored issues
show
Bug introduced by
It seems like getFilter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
42
        return $this;
43
    }
44
45
    /**
46
     * Get name of grid.
47
     *
48
     * @return string
49
     */
50
    public function getName()
51
    {
52
        return $this->name;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getGridRowName()
59
    {
60
        return $this->elementNameWithPrefix('grid_row');
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getSelectAllName()
67
    {
68
        return $this->elementNameWithPrefix('grid_select_all');
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getPerPageName()
75
    {
76
        return $this->elementNameWithPrefix('grid_per_page');
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getGridBatchName()
83
    {
84
        return $this->elementNameWithPrefix('grid_batch');
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getExportSelectedName()
91
    {
92
        return $this->elementNameWithPrefix('export_selected');
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getSelectedRowsName()
99
    {
100
        $elementName = $this->elementNames['selected_rows'];
101
102
        if ($this->name) {
103
            return sprintf("%s%s", $this->name, ucfirst($elementName));
104
        }
105
106
        return $elementName;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    protected function elementNameWithPrefix($name)
113
    {
114
        $elementName = $this->elementNames[$name];
115
116
        if ($this->name) {
117
            return sprintf("%s-%s", $this->name, $elementName);
118
        }
119
120
        return $elementName;
121
    }
122
}
123