Skip to content

Instantly share code, notes, and snippets.

@spolischook
Last active September 19, 2016 10:03
Show Gist options
  • Save spolischook/5d737ff20726f63c0f8f24914a580156 to your computer and use it in GitHub Desktop.
Save spolischook/5d737ff20726f63c0f8f24914a580156 to your computer and use it in GitHub Desktop.
<?php
interface ComparativeFood
{
public static function isApplicable(Food $food)
}
class Food {}
abstract class MarketFood implements ComparativeFood
{
public function __construct(Food $food, Envirorment $env)
{}
}
abstract class Fruits extends MarketFood{}
abstract class Vegitables extends MarketFood{}
class Apple extends Fruits
{
/**
* @return bool
*/
public static function isApplicable(Food $food)
{
return 'sphere' === $food->getForm()
&& 'white' === $food->getInsideColor();
}
}
class Watermelon extends Vegitables
{
/**
* @return bool
*/
public static function isApplicable(Food $food)
{
return 'sphere' === $food->getForm()
&& 'red' === $food->getInsideColor();
}
}
class Creator {
public function __construct()
{
$this->food['fruits'] = ['Apple'];
$this->food['vegitables'] = ['Watermelon'];
}
/**
* @return MarketFood|null
*/
public function create(Food $food, $type)
{
foreach ($this->food[$type] as $concreteFood)
{
if ($concreateFood::isApplicable($food)) {
return new $concreateFood($food, $envirorment);
}
return null;
}
}
}
$food = new Food();
$food->setForm('sphere');
$food->setColor('red');
$marketFood = $creator->create($food, 'vegitables');
var_dump(get_class($marketFood)); // Watermelon
$food->setColor('white');
$marketFood = $creator->create($food, 'fruits');
var_dump(get_class($marketFood)); // Apple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment