Completed
Push — master ( bd5521...014832 )
by Dmitry
10:52
created

ListView::renderAfterItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\widgets;
9
10
use Closure;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Html;
13
14
/**
15
 * The ListView widget is used to display data from data
16
 * provider. Each data model is rendered using the view
17
 * specified.
18
 *
19
 * For more details and usage information on ListView, see the [guide article on data widgets](guide:output-data-widgets).
20
 *
21
 * @author Qiang Xue <[email protected]>
22
 * @since 2.0
23
 */
24
class ListView extends BaseListView
25
{
26
    /**
27
     * @var array|Closure the HTML attributes for the container of the rendering result of each data model.
28
     * This can be either an array specifying the common HTML attributes for rendering each data item,
29
     * or an anonymous function that returns an array of the HTML attributes. The anonymous function will be
30
     * called once for every data model returned by [[dataProvider]].
31
     * The "tag" element specifies the tag name of the container element and defaults to "div".
32
     * If "tag" is false, it means no container element will be rendered.
33
     *
34
     * If this property is specified as an anonymous function, it should have the following signature:
35
     *
36
     * ```php
37
     * function ($model, $key, $index, $widget)
38
     * ```
39
     *
40
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
41
     */
42
    public $itemOptions = [];
43
    /**
44
     * @var string|callable the name of the view for rendering each data item, or a callback (e.g. an anonymous function)
45
     * for rendering each data item. If it specifies a view name, the following variables will
46
     * be available in the view:
47
     *
48
     * - `$model`: mixed, the data model
49
     * - `$key`: mixed, the key value associated with the data item
50
     * - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
51
     * - `$widget`: ListView, this widget instance
52
     *
53
     * Note that the view name is resolved into the view file by the current context of the [[view]] object.
54
     *
55
     * If this property is specified as a callback, it should have the following signature:
56
     *
57
     * ```php
58
     * function ($model, $key, $index, $widget)
59
     * ```
60
     */
61
    public $itemView;
62
    /**
63
     * @var array additional parameters to be passed to [[itemView]] when it is being rendered.
64
     * This property is used only when [[itemView]] is a string representing a view name.
65
     */
66
    public $viewParams = [];
67
    /**
68
     * @var string the HTML code to be displayed between any two consecutive items.
69
     */
70
    public $separator = "\n";
71
    /**
72
     * @var array the HTML attributes for the container tag of the list view.
73
     * The "tag" element specifies the tag name of the container element and defaults to "div".
74
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
75
     */
76
    public $options = ['class' => 'list-view'];
77
    /**
78
     * @var Closure an anonymous function that is called once BEFORE rendering each data model.
79
     * It should have the following signature:
80
     *
81
     * ```php
82
     * function ($model, $key, $index, $widget)
83
     * ```
84
     *
85
     * - `$model`: the current data model being rendered
86
     * - `$key`: the key value associated with the current data model
87
     * - `$index`: the zero-based index of the data model in the model array returned by [[dataProvider]]
88
     * - `$widget`: the ListView object
89
     *
90
     * The return result of the function will be rendered directly.
91
     * @see renderBeforeItem
92
     * @since 2.0.11
93
     */
94
    public $beforeItem;
95
    /**
96
     * @var Closure an anonymous function that is called once AFTER rendering each data model.
97
     * It should have the similar signature as [[beforeItem]]. The return result of the function
98
     * will be rendered directly.
99
     * @see renderAfterItem
100
     * @since 2.0.11
101
     */
102
    public $afterItem;
103
104
    /**
105
     * Renders all data models.
106
     * @return string the rendering result
107
     */
108 9
    public function renderItems()
109
    {
110 9
        $models = $this->dataProvider->getModels();
111 9
        $keys = $this->dataProvider->getKeys();
112 9
        $rows = [];
113 9
        foreach (array_values($models) as $index => $model) {
114 8
            $key = $keys[$index];
115 8
            if (($before = $this->renderBeforeItem($model, $key, $index)) !== null) {
116 1
                $rows[] = $before;
117 1
            }
118
119 8
            $rows[] = $this->renderItem($model, $key, $index);
120
121 8
            if (($after = $this->renderAfterItem($model, $key, $index)) !== null) {
122 1
                $rows[] = $after;
123 1
            }
124 9
        }
125
126 9
        return implode($this->separator, $rows);
127
    }
128
129
    /**
130
     * Calls [[beforeItem]] closure, returns execution result.
131
     * If [[beforeItem]] is not a closure, `null` will be returned.
132
     *
133
     * @param mixed $model the data model to be rendered
134
     * @param mixed $key the key value associated with the data model
135
     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
136
     * @return string|null [[beforeItem]] call result or `null` when [[beforeItem]] is not a closure
137
     * @see beforeItem
138
     * @since 2.0.11
139
     */
140 8
    protected function renderBeforeItem($model, $key, $index)
141
    {
142 8
        if ($this->beforeItem instanceof Closure) {
143 1
            return call_user_func($this->beforeItem, $model, $key, $index, $this);
144
        }
145
146 7
        return null;
147
    }
148
149
    /**
150
     * Calls [[afterItem]] closure, returns execution result.
151
     * If [[afterItem]] is not a closure, `null` will be returned.
152
     *
153
     * @param mixed $model the data model to be rendered
154
     * @param mixed $key the key value associated with the data model
155
     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
156
     * @return string|null [[afterItem]] call result or `null` when [[afterItem]] is not a closure
157
     * @see afterItem
158
     * @since 2.0.11
159
     */
160 8
    protected function renderAfterItem($model, $key, $index)
161
    {
162 8
        if ($this->afterItem instanceof Closure) {
163 1
            return call_user_func($this->afterItem, $model, $key, $index, $this);
164
        }
165
166 7
        return null;
167
    }
168
169
    /**
170
     * Renders a single data model.
171
     * @param mixed $model the data model to be rendered
172
     * @param mixed $key the key value associated with the data model
173
     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
174
     * @return string the rendering result
175
     */
176 8
    public function renderItem($model, $key, $index)
177
    {
178 8
        if ($this->itemView === null) {
179 6
            $content = $key;
180 8
        } elseif (is_string($this->itemView)) {
181 1
            $content = $this->getView()->render($this->itemView, array_merge([
182 1
                'model' => $model,
183 1
                'key' => $key,
184 1
                'index' => $index,
185 1
                'widget' => $this,
186 1
            ], $this->viewParams));
187 1
        } else {
188 1
            $content = call_user_func($this->itemView, $model, $key, $index, $this);
189
        }
190 8
        if ($this->itemOptions instanceof Closure) {
191 1
            $options = call_user_func($this->itemOptions, $model, $key, $index, $this);
192 1
        } else {
193 7
            $options = $this->itemOptions;
194
        }
195 8
        $tag = ArrayHelper::remove($options, 'tag', 'div');
196 8
        $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
197
198 8
        return Html::tag($tag, $content, $options);
199
    }
200
}
201