Last active
August 29, 2015 14:16
-
-
Save mageekguy/dd610593e4be3ff90504 to your computer and use it in GitHub Desktop.
east oriented data loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace data | |
{ | |
interface loader | |
{ | |
function dataNeedDataWithName(name $name); | |
} | |
interface loadableData | |
{ | |
function dataLoaderIs(loader $loader); | |
function loaderHasDataWithName(data $data, name $name); | |
function loaderHasNoDataWithName(name $name); | |
function loaderHasNoMoreData(); | |
} | |
final class data | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! is_string($value)) | |
{ | |
throw new \domainException('Data should be a string'); | |
} | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
final class name | |
{ | |
private | |
$value | |
; | |
function __construct($value) | |
{ | |
if (! is_string($value)) | |
{ | |
throw new \domainException('Data name should be a string'); | |
} | |
$this->value = $value; | |
} | |
function __toString() | |
{ | |
return $this->value; | |
} | |
} | |
} | |
namespace | |
{ | |
use | |
\data | |
; | |
class db implements data\loader | |
{ | |
private | |
$recordset, | |
$names | |
; | |
function __construct(array $recordset) | |
{ | |
$this->recordset = $recordset; | |
} | |
function newLoadableData(data\loadableData $data) | |
{ | |
$loader = new self($this->recordset); | |
return $loader->loaderHasDataWithNameForData($data->dataLoaderIs($loader)); | |
} | |
function dataNeedDataWithName(data\name $name) | |
{ | |
$this->names[] = $name; | |
return $this; | |
} | |
private function loaderHasDataWithNameForData(data\loadableData $data) | |
{ | |
foreach ($this->recordset as $record) | |
{ | |
$newData = $data; | |
foreach ($this->names as $name) | |
{ | |
$newData = ! isset($record[(string) $name]) | |
? | |
$newData->loaderHasNoDataWithName($name) | |
: | |
$newData->loaderHasDataWithName(new data\data($record[(string) $name]), $name) | |
; | |
} | |
$newData->loaderHasNoMoreData(); | |
} | |
return $this; | |
} | |
} | |
class mail implements data\loadableData | |
{ | |
private | |
$sender, | |
$recipient, | |
$consumer | |
; | |
function __construct() | |
{ | |
$this->sender = new data\data(''); | |
$this->recipient = new data\data(''); | |
} | |
function dataLoaderIs(data\loader $loader) | |
{ | |
$loader | |
->dataNeedDataWithName(new data\name('sender')) | |
->dataNeedDataWithName(new data\name('recipient')) | |
; | |
return $this; | |
} | |
function loaderHasDataWithName(data\data $data, data\name $name) | |
{ | |
switch ((string) $name) | |
{ | |
case 'sender': | |
case 'recipient': | |
$mail = clone $this; | |
$mail->{$name} = $data; | |
} | |
return $mail; | |
} | |
function loaderHasNoDataWithName(data\name $name) | |
{ | |
return $this; | |
} | |
function loaderHasNoMoreData() | |
{ | |
if ($this->consumer) | |
{ | |
$this->consumer->newMail($this); | |
} | |
return $this; | |
} | |
function mailConsumerIs(mailConsumer $consumer) | |
{ | |
$mail = clone $this; | |
$mail->consumer = $consumer; | |
return $mail; | |
} | |
} | |
class mailConsumer | |
{ | |
function newMail(mail $mail) | |
{ | |
var_dump($mail); | |
return $this; | |
} | |
} | |
(new db([ | |
[ 'recipient' => 'foo', 'sender' => 'bar' ], | |
[ uniqid() => uniqid() , uniqid() => uniqid() ] | |
] | |
) | |
) | |
->newLoadableData( | |
(new mail) | |
->mailConsumerIs(new mailConsumer) | |
) | |
; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment