Merge pull request #395 from merkuriy/JsonSerializableCollection

Added JsonSerializable for util/Collection
pull/434/head v1.3.9
Mike Cao 4 years ago committed by GitHub
commit bc5e03ca59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 <b>json_encode</b>
*/
public function jsonSerialize() {
return $this->data;
}
/**
* Removes all items from the collection.
*/

@ -0,0 +1,11 @@
<?php
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
interface JsonSerializable {
public function jsonSerialize();
}
Loading…
Cancel
Save