Created
July 21, 2015 19:36
-
-
Save viniciusss/dc060cf0b8606ff97783 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Faturamento/EntregaOrigem; | |
$gerador = new GeradorObservacaoComposite(); | |
$gerador->addGerador(new GeradorObservacaoTipoEntrega()); | |
$entrega = new Entrega(new Entrega\Tipo('Entrega Loja')); | |
echo $gerador->gerarObservacao($entrega); // Resultado: O tipo de entrega é Entrega Loja. |
This file contains hidden or 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 Faturamento/EntregaOrigem; | |
interface GeradorObervacao | |
{ | |
public function getObservacao(Entrega $entrega); | |
} |
This file contains hidden or 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 Faturamento/EntregaOrigem; | |
abstract class GeradorObservacaoAbstract implements GeradorObervacao | |
{ | |
protected function getObservacaoPadrao(); | |
protected function getParams(Entrega $entrega); | |
public function getObservacao(Entrega $entrega) | |
{ | |
$params = $this->getParams($entrega); | |
$find = array_keys($params); | |
$replace = array_values($params); | |
return str_replace($search, $replace, $this->getObservacaoPadrao()); | |
} | |
} |
This file contains hidden or 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 Faturamento/EntregaOrigem; | |
class GeradorObservacaoComposite | |
{ | |
/** | |
* array | |
*/ | |
private $geradores; | |
public function __construct() | |
{ | |
$this->geradores = []; | |
} | |
public function addGerador(GeradorObervacao $gerador) | |
{ | |
$this->geradores[] = $gerador; | |
} | |
public function gerarObservacao(Entrega $entrega) | |
{ | |
$observacao = ''; | |
foreach($this->geradores as $gerador) { | |
$observacao .= $gerador->getObservacao($entrega); | |
} | |
return $observacao; | |
} | |
} |
This file contains hidden or 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 Faturamento/EntregaOrigem; | |
class GeradorObservacaoTipoEntrega extends GeradorObservacaoAbstract | |
{ | |
const MSG_PADRAO = 'O tipo de entrega é %tipo_entrega%.'; | |
protected function getObservacaoPadrao() | |
{ | |
return static::MSG_PADRAO; | |
} | |
protected function getParams(Entrega $entrega) | |
{ | |
return [ | |
'%tipo_entrega%' => $entrega->getTipo(), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment