Filter validation rules trim array values

I have values in array and i want to trim all values.

Check my below rule and it is working ,but Is there any other way do it without wrting filter function?


$this->origin = [" Moscow  ","Kursk "];

$this->destination  = ["Voronezh  ","     Belgorod"];


[['origin', 'destination'], 'filter','filter'=>function($value){

		    if(is_array($value)){ // Trim values

			  $value = array_map("trim", $value);  

		    }  else {

			  $value = trim($value);

		    }

		    return $value;

		}, 'on' => 'search'],