Skip to content

Instantly share code, notes, and snippets.

@assertchris
Created September 9, 2012 11:13
Show Gist options
  • Select an option

  • Save assertchris/3683845 to your computer and use it in GitHub Desktop.

Select an option

Save assertchris/3683845 to your computer and use it in GitHub Desktop.
<?php
class Collection implements IteratorAggregate, ArrayAccess, Countable
{
// ...
public function toJSON()
{
$return = array();
foreach ($this->_array as $item)
{
if (method_exists($item, "toJSON"))
{
$return[$key] = $value->toJSON();
}
else
{
$return[$key] = $value;
}
}
return json_encode($return);
}
// ...
}
class Item
{
public function toJSON()
{
$return = array();
foreach ($this as $key => $value)
{
if (method_exists($value, "toJSON"))
{
$return[$key] = $value->toJSON();
}
else
{
$return[$key] = $value;
}
}
return json_encode($return);
}
}
class Form extends Item
{
public function getFields()
{
if (get_class($this->_fields) !== "Collection")
{
$this->_fields = new Collection($this->_fields);
}
return $this->_fields;
}
// ...
public function __get($name)
{
if ($name === "fields")
{
return $this->getFields();
}
}
}
class Field extends Item
{
public function getInputs()
{
if (get_class($this->_inputs) !== "Collection")
{
$this->_inputs = new Collection($this->_inputs);
}
return $this->_inputs;
}
// ...
public function __get($name)
{
if ($name === "inputs")
{
return $this->getInputs();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment