/**
* Method to get an array of fieldset objects optionally filtered over a given field group.
*
* @param string $group The dot-separated form group path on which to filter the fieldsets.
*
* @return array The array of fieldset objects.
*
* @since 1.7.0
*/
public function getFieldsets($group = null)
{
$fieldsets = [];
$sets = [];
// Make sure there is a valid Form XML document.
if (!$this->xml instanceof \SimpleXMLElement) {
throw new \UnexpectedValueException(sprintf('%s::%s `xml` is not an instance of SimpleXMLElement', \get_class($this), __METHOD__));
}
if ($group) {
// Get the fields elements for a given group.
$elements =& $this->findGroup($group);
foreach ($elements as &$element) {
// Get an array of <fieldset /> elements and fieldset attributes within the fields element.
if ($tmp = $element->xpath('descendant::fieldset[@name] | descendant::field[@fieldset]/@fieldset')) {
$sets = array_merge($sets, (array) $tmp);
}
}
} else {
// Get an array of <fieldset /> elements and fieldset attributes.
$sets = $this->xml->xpath('//fieldset[@name and not(ancestor::field/form/*)] | //field[@fieldset and not(ancestor::field/form/*)]/@fieldset');
}
// If no fieldsets are found return empty.
if (empty($sets)) {
return $fieldsets;
}
// Process each found fieldset.
foreach ($sets as $set) {
if ((string) $set['hidden'] === 'true') {
continue;
}
// Are we dealing with a fieldset element?
if ((string) $set['name']) {
// Only create it if it doesn't already exist.
if (empty($fieldsets[(string) $set['name']])) {
// Build the fieldset object.
$fieldset = (object) array('name' => '', 'label' => '', 'description' => '');
foreach ($set->attributes() as $name => $value) {
$fieldset->{$name} = (string) $value;
}
// Add the fieldset object to the list.
$fieldsets[$fieldset->name] = $fieldset;
}
} else {
// Only create it if it doesn't already exist.
if (empty($fieldsets[(string) $set])) {
// Attempt to get the fieldset element for data (throughout the entire form document).
$tmp = $this->xml->xpath('//fieldset[@name="' . (string) $set . '"]');
// If no element was found, build a very simple fieldset object.
if (empty($tmp)) {
$fieldset = (object) array('name' => (string) $set, 'label' => '', 'description' => '');
} else {
$fieldset = (object) array('name' => '', 'label' => '', 'description' => '');
foreach ($tmp[0]->attributes() as $name => $value) {
$fieldset->{$name} = (string) $value;
}
}
// Add the fieldset object to the list.
$fieldsets[$fieldset->name] = $fieldset;
}
}
}
return $fieldsets;
}