diff --git a/src/Form.php b/src/Form.php index e761f0c..a36b448 100644 --- a/src/Form.php +++ b/src/Form.php @@ -564,9 +564,10 @@ public function count(): int /** * Method to get the field values as an array * + * @param array $options * @return array */ - public function toArray(): array + public function toArray(array $options = []): array { $fieldValues = []; @@ -574,6 +575,18 @@ public function toArray(): array $fieldValues = array_merge($fieldValues, $fieldset->toArray()); } + if (!empty($options)) { + if (isset($options['exclude'])) { + if (!is_array($options['exclude'])) { + $options['exclude'] = [$options['exclude']]; + } + $fieldValues = array_diff_key($fieldValues, array_flip($options['exclude'])); + } + if (isset($options['filter'])) { + $fieldValues = array_filter($fieldValues, $options['filter']); + } + } + return $fieldValues; } diff --git a/src/FormInterface.php b/src/FormInterface.php index 364a646..268675d 100644 --- a/src/FormInterface.php +++ b/src/FormInterface.php @@ -39,9 +39,10 @@ public function count(): int; /** * Get values * + * @param array $options * @return array */ - public function toArray(): array; + public function toArray(array $options = []): array; /** * Method to iterate over object @@ -116,4 +117,4 @@ public function offsetSet(mixed $offset, mixed $value): void; */ public function offsetUnset(mixed $offset): void; -} \ No newline at end of file +} diff --git a/src/FormValidator.php b/src/FormValidator.php index 4b5e283..54d5fc5 100644 --- a/src/FormValidator.php +++ b/src/FormValidator.php @@ -506,11 +506,26 @@ public function count(): int /** * Get values * + * @param array $options * @return array */ - public function toArray(): array + public function toArray(array $options = []): array { - return $this->values; + $fieldValues = $this->values; + + if (!empty($options)) { + if (isset($options['exclude'])) { + if (!is_array($options['exclude'])) { + $options['exclude'] = [$options['exclude']]; + } + $fieldValues = array_diff_key($fieldValues, array_flip($options['exclude'])); + } + if (isset($options['filter'])) { + $fieldValues = array_filter($fieldValues, $options['filter']); + } + } + + return $fieldValues; } /** @@ -560,4 +575,4 @@ public function __unset(string $name): void } } -} \ No newline at end of file +}