1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Layout\Argument\Interpreter;
use Magento\Framework\Data\Argument\InterpreterInterface;
use Magento\Framework\ObjectManagerInterface;
/**
* Interpreter that retrieves options from an option source model
*/
class Options implements InterpreterInterface
{
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* {@inheritdoc}
* @return array Format: array(array('value' => <value>, 'label' => '<label>'), ...)
* @throws \InvalidArgumentException
* @throws \UnexpectedValueException
*/
public function evaluate(array $data)
{
if (!isset($data['model'])) {
throw new \InvalidArgumentException('Options source model class is missing.');
}
$modelClass = $data['model'];
$modelInstance = $this->objectManager->get($modelClass);
if (!$modelInstance instanceof \Magento\Framework\Data\OptionSourceInterface) {
throw new \UnexpectedValueException(
sprintf("Instance of the options source model is expected, got %s instead.", get_class($modelInstance))
);
}
$result = [];
foreach ($modelInstance->toOptionArray() as $value => $label) {
if (is_array($label)) {
$result[] = $label;
} else {
$result[] = ['value' => $value, 'label' => $label];
}
}
return $result;
}
}