GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bd5521...7c9800 )
by Robert
40:43
created

ListView::renderBeforeItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
eloc 4
nc 2
nop 3
crap 6
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 8
     *
85
     * - `$model`: the current data model being rendered
86 8
     * - `$key`: the key value associated with the current data model
87 8
     * - `$index`: the zero-based index of the data model in the model array returned by [[dataProvider]]
88 8
     * - `$widget`: the ListView object
89 8
     *
90 7
     * The return result of the function will be rendered directly.
91 8
     * Note: If the function returns `null`, nothing will be rendered before the item.
92
     * @see renderBeforeItem
93 8
     * @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 7
     * @see renderAfterItem
104
     * @since 2.0.11
105 7
     */
106 5
    public $afterItem;
107 7
108 1
    /**
109 1
     * Renders all data models.
110 1
     * @return string the rendering result
111 1
     */
112 1
    public function renderItems()
113 1
    {
114 1
        $models = $this->dataProvider->getModels();
115 1
        $keys = $this->dataProvider->getKeys();
116
        $rows = [];
117 7
        foreach (array_values($models) as $index => $model) {
118 1
            $key = $keys[$index];
119 1
            if (($before = $this->renderBeforeItem($model, $key, $index)) !== null) {
120 6
                $rows[] = $before;
121
            }
122 7
123 7
            $rows[] = $this->renderItem($model, $key, $index);
124
125 7
            if (($after = $this->renderAfterItem($model, $key, $index)) !== null) {
126
                $rows[] = $after;
127
            }
128
        }
129
130
        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
    protected function renderBeforeItem($model, $key, $index)
145
    {
146
        if ($this->beforeItem instanceof Closure) {
147
            return call_user_func($this->beforeItem, $model, $key, $index, $this);
148
        }
149
150
        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
    protected function renderAfterItem($model, $key, $index)
165
    {
166
        if ($this->afterItem instanceof Closure) {
167
            return call_user_func($this->afterItem, $model, $key, $index, $this);
168
        }
169
170
        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
    public function renderItem($model, $key, $index)
181
    {
182
        if ($this->itemView === null) {
183
            $content = $key;
184
        } elseif (is_string($this->itemView)) {
185
            $content = $this->getView()->render($this->itemView, array_merge([
186
                'model' => $model,
187
                'key' => $key,
188
                'index' => $index,
189
                'widget' => $this,
190
            ], $this->viewParams));
191
        } else {
192
            $content = call_user_func($this->itemView, $model, $key, $index, $this);
193
        }
194
        if ($this->itemOptions instanceof Closure) {
195
            $options = call_user_func($this->itemOptions, $model, $key, $index, $this);
196
        } else {
197
            $options = $this->itemOptions;
198
        }
199
        $tag = ArrayHelper::remove($options, 'tag', 'div');
200
        $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
201
202
        return Html::tag($tag, $content, $options);
203
    }
204
}
205