Skip to content

Instantly share code, notes, and snippets.

@zogot
Last active August 29, 2015 14:24
Show Gist options
  • Save zogot/bbb34e7f0283c6522575 to your computer and use it in GitHub Desktop.
Save zogot/bbb34e7f0283c6522575 to your computer and use it in GitHub Desktop.
<?php
namespace Project\Planets;
class Planet
{
protected $name;
/**
* @var Resource[]
*/
protected $resources = [];
public function __construct($name)
{
$this->name = $name;
}
public function addResource(Resource $resource)
{
$this->resources[$resource->getName()] = $resource;
}
public function hasEnoughResource(Resource $resource)
{
$resourceName = $resource->getName();
if(!array_key_exists($resource->getName(), $this->resources)) {
return false;
}
return ($resource->getValue() <= $this->resources[$resourceName]->getValue());
}
}
class Resource
{
protected $name;
protected $value;
public function __construct($name, $value)
{
$this->name = $name;
$this->value = $value;
}
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
}
class Oxygen extends Resource
{
public function __construct($value)
{
parent::__construct('oxygen', $value);
}
}
$planet = new Planet('Sol');
$planet->addResource(new Resource('Metal', 100));
$planet->addResource(new Resource('Oxygen', 150));
$planet->hasEnoughResource(new Resource('Oxygen', 170));
// better is this:
/**
* $planet->addResource(new Oxygen(20));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment