Skip to content

Instantly share code, notes, and snippets.

@martindilling
Last active May 25, 2021 08:38
Show Gist options
  • Save martindilling/54bad155839b1c95e82844f16858249d to your computer and use it in GitHub Desktop.
Save martindilling/54bad155839b1c95e82844f16858249d to your computer and use it in GitHub Desktop.
The actual "application" code isn't that great, but the structure of the objects should be decent to work with and make it easy to expand the system with more shapes or calculating different things for shapes. For example, add a method on the Shape interface to get the circumference, then you're forced to implement that method on all the shapes.
--------------------------------------------------------------------------------
Choose shape:
1: Rectangle
2: Isosceles Triangle
3: Circle
#> 3
--------------------------------------------------------------------------------
Write the arguments for a Circle:
#Radius> 15
================================================================================
Given a Circle with the dimensions:
Radius = 15
--------------------------------------------------------------------------------
Result:
The area is: 706.8583470577
--------------------------------------------------------------------------------
Choose shape:
1: Rectangle
2: Isosceles Triangle
3: Circle
#> 2
--------------------------------------------------------------------------------
Write the arguments for a Isosceles Triangle:
#Base> 15
#Height> 20
================================================================================
Given a Isosceles Triangle with the dimensions:
Base = 15
Height = 20
--------------------------------------------------------------------------------
Result:
The area is: 150
--------------------------------------------------------------------------------
Choose shape:
1: Rectangle
2: Isosceles Triangle
3: Circle
#> 1
--------------------------------------------------------------------------------
Write the arguments for a Rectangle:
#Width> 15
#Height> 20
================================================================================
Given a Rectangle with the dimensions:
Width = 15
Height = 20
--------------------------------------------------------------------------------
Result:
The area is: 300
<?php
/******************************************************************************
* Objects
******************************************************************************/
interface Shape
{
public static function name() : string;
public static function arguments() : array;
public function area() : float;
}
class Rectangle implements Shape
{
/** @var float */
private $width;
/** @var float */
private $height;
public function __construct(float $width, float $height)
{
$this->width = $width;
$this->height = $height;
}
public static function name() : string
{
return 'Rectangle';
}
public static function arguments() : array
{
return [
1 => 'Width',
2 => 'Height',
];
}
public function area() : float
{
return $this->width * $this->height;
}
}
class IsoscelesTriangle implements Shape
{
/** @var float */
private $base;
/** @var float */
private $height;
public function __construct(float $base, float $height)
{
$this->base = $base;
$this->height = $height;
}
public static function name() : string
{
return 'Isosceles Triangle';
}
public static function arguments() : array
{
return [
1 => 'Base',
2 => 'Height',
];
}
public function area() : float
{
return 0.5 * $this->base * $this->height;
}
}
class Circle implements Shape
{
/** @var float */
private $radius;
public function __construct(float $radius)
{
$this->radius = $radius;
}
public static function name() : string
{
return 'Circle';
}
public static function arguments() : array
{
return [
1 => 'Radius',
];
}
public function area() : float
{
return pi() * pow($this->radius, 2);
}
}
/******************************************************************************
* Helper functions to help working in the terminal
******************************************************************************/
function write(string $text) : void
{
fwrite(STDOUT, $text);
}
function line(string $text) : void
{
write($text . "\n");
}
function nl() : void
{
line('');
}
function heading(string $text, string $lineChar = '-') : void
{
nl();
line(str_repeat($lineChar, 80));
line($text);
}
function input(string $text = '') : string
{
write("#{$text}> ");
return fgets(STDIN);
}
/******************************************************************************
* Run the application
******************************************************************************/
// The available shapes
$shapes = [
1 => Rectangle::class,
2 => IsoscelesTriangle::class,
3 => Circle::class,
];
// Ask to select an available shape
heading('Choose shape:');
/** @var Shape $class */
foreach ($shapes as $key => $class) {
line(" {$key}: " . $class::name());
}
// Get the selected shape class (Should have error handling)
$selection = (int) input();
$class = $shapes[$selection];
// Ask to write the needed values to construct the selected shape
heading("Write the arguments for a {$class::name()}:");
$arguments = [];
foreach ($class::arguments() as $key => $argument) {
$arguments[$key] = (float) input($argument);
}
// Construct the selected shape with given arguments
/** @var Shape $shape */
$shape = new $class(...$arguments);
// Print summary of selection
heading("Given a {$class::name()} with the dimensions: ", '=');
foreach ($class::arguments() as $key => $argument) {
line("{$argument} = {$arguments[$key]}");
}
// Print the result
heading('Result:');
line("The area is: {$shape->area()}");
nl();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment