Completed
Pull Request — master (#58)
by
unknown
12:29 queued 10:55
created

LinkSorter::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Yiisoft\Widget;
4
5
use Yiisoft\Data\Reader\Sort;
6
use Yiisoft\Factory\Exceptions\InvalidConfigException;
7
use Yiisoft\Html\Html;
8
9
/**
10
 * LinkSorter renders a list of sort links for the given sort definition.
11
 * LinkSorter will generate a hyperlink for every attribute declared in [[sort]].
12
 * For more details and usage information on LinkSorter, see the [guide article on sorting](guide:output-sorting).
13
 * @method static LinkSorter widget()
14
 */
15
class LinkSorter extends Widget
16
{
17
    /**
18
     * @var \Yiisoft\Data\Reader\Sort the sort definition
19
     */
20
    public $sort;
21
    /**
22
     * @var array list of the attributes that support sorting. If not set, it will be determined
23
     *            using [[Sort::attributes]].
24
     */
25
    public $attributes;
26
    /**
27
     * @var array HTML attributes for the sorter container tag.
28
     *
29
     * @see Html::ul() for special attributes.
30
     * @see Html::renderTagAttributes() for details on how attributes are being rendered.
31
     */
32
    public $options = ['class' => 'sorter'];
33
    /**
34
     * @var array HTML attributes for the link in a sorter container tag which are passed to [[Sort::link()]].
35
     *
36
     * @see Html::renderTagAttributes() for details on how attributes are being rendered.
37
     */
38
    public $linkOptions = [];
39
40
    /**
41
     * Initializes the sorter.
42
     */
43
    public function init(): void
44
    {
45
        parent::init();
46
47
        if ($this->sort === null) {
48
            throw new InvalidConfigException('The "sort" property must be set.');
49
        }
50
    }
51
52 2
    public function sort(Sort $sort): self
53
    {
54 2
        $this->sort = $sort;
55
56 2
        return $this;
57
    }
58
59 1
    public function attributes(array $attributes): self
60
    {
61 1
        $this->attributes = $attributes;
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * Executes the widget.
68
     * This method renders the sort links.
69
     *
70
     * @return string the result of widget execution to be outputted.
71
     */
72 2
    public function run(): string
73
    {
74 2
        return $this->renderSortLinks();
75
    }
76
77
    /**
78
     * Renders the sort links.
79
     *
80
     * @return string the rendering result
81
     */
82 2
    protected function renderSortLinks(): string
83
    {
84 2
        $attributes = empty($this->attributes) ? array_keys($this->sort->getCriteria()) : $this->attributes;
85 2
        $links = [];
86 2
        foreach ($attributes as $name) {
87 1
            $links[] = Html::a($name, sprintf('?sort=%s', $name), $this->linkOptions);
88
        }
89
90 2
        return Html::ul($links, array_merge($this->options, ['encode' => false]));
91
    }
92
}
93