Is there a way to filter/transform a simple variable using Events?

Hello!

Is there a way to use events to filter variables? I'd like to hook some filters (with different levels of priority) and the event system should work. The only way I've figured out was creating a wrapper class to the value:

<?php
class EventFilter {

    private $value;

    public function __construct($value)
    {
        $this->setValue($value);
    }

    public function setValue($value)
    {
        $this->value = $value;
    }

    public function getValue()
    {
        return $this->value;
    }

}

And use it:

<?php
$messages = new EventFilter($messages);
$di->getShared('eventsManager')->fire('translate:messages', $messages);
$messages = $messages->getValue();

$di->getShared('eventsManager')->attach('translate:messages', function($event, EventFilter $ef){
    $messages = $ef->getValue();

    // Transform the $messages

    $ef->setValue($messages);
});

Is there a better way?

Your code is fine. Priorities can be set by the third argument to attach():

$eventsManager->attach('translate:messages', function() {}, 150); // make 150 less to decrease the priority