Skip to content

Instantly share code, notes, and snippets.

@jdeveloper
Last active June 20, 2019 19:14
Show Gist options
  • Save jdeveloper/3dd10ff80437837793c2b33904bb9149 to your computer and use it in GitHub Desktop.
Save jdeveloper/3dd10ff80437837793c2b33904bb9149 to your computer and use it in GitHub Desktop.
solid
class Circle
{
public $radius;
public function __construct($radius): void
{
$this->radius = $radius;
}
}
class Square
{
public $length;
public function __construct($length): void
{
$this->length = $length;
}
}
class SumCalculator
{
public function sum(): float
{
$area = [];
foreach($this->shapes as $shape) {
if($shape instanceof Square){
$area[] = pow($shape->length, 2);
}else if($shape instanceof Circle) {
$area[] = pi() * pow($shape->radius, 2);
}
}
return array_sum($area);
}
}
class Circle
{
public $radius;
public function __construct($radius): void
{
$this->radius = $radius;
}
public function area(): float
{
return pi() * pow($this->radius, 2);
}
}
class Square
{
public $length;
public function __construct($length): void
{
$this->length = $length;
}
public function area(): float
{
// logic
}
}
class SumCalculator
{
public function sum(): float
{
$area = [];
foreach($this->shapes as $shape) {
$area[] = $shape->area();
}
return array_sum($area);
}
}
class PasswordReminder {
private $dbConnection;
public function __construct(MySQLConnection $dbConnection)
{
$this->dbConnection = $dbConnection;
}
}
interface DBConnectionInterface
{
public function connect(): void;
}
class MySQLConnection implements DBConnectionInterface
{
public function connect(): void
{
return "Database connection";
}
}
class PasswordReminder {
private $dbConnection;
public function __construct(DBConnectionInterface $dbConnection)
{
$this->dbConnection = $dbConnection;
}
}
class ExamService2
{
public function resolveExam(Exam $exam, array $answers): void
{
// logic
}
}
interface ExamExporterInterface interface
{
public function export(Exam $exam): string;
}
class ExamXmlExporter implements ExamExporterInterface
{
public function export(Exam $exam): string
{
// logic
}
}
class ExamJsonExporter implements ExamExporterInterface
{
public function export(Exam $exam): string
{
// logic
}
}
class ExamCommand
{
public function execute()
{
// logic getting exam
$examService->resolveExam($exam, $answers);
$examExporter->export($exam);
}
}
class ExamService
{
public function resolveExam(Exam $exam, array $answers): void
{
// logic
}
public function export(Exam $exam): string
{
// logic
}
}
class ExamCommand
{
public function execute(): void
{
// logic getting exam
$examService->resolveExam($exam, $answers);
$examService->export($exam);
}
}
class ExamService2
{
public function resolveExam(Exam $exam, array $answers): void
{
// logic
}
}
interface HttpClientInterface
{
public function get($url): Response;
}
class HttpClient implements HttpClientInterface
{
public function get($url): Response
{
return new Response();
}
}
class CachedHttpClient implement HttpClientInterface
{
protected $httpClient;
protected $cached = [];
public function __constructor(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
public function get($url): Resint
{
return $this->cached[$url];
}
}
class Rectangle
{
protected $witdth;
protected $height;
public function setWidth(int $width): void
{
$this->width = $width;
}
public function getWidth(): int
{
return $this->width;
}
public function setHeight(int $height): void
{
$this->height = $height;
}
public function getHeight(): int
{
return $this->height;
}
public function area(): int
{
return $this->width*$this->height;
}
}
class Square extends Rectangle
{
public function setWidth(int $width): void
{
parent::setWidth($width);
parent::setHeight($width);
}
public function setHeight(int $height): void
{
parent::setHeight($height);
parent::setWidth($height);
}
}
class AreaTest extends \PHPUnit_Framework_TestCase
{
public function testArea(): void
{
$rectangle = new Rectangle();
$rectangle->setWidth(5);
$rectangle->setHeight(4);
$this->assertEquals(20, $rectangle->area());
}
}
interface Product
{
public function getName(): string;
public function getStock(): int;
public function getNumberOfDisks(): int;
public function getReleaseDate(): \DateTime;
}
class CD implements Product
{
// implmentacion
}
class DVD implements Product
{
// implmentacion
}
interface AgeAware
{
public function getEdadMinima(): int;
}
class CD implements Product
{
// implmentacion
}
class DVD implements Product, AgeAware
{
// implmentacion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment