Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Created Feb 25, 2016
Embed
What would you like to do?
Just a dna processor written in 30 minutes in the train for my friend Gerald.
<?php
interface base
{
function adnFormaterIs(adnFormater $formater);
}
interface baseRecipient
{
function baseIs(base $base);
}
class a
implements
base
{
function adnFormaterIs(adnFormater $formater)
{
$formater->baseIsA();
return $this;
}
}
class t
implements
base
{
function adnFormaterIs(adnFormater $formater)
{
$formater->baseIsT();
return $this;
}
}
class c
implements
base
{
function adnFormaterIs(adnFormater $formater)
{
$formater->baseIsC();
return $this;
}
}
class g
implements
base
{
function adnFormaterIs(adnFormater $formater)
{
$formater->baseIsG();
return $this;
}
}
interface adn
{
function addendIsBase(base $base);
function recipientOfBaseIs(baseRecipient $recipient);
}
class notSyntheticAdn
implements
adn
{
function __construct(base... $bases)
{
$this->bases = $bases;
}
function addendIsBase(base $base)
{
$adn = clone $this;
$adn->bases[] = $base;
return $adn;
}
function recipientOfBaseIs(baseRecipient $recipient)
{
foreach ($this->bases as $base)
{
$recipient->baseIs($base);
}
return $this;
}
}
interface adnProvider
{
function recipientOfAdnIs(adnRecipient $recipient);
}
class adnProviderString
implements
adnProvider
{
private
$string
;
function __construct($string)
{
$this->string = $string;
}
function recipientOfAdnIs(adnRecipient $recipient)
{
$adn = new notSyntheticAdn;
foreach (str_split($this->string) as $letter)
{
switch ($letter)
{
case 'A':
$adn = $adn->addendIsBase(new A);
break;
case 'T':
$adn = $adn->addendIsBase(new T);
break;
case 'C':
$adn = $adn->addendIsBase(new C);
break;
case 'G':
$adn = $adn->addendIsBase(new G);
break;
}
}
$recipient->adnIs($adn);
return $this;
}
}
interface output
{
function stringIs($string);
}
class stdout
implements
output
{
function stringIs($string)
{
echo $string . PHP_EOL;
return $this;
}
}
class nilOutput
implements
output
{
function stringIs($string)
{
return $this;
}
}
interface adnFormater
{
function outputOfAdnIs(output $output, adn $adn);
function baseIsA();
function baseIsT();
function baseIsC();
function baseIsG();
}
interface adnRecipient
{
function adnIs(adn $adn);
}
class AdnFormaterWXYZ
implements
adnFormater,
baseRecipient
{
private
$output
;
function __construct()
{
$this->output = new nilOutput;
}
function outputOfAdnIs(output $output, adn $adn)
{
$formater = clone $this;
$formater->output = $output;
$adn->recipientOfBaseIs($formater);
return $this;
}
function baseIs(base $base)
{
$base->adnFormaterIs($this);
return $this;
}
function baseIsA()
{
$this->output->stringIs('W');
return $this;
}
function baseIsT()
{
$this->output->stringIs('X');
return $this;
}
function baseIsC()
{
$this->output->stringIs('Y');
return $this;
}
function baseIsG()
{
$this->output->stringIs('Z');
return $this;
}
}
class adnProcessor
implements
adnRecipient
{
private
$formater,
$output
;
function __construct(adnFormater $formater, output $output)
{
$this->formater = $formater;
$this->output = $output;
}
function adnIs(adn $adn)
{
$this->formater->outputOfAdnIs($this->output, $adn);
return $this;
}
}
(new adnProviderString('GATTACA'))->recipientOfAdnIs(new adnProcessor(new AdnFormaterWXYZ, new stdout));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment