diff --git a/flight/util/Collection.php b/flight/util/Collection.php index 196ea66..1062b11 100644 --- a/flight/util/Collection.php +++ b/flight/util/Collection.php @@ -8,11 +8,15 @@ namespace flight\util; +if (!interface_exists('JsonSerializable')) { + require_once dirname(__FILE__) . '/LegacyJsonSerializable.php'; +} + /** * The Collection class allows you to access a set of data * using both array and object notation. */ -class Collection implements \ArrayAccess, \Iterator, \Countable { +class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializable { /** * Collection data. * @@ -194,6 +198,15 @@ class Collection implements \ArrayAccess, \Iterator, \Countable { $this->data = $data; } + /** + * Gets the collection data which can be serialized to JSON + * + * @return array Collection data which can be serialized by json_encode + */ + public function jsonSerialize() { + return $this->data; + } + /** * Removes all items from the collection. */ diff --git a/flight/util/LegacyJsonSerializable.php b/flight/util/LegacyJsonSerializable.php new file mode 100644 index 0000000..4c907c7 --- /dev/null +++ b/flight/util/LegacyJsonSerializable.php @@ -0,0 +1,11 @@ + + * @license MIT, http://flightphp.com/license + */ + +interface JsonSerializable { + public function jsonSerialize(); +} \ No newline at end of file