Skip to content

Instantly share code, notes, and snippets.

@felipevolpatto
Last active October 18, 2019 12:59
Show Gist options
  • Save felipevolpatto/e65ef3e263d218f46f969a96cd952c9b to your computer and use it in GitHub Desktop.
Save felipevolpatto/e65ef3e263d218f46f969a96cd952c9b to your computer and use it in GitHub Desktop.
Design patterns: Builder pattern
<?php
namespace Builder\Objets;
class Connection {
public $host;
public $port;
public $user;
public $pass;
public $db;
public $charset;
}
<?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;
}
<?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();
}
}
<?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;
<?php
namespace Builder\Objets;
class InfluxConnection extends Connection {
public function __toString() {
return var_export($this, true);
}
}
<?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;
}
}
<?php
namespace Builder\Objets;
class MysqlConnection extends Connection {
public function __toString() {
return var_export($this, true);
}
}
<?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;
}
}
<?php
namespace Builder\Objets;
class RedisConnection extends Connection {
public function __toString() {
return var_export($this, true);
}
}
<?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