Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Last active November 12, 2015 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageekguy/aa2b58d5b1fab2cf7bfe to your computer and use it in GitHub Desktop.
Save mageekguy/aa2b58d5b1fab2cf7bfe to your computer and use it in GitHub Desktop.
East iterator
<?php
interface consumer
{
function dataHasName(data $data, name $name);
}
interface aggregator
{
function recipientOfDataConsumerIs(consumer $consumer, recipient $recipient);
}
interface recipient
{
function dataConsumerIs(consumer $consumer);
}
interface cityDataProvider
{
function recipientOfCityMapperIsCountryBuilder(cityMapper $mapper, countryBuilder $recipient);
}
class data
{
private
$data
;
function __construct($data)
{
$this->data = $data;
}
function __toString()
{
return (string) $this->data;
}
}
class name extends data
{
}
class lazyCityDataProvider implements cityDataProvider, recipient
{
private
$aggregator
;
function __construct(aggregator $aggregator)
{
$this->aggregator = $aggregator;
}
function recipientOfCityMapperIsCountryBuilder(cityMapper $cityMapper, countryBuilder $recipient)
{
$cityDataProvider = clone $this;
$cityDataProvider->countryBuilder = $recipient;
$this->aggregator->recipientOfDataConsumerIs($cityMapper, $cityDataProvider);
return $this;
}
function dataConsumerIs(consumer $consumer)
{
$this->countryBuilder->cityMapperIs($consumer);
return $this;
}
}
class queryResult implements aggregator
{
private
$records
;
function __construct(array $records)
{
$this->records = $records;
}
function recipientOfDataConsumerIs(consumer $consumer, recipient $recipient)
{
foreach ($this->records as $datas)
{
foreach ($datas as $field => $value)
{
$consumer = $consumer->dataHasName(new data($value), new name($field));
}
$recipient->dataConsumerIs($consumer);
}
return $this;
}
}
class city
{
private
$id,
$name
;
function nameIs(cityName $name)
{
$city = clone $this;
$city->name = $name;
return $city;
}
function idIs(cityId $id)
{
$city = clone $this;
$city->id = $id;
return $city;
}
}
class cityId extends data {}
class cityName extends data {}
class cityMapper implements consumer
{
private
$city
;
function __construct(city $city)
{
$this->city = $city;
}
function dataHasName(data $data, name $name)
{
$mapper = clone $this;
switch ((string) $name)
{
case 'city_name':
$mapper->city = $mapper->city->nameIs(new cityName((string) $data));
break;
case 'city_id':
$mapper->city = $mapper->city->idIs(new cityId((string) $data));
break;
}
return $mapper;
}
}
class countryBuilder
{
function cityMapperIs(cityMapper $cityMapper)
{
var_dump($cityMapper);
return $this;
}
}
(new lazyCityDataProvider(
new queryResult([
[ 'city_name' => 'Reims', 'city_id' => 51 ],
[ 'city_name' => 'Lyon', 'city_id' => 69 ]
]
)
)
)->recipientOfCityMapperIsCountryBuilder(new cityMapper(new city), new countryBuilder);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment