1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Advanced Grid for Yii2. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-higrid |
6
|
|
|
* @package yii2-higrid |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\higrid\representations; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
use yii\base\InvalidConfigException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class RepresentationCollection represents a collection of [[Representation]] objects |
18
|
|
|
* and provides API to access them. |
19
|
|
|
* |
20
|
|
|
* @author Dmytro Naumenko <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class RepresentationCollection implements RepresentationCollectionInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* RepresentationCollection constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->fillRepresentations(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $representations = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @void |
39
|
|
|
*/ |
40
|
|
|
protected function fillRepresentations() |
41
|
|
|
{ |
42
|
|
|
$this->representations = []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function getAll() |
49
|
|
|
{ |
50
|
|
|
foreach ($this->representations as $name => $representation) { |
51
|
|
|
if (!is_object($representation)) { |
52
|
|
|
$representation = $this->representations[$name] = Yii::createObject(array_merge([ |
53
|
|
|
'class' => Representation::class, |
54
|
|
|
], $representation)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!$representation instanceof RepresentationInterface) { |
58
|
|
|
throw new InvalidConfigException('Representation "' . $name . '" must be instance of RepresentationInterface'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$representation->isVisible()) { |
62
|
|
|
unset($this->representations[$name]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->representations; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getByName($name) |
73
|
|
|
{ |
74
|
|
|
if (!$this->exists($name)) { |
75
|
|
|
return $this->getDefault(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->getAll()[$name]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getDefault() |
85
|
|
|
{ |
86
|
|
|
$representations = $this->getAll(); |
87
|
|
|
if (count($representations) === 0) { |
88
|
|
|
throw new InvalidConfigException('Default can not be applied because collection is empty.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return reset($representations); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function exists($name) |
98
|
|
|
{ |
99
|
|
|
return isset($this->getAll()[$name]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|