Completed
Push — cache-closure ( 7d9053...380edd )
by Dmitry
35:54
created

ListView::renderBeforeItem()   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
     * Note: If the function returns `null`, nothing will be rendered before the item.
92
     * @see renderBeforeItem
93
     * @since 2.0.11
94
     */
95
    public $beforeItem;
96
    /**
97
     * @var Closure an anonymous function that is called once AFTER rendering each data model.
98
     *
99
     * It should have the same signature as [[beforeItem]].
100
     *
101
     * The return result of the function will be rendered directly.
102
     * Note: If the function returns `null`, nothing will be rendered after the item.
103
     * @see renderAfterItem
104
     * @since 2.0.11
105
     */
106
    public $afterItem;
107
108
    /**
109
     * Renders all data models.
110
     * @return string the rendering result
111
     */
112 9
    public function renderItems()
113
    {
114 9
        $models = $this->dataProvider->getModels();
115 9
        $keys = $this->dataProvider->getKeys();
116 9
        $rows = [];
117 9
        foreach (array_values($models) as $index => $model) {
118 8
            $key = $keys[$index];
119 8
            if (($before = $this->renderBeforeItem($model, $key, $index)) !== null) {
120 1
                $rows[] = $before;
121 1
            }
122
123 8
            $rows[] = $this->renderItem($model, $key, $index);
124
125 8
            if (($after = $this->renderAfterItem($model, $key, $index)) !== null) {
126 1
                $rows[] = $after;
127 1
            }
128 9
        }
129
130 9
        return implode($this->separator, $rows);
131
    }
132
133
    /**
134
     * Calls [[beforeItem]] closure, returns execution result.
135
     * If [[beforeItem]] is not a closure, `null` will be returned.
136
     *
137
     * @param mixed $model the data model to be rendered
138
     * @param mixed $key the key value associated with the data model
139
     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
140
     * @return string|null [[beforeItem]] call result or `null` when [[beforeItem]] is not a closure
141
     * @see beforeItem
142
     * @since 2.0.11
143
     */
144 8
    protected function renderBeforeItem($model, $key, $index)
145
    {
146 8
        if ($this->beforeItem instanceof Closure) {
147 1
            return call_user_func($this->beforeItem, $model, $key, $index, $this);
148
        }
149
150 7
        return null;
151
    }
152
153
    /**
154
     * Calls [[afterItem]] closure, returns execution result.
155
     * If [[afterItem]] is not a closure, `null` will be returned.
156
     *
157
     * @param mixed $model the data model to be rendered
158
     * @param mixed $key the key value associated with the data model
159
     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
160
     * @return string|null [[afterItem]] call result or `null` when [[afterItem]] is not a closure
161
     * @see afterItem
162
     * @since 2.0.11
163
     */
164 8
    protected function renderAfterItem($model, $key, $index)
165
    {
166 8
        if ($this->afterItem instanceof Closure) {
167 1
            return call_user_func($this->afterItem, $model, $key, $index, $this);
168
        }
169
170 7
        return null;
171
    }
172
173
    /**
174
     * Renders a single data model.
175
     * @param mixed $model the data model to be rendered
176
     * @param mixed $key the key value associated with the data model
177
     * @param int $index the zero-based index of the data model in the model array returned by [[dataProvider]].
178
     * @return string the rendering result
179
     */
180 8
    public function renderItem($model, $key, $index)
181
    {
182 8
        if ($this->itemView === null) {
183 6
            $content = $key;
184 8
        } elseif (is_string($this->itemView)) {
185 1
            $content = $this->getView()->render($this->itemView, array_merge([
186 1
                'model' => $model,
187 1
                'key' => $key,
188 1
                'index' => $index,
189 1
                'widget' => $this,
190 1
            ], $this->viewParams));
191 1
        } else {
192 1
            $content = call_user_func($this->itemView, $model, $key, $index, $this);
193
        }
194 8
        if ($this->itemOptions instanceof Closure) {
195 1
            $options = call_user_func($this->itemOptions, $model, $key, $index, $this);
196 1
        } else {
197 7
            $options = $this->itemOptions;
198
        }
199 8
        $tag = ArrayHelper::remove($options, 'tag', 'div');
200 8
        $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
201
202 8
        return Html::tag($tag, $content, $options);
203
    }
204
}
205