Last active
October 18, 2019 12:59
-
-
Save felipevolpatto/e65ef3e263d218f46f969a96cd952c9b to your computer and use it in GitHub Desktop.
Design patterns: Builder pattern
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 Builder\Objets; | |
class Connection { | |
public $host; | |
public $port; | |
public $user; | |
public $pass; | |
public $db; | |
public $charset; | |
} |
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 Builder\Builders; | |
use Builder\Objets\Connection; | |
interface ConnectionBuilderInterface { | |
public function addHost(): void; | |
public function addPort(): void; | |
public function addCharset(): void; | |
public function addUser(): void; | |
public function addPass(): void; | |
public function addDb(): void; | |
public function getConnection(): Connection; | |
} |
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 Builder; | |
use Builder\Objets\Connection; | |
class ConnectionCreator { | |
public function buildConnection(object $builder): Connection { | |
$builder->addHost(); | |
$builder->addPort(); | |
$builder->addCharset(); | |
$builder->addUser(); | |
$builder->addPass(); | |
$builder->addDb(); | |
return $builder->getConnection(); | |
} | |
} |
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 | |
use Builder\Builders\InfluxConnectionBuilder; | |
use Builder\Builders\MysqlConnectionBuilder; | |
use Builder\Builders\RedisConnectionBuilder; | |
use Builder\ConnectionCreator; | |
$mysqlBuilder = new MysqlConnectionBuilder([ | |
'host' => 'localhost', | |
'port' => '3306', | |
'charset' => 'utf-8', | |
'user' => 'root-bling', | |
'pass' => '', | |
'db' => 'metrics' | |
]); | |
$influxBuilder = new InfluxConnectionBuilder([ | |
'host' => '192.168.0.14', | |
'port' => '8086', | |
'charset' => 'utf-8', | |
'user' => 'root', | |
'pass' => 'asdf000', | |
'db' => 'bling' | |
]); | |
$redisBuilder = new RedisConnectionBuilder([ | |
'host' => '192.168.0.15', | |
'port' => '6379', | |
'charset' => 'utf-16', | |
]); | |
$creator = new ConnectionCreator(); | |
$mysql = $creator->buildConnection($mysqlBuilder); | |
$influx = $creator->buildConnection($influxBuilder); | |
$redis = $creator->buildConnection($redisBuilder); | |
echo $mysql; | |
echo $influx; | |
echo $redis; |
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 Builder\Objets; | |
class InfluxConnection extends Connection { | |
public function __toString() { | |
return var_export($this, true); | |
} | |
} |
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 Builder\Builders; | |
use Builder\Builders\ConnectionBuilderInterface; | |
use Builder\Objets\Connection; | |
use Builder\Objets\InfluxConnection; | |
class InfluxConnectionBuilder implements ConnectionBuilderInterface { | |
private $options; | |
private $connection; | |
public function __construct(array $options) { | |
$this->options = $options; | |
$this->connection = new InfluxConnection(); | |
} | |
public function addHost(): void { | |
$this->connection->host = $this->options['host']; | |
} | |
public function addPort(): void { | |
$this->connection->port = $this->options['port']; | |
} | |
public function addUser(): void { | |
$this->connection->user = $this->options['user']; | |
} | |
public function addPass(): void { | |
$this->connection->pass = $this->options['pass']; | |
} | |
public function addCharset(): void { | |
$this->connection->charset = $this->options['charset']; | |
} | |
public function addDb(): void { | |
$this->connection->db = $this->options['db']; | |
} | |
public function getConnection(): Connection { | |
return $this->connection; | |
} | |
} |
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 Builder\Objets; | |
class MysqlConnection extends Connection { | |
public function __toString() { | |
return var_export($this, true); | |
} | |
} |
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 Builder\Builders; | |
use Builder\Objets\Connection; | |
use Builder\Objets\MysqlConnection; | |
use Builder\Builders\ConnectionBuilderInterface; | |
class MysqlConnectionBuilder implements ConnectionBuilderInterface { | |
private $options; | |
private $connection; | |
public function __construct(array $options) { | |
$this->options = $options; | |
$this->connection = new MysqlConnection(); | |
} | |
public function addHost(): void { | |
$this->connection->host = $this->options['host']; | |
} | |
public function addPort(): void { | |
$this->connection->port = $this->options['port']; | |
} | |
public function addUser(): void { | |
$this->connection->user = $this->options['user']; | |
} | |
public function addPass(): void { | |
$this->connection->pass = $this->options['pass']; | |
} | |
public function addCharset(): void { | |
$this->connection->charset = $this->options['charset']; | |
} | |
public function addDb(): void { | |
$this->connection->db = $this->options['db']; | |
} | |
public function getConnection(): Connection { | |
return $this->connection; | |
} | |
} |
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 Builder\Objets; | |
class RedisConnection extends Connection { | |
public function __toString() { | |
return var_export($this, true); | |
} | |
} |
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 Builder\Builders; | |
use Builder\Builders\ConnectionBuilderInterface; | |
use Builder\Objets\Connection; | |
use Builder\Objets\RedisConnection; | |
class RedisConnectionBuilder implements ConnectionBuilderInterface { | |
private $options; | |
private $connection; | |
public function __construct(array $options) { | |
$this->options = $options; | |
$this->connection = new RedisConnection(); | |
} | |
public function addHost(): void { | |
$this->connection->host = $this->options['host']; | |
} | |
public function addPort(): void { | |
$this->connection->port = $this->options['port']; | |
} | |
public function addUser(): void { } | |
public function addPass(): void { } | |
public function addCharset(): void { | |
$this->connection->charset = $this->options['charset']; | |
} | |
public function addDb(): void { | |
$this->connection->db = $this->options['db']; | |
} | |
public function getConnection(): Connection { | |
return $this->connection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment