forked from oroinc/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldSearchProvider.php
96 lines (79 loc) · 2.56 KB
/
FieldSearchProvider.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Oro\Bundle\ConfigBundle\Provider;
use Oro\Bundle\ConfigBundle\Config\ConfigBag;
use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\ConfigBundle\Exception\ItemNotFoundException;
use Symfony\Component\Translation\TranslatorInterface;
class FieldSearchProvider implements SearchProviderInterface
{
/** @var ConfigBag */
private $configBag;
/** @var TranslatorInterface */
private $translator;
/** @var ConfigManager */
private $configManager;
/**
* @param ConfigBag $configBag
* @param TranslatorInterface $translator
* @param ConfigManager $configManager
*/
public function __construct(ConfigBag $configBag, TranslatorInterface $translator, ConfigManager $configManager)
{
$this->configBag = $configBag;
$this->translator = $translator;
$this->configManager = $configManager;
}
/**
* {@inheritdoc}
*/
public function supports($name)
{
return $this->configBag->getFieldsRoot($name) !== false;
}
/**
* {@inheritdoc}
*/
public function getData($name)
{
$field = $this->configBag->getFieldsRoot($name);
if ($field === false) {
throw new ItemNotFoundException(sprintf('Field "%s" is not defined.', $name));
}
$searchData = [];
if (isset($field['options']['label'])) {
$searchData[] = $this->translator->trans($field['options']['label']);
}
if (isset($field['options']['tooltip'])) {
$searchData[] = $this->translator->trans($field['options']['tooltip']);
}
if (isset($field['search_type'])) {
$searchData = array_merge($searchData, $this->getBySearchType($name, $field));
}
return $searchData;
}
/**
* @param string $name
* @param array $field
*
* @return array
*/
private function getBySearchType($name, array $field)
{
if ($field['search_type'] === 'text') {
return [$this->configManager->get($name)];
}
if ($field['search_type'] === 'choice') {
if (!isset($field['options']['choices'])) {
throw new \LogicException(
'The choices option should be defined, when search type "choice" is used.'
);
}
$data = [];
foreach ($field['options']['choices'] as $choiceLabel => $choiceValue) {
$data[] = $this->translator->trans($choiceLabel);
}
return $data;
}
return [];
}
}