Last active
September 23, 2020 16:25
-
-
Save mayoz/17a96a87605bd4e615613da031e16699 to your computer and use it in GitHub Desktop.
Refactor normalizer to cleaner implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Normalizer; | |
class CompositeNormalizer implements Normalizer | |
{ | |
protected $normalizers; | |
/** | |
* @param Normalizer[] $normalizers | |
*/ | |
public function __construct($normalizers) | |
{ | |
$this->normalizers = $normalizers; | |
} | |
/** | |
* @param array[] $segments | |
* @return array[] | |
*/ | |
public function normalize($segments): array; | |
{ | |
foreach ($this->normalizers as $normalizer) { | |
$segments = (new $normalizer)->normalize($segments) | |
} | |
return $segments; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Normalizer; | |
class ConditionNormalizer implements Normalizer | |
{ | |
const CONDITIONS = [ | |
'foo', | |
'bar', | |
'baz', | |
]; | |
/** | |
* @param array[] $segments | |
* @return array[] | |
*/ | |
public function normalize($segments): array | |
{ | |
foreach ($segments as $segment) { | |
if ($this->check($segment) { | |
$segment['conditionLogic'] = 'or'; | |
} | |
} | |
return $segments; | |
} | |
/** | |
* @param array $segment | |
* @return bool | |
*/ | |
protected function check($segment): bool | |
{ | |
return !empty($segment['conditionList']) | |
&& in_array($segment['conditionList'][0]->id, self::CONDITIONS)) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Normalizer; | |
class DateRangeNormalizer implements Normalizer | |
{ | |
/** | |
* @param array[] $segments | |
* @return array[] | |
*/ | |
public function normalize($segments): array; | |
{ | |
foreach ($segments as $segment) { | |
// run your logic, manipulate $segment variable if need | |
} | |
return $segments; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Normalizer; | |
interface Normalizer | |
{ | |
/** | |
* @param array[] $segments | |
* @return array[] | |
*/ | |
public function normalize($segments): array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: