Completed
Push — 2.1 ( c952e8...98ed49 )
by Carsten
10:00
created

ListView   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 89
rs 10
c 0
b 0
f 0
ccs 0
cts 24
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A renderItems() 0 11 2
A renderItem() 0 20 4
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 Yii;
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
 * @author Qiang Xue <[email protected]>
20
 * @since 2.0
21
 */
22
class ListView extends BaseListView
23
{
24
    /**
25
     * @var array the HTML attributes for the container of the rendering result of each data model.
26
     * The "tag" element specifies the tag name of the container element and defaults to "div".
27
     * If "tag" is false, it means no container element will be rendered.
28
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
29
     */
30
    public $itemOptions = [];
31
    /**
32
     * @var string|callable the name of the view for rendering each data item, or a callback (e.g. an anonymous function)
33
     * for rendering each data item. If it specifies a view name, the following variables will
34
     * be available in the view:
35
     *
36
     * - `$model`: mixed, the data model
37
     * - `$key`: mixed, the key value associated with the data item
38
     * - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
39
     * - `$widget`: ListView, this widget instance
40
     *
41
     * Note that the view name is resolved into the view file by the current context of the [[view]] object.
42
     *
43
     * If this property is specified as a callback, it should have the following signature:
44
     *
45
     * ```php
46
     * function ($model, $key, $index, $widget)
47
     * ```
48
     */
49
    public $itemView;
50
    /**
51
     * @var array additional parameters to be passed to [[itemView]] when it is being rendered.
52
     * This property is used only when [[itemView]] is a string representing a view name.
53
     */
54
    public $viewParams = [];
55
    /**
56
     * @var string the HTML code to be displayed between any two consecutive items.
57
     */
58
    public $separator = "\n";
59
    /**
60
     * @var array the HTML attributes for the container tag of the list view.
61
     * The "tag" element specifies the tag name of the container element and defaults to "div".
62
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
63
     */
64
    public $options = ['class' => 'list-view'];
65
66
67
    /**
68
     * Renders all data models.
69
     * @return string the rendering result
70
     */
71
    public function renderItems()
72
    {
73
        $models = $this->dataProvider->getModels();
74
        $keys = $this->dataProvider->getKeys();
75
        $rows = [];
76
        foreach (array_values($models) as $index => $model) {
77
            $rows[] = $this->renderItem($model, $keys[$index], $index);
78
        }
79
80
        return implode($this->separator, $rows);
81
    }
82
83
    /**
84
     * Renders a single data model.
85
     * @param mixed $model the data model to be rendered
86
     * @param mixed $key the key value associated with the data model
87
     * @param integer $index the zero-based index of the data model in the model array returned by [[dataProvider]].
88
     * @return string the rendering result
89
     */
90
    public function renderItem($model, $key, $index)
91
    {
92
        if ($this->itemView === null) {
93
            $content = $key;
94
        } elseif (is_string($this->itemView)) {
95
            $content = $this->getView()->render($this->itemView, array_merge([
96
                'model' => $model,
97
                'key' => $key,
98
                'index' => $index,
99
                'widget' => $this,
100
            ], $this->viewParams));
101
        } else {
102
            $content = call_user_func($this->itemView, $model, $key, $index, $this);
103
        }
104
        $options = $this->itemOptions;
105
        $tag = ArrayHelper::remove($options, 'tag', 'div');
106
        $options['data-key'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : (string) $key;
107
108
        return Html::tag($tag, $content, $options);
109
    }
110
}
111