Skip to content

Instantly share code, notes, and snippets.

@tipochka
Created March 29, 2018 06:37
Show Gist options
  • Save tipochka/145764b8139beb0713b152294722d788 to your computer and use it in GitHub Desktop.
Save tipochka/145764b8139beb0713b152294722d788 to your computer and use it in GitHub Desktop.
<?php
interface RectangleDataGetterInterface
{
public function getHeight(): int;
public function getWidth(): int;
}
interface RectangleInterface extends RectangleDataGetterInterface
{
public function setHeight(int $height);
public function setWidth(int $width);
}
interface SquareInterface extends RectangleDataGetterInterface
{
public function setSide(int $side);
}
class Rectangle implements RectangleInterface
{
private $height;
private $width;
public function __construct(int $height, int $width)
{
$this->height = $height;
$this->width = $width;
}
public function setHeight(int $height)
{
$this->height = $height;
}
public function setWidth(int $width)
{
$this->width = $width;
}
public function getHeight(): int
{
return $this->height;
}
public function getWidth(): int
{
return $this->width;
}
}
class Square implements SquareInterface
{
private $side;
public function __construct(int $side)
{
$this->side = $side;
}
public function setSide(int $side)
{
$this->side = $side;
}
public function getHeight(): int
{
return $this->side;
}
public function getWidth(): int
{
return $this->side;
}
}
class RectangleCalculator
{
private $rectangle;
public function __construct(RectangleDataGetterInterface $rectangle)
{
$this->rectangle = $rectangle;
}
public function getArea(): int
{
return $this->rectangle->getWidth() * $this->rectangle->getHeight();
}
}
$rectangle = new Rectangle(2, 4);
$rectangleCalculator = new RectangleCalculator($rectangle);
echo $rectangleCalculator->getArea() . PHP_EOL;
$square = new Square(3);
$squareCalculator = new RectangleCalculator($square);
echo $squareCalculator->getArea() . PHP_EOL;
$square->setSide(5);
echo $squareCalculator->getArea() . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment