Skip to content

Instantly share code, notes, and snippets.

@mavimo
Last active April 4, 2016 12:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavimo/b0c96c94bc5a76d5b086efd0b2244e92 to your computer and use it in GitHub Desktop.
Save mavimo/b0c96c94bc5a76d5b086efd0b2244e92 to your computer and use it in GitHub Desktop.
Codemaster day2
<?php
namespace Codemaster\Example\Day2;
use Codemaster\Example\Day2\EngineInterface;
class Car
{
private $engine;
private $wheels;
public function __construct(EngineInterface $engine, WheelsArray $wheels)
{
$this->engine = $engine;
$this->wheels = $wheels;
}
public function __toString()
{
return sprintf("Car con %s e %s", $this->engine, $this->wheels);
}
}
<?php
namespace Codemaster\Example\Day2;
use InvalidArgumentException;
abstract class Engine
{
private $power;
public function __construct($power)
{
if (is_int($power) && $power > 0) {
$this->power = $power;
} else {
throw new InvalidArgumentException();
}
}
public function __toString()
{
return sprintf("Motore con %dcv", $this->power);
}
}
<?php
namespace Codemaster\Example\Day2;
use Codemaster\Example\Day2\Engine;
use InvalidArgumentException;
class EngineCombustione extends Engine implements EngineInterface
{
private $fuel;
const GPL = 'GPL';
const DIESEL = 'DIESEL';
const BENZINA = 'BENZINA';
public function __construct($fuel, $power)
{
parent::__construct($power);
if ($fuel === EngineCombustione::GPL ||
$fuel === EngineCombustione::BENZINA ||
$fuel === EngineCombustione::DIESEL) {
$this->fuel = $fuel;
} else {
throw new InvalidArgumentException();
}
}
}
<?php
namespace Codemaster\Example\Day2;
use Codemaster\Example\Day2\Engine;
class EngineElettrico extends Engine implements EngineInterface
{
private $fuel;
const ELETTRICITA = 'ELETTRICITA';
public function __construct($fuel, $power)
{
parent::__construct($power);
if ($fuel === EngineElettrico::ELETTRICITA) {
$this->fuel = $fuel;
} else {
throw new InvalidArgumentException();
}
}
}
<?php
namespace Codemaster\Example\Day2;
interface EngineInterface
{
}
<?php
include "Wheel.php";
include "WheelsArray.php";
include "Car.php";
include "Engine.php";
include "EngineInterface.php";
include "EngineCombustione.php";
include "EngineElettrico.php";
use Codemaster\Example\Day2\Wheel;
use Codemaster\Example\Day2\WheelsArray;
use Codemaster\Example\Day2\Car;
use Codemaster\Example\Day2\EngineCombustione;
use Codemaster\Example\Day2\EngineElettrico;
// Creo l'automobile
$wheels = new WheelsArray();
for ($i = 0; $i < 4; $i++) {
$wheels->addWheel(new Wheel(Wheel::NEVE, 18));
}
// $wheels->addWheel(new Wheel(Wheel::NEVE, 18));
// $wheels->addWheel(new Wheel(Wheel::NEVE, 18));
// $wheels->addWheel(new Wheel(Wheel::NEVE, 18));
$eng1 = new EngineCombustione(EngineCombustione::GPL, 70);
$automobile = new Car($eng1, $wheels);
print $automobile;
print '<br />';
// Creo l'automobile elettrica
$engElettrico = new EngineElettrico(EngineElettrico::ELETTRICITA, 70);
$automobileElettrica = new Car($engElettrico, $wheels);
print $automobileElettrica;
print '<br />';
// Creo il camion
$wheels = new WheelsArray();
for ($i = 0; $i < 6; $i++) {
$wheels->addWheel(new Wheel(Wheel::NORMALE, 38));
}
$eng2 = new EngineCombustione(EngineCombustione::DIESEL, 350);
$camion = new Car($eng2, $wheels);
print $camion;
print '<br />';
<?php
namespace Codemaster\Example\Day2;
use InvalidArgumentException;
class Wheel
{
private $type;
private $size;
const NEVE = 'NEVE';
const NORMALE = 'NORMALE';
public function __construct($type, $size)
{
if ($type === Wheel::NEVE || $type === Wheel::NORMALE) {
$this->type = $type;
} else {
// Errore
throw new InvalidArgumentException();
}
if (is_int($size) && $size > 0) {
$this->size = $size;
} else {
// Errore
throw new InvalidArgumentException();
}
}
public function __toString()
{
return sprintf('Ruota %s da %d"', $this->type, $this->size);
}
}
<?php
namespace Codemaster\Example\Day2;
use Codemaster\Example\Day2\Wheel;
class WheelsArray
{
private $wheels = [];
public function addWheel(Wheel $wheel)
{
$this->wheels[] = $wheel;
}
public function __toString()
{
return sprintf("%d %s", count($this->wheels), reset($this->wheels));
// $data = '';
// foreach ($this->wheels as $wheel) {
// $data .= $wheel;
// }
// return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment