Last active
August 29, 2015 14:02
-
-
Save stephandesouza/0d6ff680532668e6eab0 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 | |
interface Produto | |
{ | |
public function especificacoes(); | |
} | |
class Livro implements Produto | |
{ | |
public function especificacoes() { | |
} | |
} | |
class Caneta implements Produto | |
{ | |
public function especificacoes() { | |
} | |
} | |
class Papel implements Produto | |
{ | |
public function especificacoes() { | |
} | |
} | |
class ProdutoFactory extends TipedFactory | |
{ | |
private $interface = 'Produto'; | |
public function readDefaults() { | |
return include('./types.produto.php'); | |
} | |
} |
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 | |
interface Atendimento | |
{ | |
public function encerrar(); | |
} | |
class Suporte implements Atendimento | |
{ | |
public function encerrar() | |
{ | |
return "Encerrar Suporte"; | |
} | |
} | |
class Financeiro implements Atendimento | |
{ | |
public function encerrar() | |
{ | |
return "Encerrar Financeiro"; | |
} | |
} | |
class Administrativo implements Atendimento | |
{ | |
public function encerrar() | |
{ | |
return "Encerrar Administrativo"; | |
} | |
} | |
abstract class TipedFactory { | |
/** | |
* $types funcionará como um Dictionary, que será utilizado para | |
* definir o tipo de atendimento e a respectiva classe | |
* | |
* @var array | |
*/ | |
private $types = array(); | |
/** | |
* $interface especifica a qual Interface este Factory deve respeitar | |
* @var string | |
*/ | |
private $interface; | |
public function __construct() | |
{ | |
$this->configureDefaults(); | |
} | |
/** | |
* Retorna as configurações padrões a ser usada no Factory | |
* @return array | |
*/ | |
public function readDefaults() { | |
return array(); | |
} | |
/** | |
* Configura os tipos padrão de atendimento. É chamado automaticamente | |
* pelo construtor. | |
*/ | |
protected function configureDefaults() | |
{ | |
$defaults = $this->readDefaults(); | |
foreach($defaults as $key => $class) { | |
$this->setTypeClass($key, $class); | |
} | |
} | |
/** | |
* | |
* @param string $type | |
* @return callable | |
* @throws \OutOfBoundsException | |
*/ | |
public function getByType($type) | |
{ | |
if (!isset($this->types[$type])) { | |
throw new \OutOfBoundsException('tipo desconhecido'); | |
} | |
return new $this->types[$type]; | |
} | |
/** | |
* Permite que novos tipos sejam adicionados, se necessário | |
*/ | |
public function setTypeClass($type, $class) | |
{ | |
if (!class_exists($class) || !in_array($this->interface, class_implements($class))) { | |
throw new \OutOfBoundsException('tipo inválido'); | |
} | |
$this->types[$type] = $class; | |
} | |
} | |
class AtendimentoFactory extends TipedFactory | |
{ | |
private $interface = 'Atendimento'; | |
public function readDefaults() { | |
return include('./types.atendimento.php'); | |
} | |
} |
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 | |
return array( | |
1 => 'Suporte', | |
'Financeiro', | |
'Administrativo' | |
); |
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
return array( | |
1 => 'Livro', | |
'Caneta', | |
'Papel' | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment