Created
September 9, 2012 11:13
-
-
Save assertchris/3683845 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| 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