Skip to content

Instantly share code, notes, and snippets.

@mannion007
Last active September 24, 2016 23:42
Show Gist options
  • Save mannion007/e79be6354ebe3521cddfa9041cc2444a to your computer and use it in GitHub Desktop.
Save mannion007/e79be6354ebe3521cddfa9041cc2444a to your computer and use it in GitHub Desktop.
<?php
/**
* @author James Mannion <[email protected]>
* @link https://www.jamse.net
*/
/**
* Interface CarInterface
*/
interface CarInterface
{
public function getYearOfManufacture() : int;
public function getBrakeHorsePower() : int;
}
/**
* Class Car
*/
class Car implements CarInterface
{
private $yearOfManufacture;
private $brakeHorsePower;
public function __construct(int $yearOfManufacture, int $brakeHorsePower)
{
$this->yearOfManufacture = $yearOfManufacture;
$this->brakeHorsePower = $brakeHorsePower;
}
/**
* Year the car was manufactured
*
* @return int
*/
public function getYearOfManufacture() : int
{
return $this->yearOfManufacture;
}
/**
* Max brake horsepower
*
* @return int
*/
public function getBrakeHorsePower() : int
{
return $this->brakeHorsePower;
}
}
/**
* (Abstract) collection of cars.
* Class CarCollection
*/
abstract class CarCollection implements IteratorAggregate
{
/** @var Car[] */
protected $cars = [];
public function __construct($cars)
{
$this->cars = $cars;
}
/**
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->cars);
}
/**
* Add a car to the collection
*
* @param Car $carToAdd
*/
public function addCar(Car $carToAdd)
{
$this->cars[] = $carToAdd;
}
/**
* Get a filtered SportsCarCollection of cars newer than a given year
*
* @param int $yearOfManufacture
* @return CarCollection
*/
public function newerThan(int $yearOfManufacture) : CarCollection
{
$cars = [];
foreach ($this->cars as $car) {
if($car->getYearOfManufacture() >= $yearOfManufacture) {
$cars[] = $car;
}
}
return new static($cars);
}
}
/**
* A collection of sports cars
*
* Class SportsCarCollection
*/
class SportsCarCollection extends CarCollection
{
public function morePowerfulThan(int $brakeHorsePower) : SportsCarCollection
{
$cars = [];
foreach ($this->cars as $car) {
if($car->getBrakeHorsePower() >= $brakeHorsePower) {
$cars[] = $car;
}
}
return new static($cars);
}
}
/** Create a collection of sports cars made in different years with different BHP */
$sportsCars = new SportsCarCollection(
[
new Car(1995, 300),
new Car(1998, 450),
new Car(2004, 500),
new Car(1992, 400),
new Car(1985, 190)
]
);
/**
* Get and iterate over a list of sports cars built in or after 1995. Note that this behaviour is in the parent class.
*/
echo 'Modern sports cars:' . PHP_EOL;
$modernSportsCars = $sportsCars->newerThan(1995);
foreach ($modernSportsCars as $modernSportsCar) {
var_dump($modernSportsCar);
}
/**
* Get and iterate over a list of sports cars with 400BHP or more. Note that this behaviour is in the child class.
*/
echo 'Really powerful sports cars:' . PHP_EOL;
$lethalSportsCars = $sportsCars->morePowerfulThan(400);
foreach ($lethalSportsCars as $lethalSportsCar) {
var_dump($lethalSportsCar);
}
@mannion007
Copy link
Author

A simple implementation of an ArrayIterator for a blog post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment