|
1
|
|
|
<?php namespace Anomaly\SettingsModule\Setting\Command; |
|
2
|
|
|
|
|
3
|
|
|
use Anomaly\SettingsModule\Setting\Contract\SettingInterface; |
|
4
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldType; |
|
5
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeCollection; |
|
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class GetValueFieldType |
|
10
|
|
|
* |
|
11
|
|
|
* @link http://pyrocms.com/ |
|
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
|
13
|
|
|
* @author Ryan Thompson <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class GetValueFieldType |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The setting instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @var SettingInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $setting; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create a new GetValueFieldType instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @param SettingInterface $setting |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(SettingInterface $setting) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setting = $setting; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Handle the command. |
|
37
|
|
|
* |
|
38
|
|
|
* @param FieldTypeCollection $fieldTypes |
|
39
|
|
|
* @param Repository $config |
|
40
|
|
|
* @return FieldType |
|
41
|
|
|
*/ |
|
42
|
|
|
public function handle(FieldTypeCollection $fieldTypes, Repository $config) |
|
43
|
|
|
{ |
|
44
|
|
|
// Get the setting's key. |
|
45
|
|
|
$key = $this->setting->getKey(); |
|
46
|
|
|
|
|
47
|
|
|
// Get the bare value. |
|
48
|
|
|
$value = array_get($this->setting->getAttributes(), 'value'); |
|
49
|
|
|
|
|
50
|
|
|
// Try and find the setting's field configuration. |
|
51
|
|
|
if (!$field = $config->get(str_replace('::', '::settings/settings.', $key))) { |
|
52
|
|
|
$field = $config->get(str_replace('::', '::settings.', $key)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Convert short syntax. |
|
56
|
|
|
if (is_string($field)) { |
|
57
|
|
|
$field = [ |
|
58
|
|
|
'type' => $field, |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/* |
|
63
|
|
|
* Try and get the field type that |
|
64
|
|
|
* the setting uses. If none exists |
|
65
|
|
|
* then just return the value as is. |
|
66
|
|
|
*/ |
|
67
|
|
|
$type = $fieldTypes->get(array_get($field, 'type')); |
|
68
|
|
|
|
|
69
|
|
|
if (!$type) { |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Setup the field type. |
|
74
|
|
|
$type->setEntry($this->setting); |
|
75
|
|
|
$type->mergeRules(array_get($field, 'rules', [])); |
|
76
|
|
|
$type->mergeConfig(array_get($field, 'config', [])); |
|
77
|
|
|
|
|
78
|
|
|
/* |
|
79
|
|
|
* If the type can be determined then |
|
80
|
|
|
* get the modifier and restore the value |
|
81
|
|
|
* before returning it. |
|
82
|
|
|
*/ |
|
83
|
|
|
$modifier = $type->getModifier(); |
|
84
|
|
|
|
|
85
|
|
|
$type->setValue($modifier->restore($value)); |
|
86
|
|
|
|
|
87
|
|
|
return $type; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|