Perhaps Simon is right with the comment:
FWIW I think you should just use input parameters, it's not like anyone is going to type this out.
But if you don't agree with that, then perhaps the simplification below will help.
###Possible simplification One consideration I had was to use a regular expression with named subpatterns- something like this:
preg_match('#-?((?P<keywords>[^-]+)-)?((?P<employmentTypes>[^-]+)-)?(?P<location>.+)#', $parameters, $matches);
Which works well for the case where all three parameters are supplied - e.g. "-engineer~c++-full_time-canada~paris"
:
$matches => Array
(
[keywords] => engineer~c++
[employmentTypes] => full_time
[location] => canada~paris
) //removed numeric keys
So then you can set the properties accordingly:
foreach(['keywords', 'employmentTypes', 'location'] as $key) {
if ($matches[$key]) {
$this->$key = $matches[$key];
}
}
However if either keywords or employment types are omitted, there would still need to be logic to determine which of those is still present... So perhaps it would be better to give more generic names for the first two - like groupA
and groupB
...
preg_match('#-?((?P<groupA>[^-]+)-)?((?P<groupB>[^-]+)-)?(?P<location>.+)#', $parameters, $matches);
And you would still need the logic to determine if groupA
had keywords or employment types... but at least you should likely be able to simplify assignment of the locations parameter.
###Simplifying the array_map()
call
The call to array_map()
in areEmploymentTypes()
:
$jobTypes = array_map(function ($v)
{
return mb_strtolower($v);
}, $this->allEmploymentTypes);
Can be simplified by simply passing the function name mb_strtolower
as a string literal:
$jobTypes = array_map('mb_strtolower', $allEmploymentTypes);
While there may not be a noticeable improvement in speed, it eliminates the excess closure.