I am currently prototyping some code that will enable named arguments to functions via an array. I have ended up with two different solutions:
public function trigger(array $options) {
$ops = $this->setOptions([
'name' => ['required' => true, 'unless' => ['event']],
'params' => ['required' => true, 'unless' => ['event'], 'type' => 'array'],
'callback' => ['type' => 'callable'],
'context' => ['type' => 'object'],
'event' => ['class' => 'Event']
], $options);
}
public function trigger(array $options) {
$ops = $this->setOptions([
'name' => (new Option())->required()->unless('event'),
'params' => (new Option())->required()->unless('event')->type('array'),
'callback' => (new Option())->type('callable'),
'context' => (new Option())->type('object'),
'event' => (new Option())->class('Event')
], $options);
}
Now, I am in the predicament of deciding which syntax I prefer. The first uses native arrays but the underlying code is pretty messy and has the potential of being harder to extend. The second is allot cleaner to implement but does introduce an extra dependency in the Option object. This does however have the benefit of making it easier to extend.
Has anyone got an opinion on which syntax they would prefer from a users perspective? In both cases the setOptions() method is provided via a trait.