File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -191,13 +191,20 @@ public static function with_form_data(stdClass $formdata): static {
191191 /**
192192 * Transforms an instance into an associative array of data that can be passed to {@see moodleform::set_data}.
193193 *
194- * Simply casts the instance as an array, turning every public property into a key-value-pair in that array.
194+ * Turns every ** public** property into a key-value-pair in that array.
195195 *
196196 * @return array<string, mixed> Data to set on the config form.
197+ *
198+ * @phpcs:disable Squiz.WhiteSpace.ScopeClosingBrace
197199 */
198200 #[\Override]
199201 public function to_form_data (): array {
200- return (array ) $ this ;
202+ // This is a dirty hack to quickly get _only_ the public properties.
203+ $ closure = fn (simple_metric_config $ config ): array => get_object_vars ($ config );
204+ // Since `get_object_vars` is scope-aware, just calling it directly from an instance of a concrete subclass would also
205+ // return any protected properties set on that instance. We simulate a different scope by temporarily binding a closure to
206+ // an instance of an anonymous class; that closure gets the config instance as an argument and calls `get_object_vars`.
207+ return $ closure ->call (new class {}, $ this );
201208 }
202209
203210 /**
You can’t perform that action at this time.
0 commit comments