Skip to content

Instantly share code, notes, and snippets.

@benglass
Last active February 4, 2016 16:31
Show Gist options
  • Save benglass/3842d1b2b5a52bd8a985 to your computer and use it in GitHub Desktop.
Save benglass/3842d1b2b5a52bd8a985 to your computer and use it in GitHub Desktop.
<?php
interface OptionalFieldInterface
{
public function getName();
public function getValue();
}
class OptionalField implements OptionalFieldInterface
{
const NEWEST_STATUS = 'newestStatus';
const NEWEST_SUMMARY = 'newestSummary';
public function __construct($name, $value) {
$this->name = $name;
$this->value = $value;
}
public function getName() {
return $this->name;
}
public function getValue() {
return $this->value;
}
public static function newestStatus($value) {
return new self(self::NEWEST_STATUS, $value);
}
public static function newestSummary($value) {
return new self(self::NEWEST_SUMMARY, $value);
}
}
interface OptionalFieldCollectionInterface {
public function getOptionalFields();
}
class OptionalFieldCollection implements OptionalFieldCollectionInterface {
protected $optionalFields = [];
public function __construct(array $optionalFields = []) {
$this->optionalFields = $optionalFields;
}
public function getOptionalFields()
{
return $this->optionalFields;
}
}
class BillSearchResult implements OptionalFieldCollectionInterface {
protected $optionalFieldCollection;
public function __construct()
{
$this->optionalFieldCollection = new OptionalFieldCollection();
}
public function getOptionalFields()
{
return $this->optionalFieldCollection->getOptionalFields();
}
public function setOptionalFieldCollection(OptionalFieldCollectionInterface $collection)
{
$this->optionalFieldCollection = $collection;
}
public function getOptionalFieldCollection()
{
return $this->optionalFieldCollection;
}
}
class BillSearchResultCollection {
protected $results;
public function __construct(array $results)
{
$this->results = $results;
}
public function getResults()
{
return $this->results;
}
}
class NewestStatusOptionFieldDecorator implements OptionalFieldCollectionInterface
{
protected $wrappedCollection;
protected $newestStatusField;
public function __construct(OptionalFieldCollectionInterface $wrapped, OptionalField $newestStatusField)
{
$this->wrapped = $wrapped;
$this->newestStatusField = $newestStatusField;
}
public function getOptionalFields()
{
return array_merge(
$this->wrapped->getOptionalFields(),
[$this->newestStatusField]
);
}
}
class NewestSummaryOptionFieldDecorator implements OptionalFieldCollectionInterface
{
protected $wrappedCollection;
protected $newestSummaryField;
public function __construct(OptionalFieldCollectionInterface $wrapped, OptionalField $newestSummaryField)
{
$this->wrapped = $wrapped;
$this->newestSummaryField = $newestSummaryField;
}
public function getOptionalFields()
{
return array_merge(
$this->wrapped->getOptionalFields(),
[$this->newestSummaryField]
);
}
}
interface BillSearchResultDecoratorInterface
{
public function decorate(BillSearchResult $result);
public function decorateAll(BillSearchResultCollection $collection);
}
class BillSearchResultNewestStatusDecorator implements BillSearchResultDecoratorInterface
{
public function decorate(BillSearchResult $result)
{
$billStatus = 'Status!!'; // This class would actually be a service and pull this from the db
$result->setOptionalFieldCollection(
new NewestStatusOptionFieldDecorator(
$result->getOptionalFieldCollection(),
OptionalField::newestStatus($billStatus)
)
);
return $result;
}
public function decorateAll(BillSearchResultCollection $collection)
{
$results = $collection->getResults();
foreach ($results as $i => $result) {
$result->setOptionalFieldCollection(
new NewestStatusOptionFieldDecorator(
$result->getOptionalFieldCollection(),
OptionalField::newestStatus('Status '.$i)
)
);
}
return $collection;
}
}
class BillSearchResultNewestSummaryDecorator implements BillSearchResultDecoratorInterface
{
public function decorate(BillSearchResult $result)
{
$summary = 'Summary!!'; // This class would actually be a service and pull this from the db
$result->setOptionalFieldCollection(
new NewestSummaryOptionFieldDecorator(
$result->getOptionalFieldCollection(),
OptionalField::newestStatus($summary)
)
);
return $result;
}
public function decorateAll(BillSearchResultCollection $collection)
{
$results = $collection->getResults();
foreach ($results as $i => $result) {
$result->setOptionalFieldCollection(
new NewestSummaryOptionFieldDecorator(
$result->getOptionalFieldCollection(),
OptionalField::newestSummary('Summary '.$i)
)
);
}
return $collection;
}
}
// DEcorarting individual results
$billSearchResult = new BillSearchResult;
$decorators = [new BillSearchResultNewestStatusDecorator, new BillSearchResultNewestSummaryDecorator];
foreach ($decorators as $decorator) {
$billSearchResult = $decorator->decorate($billSearchResult);
}
var_dump($billSearchResult->getOptionalFields());
// $billSearchResult->setOptionalFieldCollection(
// new NewestSummaryOptionFieldDecorator(
// new NewestStatusOptionFieldDecorator(
// $billSearchResult->getOptionalFieldCollection(),
// OptionalField::newestStatus($billStatus)
// ),
// OptionalField::newestSummary($billSummary)
// )
// );
// Decoration a collection
$collection = new BillSearchResultCollection([new BillSearchResult, new BillSearchResult]);
foreach ($decorators as $decorator) {
$collection = $decorator->decorateAll($collection);
}
echo 'COLLECTIOn decorated';
foreach ($collection->getResults() as $result) {
var_dump($result->getOptionalFields());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment