There is a solution that I use frequently, I would like to know if there is an design pattern that is related or next to this solution:
interface PhoneFormatterInterface {
public function format($phone);
}
class BrazilFormatter implements PhoneFormatterInterface {
public function format($phone) {
//format a phone from +55 11 9 6666 2222 to (11) 96666-2222
}
public function isElegible($country) {
//check if the country is right
}
}
class PhoneFormatter {
public $formatters;
public function add(PhoneFormatterInterface $phoneFormatter)
{
//add formatters
}
public function format($phone, $country)
{
foreach ($formatters as $formatter) {
if ($formatter->isElegible($country)) {
return $formatter->format($phone);
}
}
}
}
class Client
{
public function main()
{
$formatter = new PhoneFormatter();
$formatter->add(...) //Add formatters, may we have some factory here
$formatter->format('+55 11 9 6666 6666', 'BR');
}
}
Can I name this solution with a known pattern?
Thank you!
Aucun commentaire:
Enregistrer un commentaire