Skip to content

Commit ff4ee57

Browse files
committed
NEW Get and Set options with dot notation
You can now access options or set them via dot notation, such as: `GoogleMapField::create($do, 'Map')->setOption('map.zoom', 12);`
1 parent 4d6c26e commit ff4ee57

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

code/GoogleMapField.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function setValue($value) {
7171
public function saveInto(DataObjectInterface $record) {
7272
$record->setCastedField($this->getLatField(), $this->latField->dataValue());
7373
$record->setCastedField($this->getLngField(), $this->lngField->dataValue());
74+
return $this;
7475
}
7576

7677
public function getChildFields() {
@@ -97,8 +98,43 @@ public function getLngData() {
9798
return $this->data->$fieldNames['lng'];
9899
}
99100

100-
public function getOption($option) {
101-
return $this->options[$option];
101+
public function getOption($name) {
102+
// Quicker execution path for "."-free names
103+
if (strpos($name, '.') === false) {
104+
if (isset($this->options[$name])) return $this->options[$name];
105+
} else {
106+
$names = explode('.', $name);
107+
108+
$var = $this->options;
109+
110+
foreach($names as $n) {
111+
if(!isset($var[$n])) {
112+
return null;
113+
}
114+
$var = $var[$n];
115+
}
116+
117+
return $var;
118+
}
119+
}
120+
121+
public function setOption($name, $val) {
122+
// Quicker execution path for "."-free names
123+
if(strpos($name,'.') === false) {
124+
$this->options[$name] = $val;
125+
} else {
126+
$names = explode('.', $name);
127+
128+
// We still want to do this even if we have strict path checking for legacy code
129+
$var = &$this->options;
130+
131+
foreach($names as $n) {
132+
$var = &$var[$n];
133+
}
134+
135+
$var = $val;
136+
}
137+
return $this;
102138
}
103139

104140
}

0 commit comments

Comments
 (0)