Page MenuHomePhabricator

D628.diff
No OneTemporary

D628.diff

diff --git a/Model.php b/Model.php
--- a/Model.php
+++ b/Model.php
@@ -1,5 +1,6 @@
<?php namespace spitfire;
+use JsonSerializable;
use Serializable;
use spitfire\collection\Collection;
use spitfire\exceptions\PrivateException;
@@ -15,7 +16,7 @@
* @todo Make this class implement Iterator
* @author César de la Cal <cesar@magic3w.com>
*/
-abstract class Model implements Serializable
+abstract class Model implements Serializable, JsonSerializable
{
/**
@@ -302,6 +303,30 @@
return $this->new;
}
+ /**
+ * The jsonserialize endpoint allows applications to json_encode this model without
+ * having to loop over all the keys.
+ *
+ * If your application wishes to not expose certain data to the outside world, feel
+ * free to implement jsonSerialize in your model and unset the keys you do not wish
+ * to broadcast.
+ *
+ * This method is intended to aid passing data to views and rendering output, the data
+ * exported here cannot be imported back into spitfire.
+ *
+ * @return array
+ */
+ public function jsonSerialize()
+ {
+ $raw = [];
+
+ foreach ($this->data as $name => $adapter) {
+ $raw[$name] = $adapter->usrGetData();
+ }
+
+ return $raw;
+ }
+
/**
* Allows the model to perform small tasks before it is written to the database.
*
diff --git a/model/model.php b/model/model.php
deleted file mode 100644
--- a/model/model.php
+++ /dev/null
@@ -1,18 +0,0 @@
-<?php
-
-trigger_error('Using the deprecated schema in model.php. Please use storage\database\Schema', E_USER_DEPRECATED);
-
-/**
- * A Model is a class used to define how Spitfire stores data into a DBMS. We
- * usually consider a DBMS as relational database engine, but Spitfire can
- * be connected to virtually any engine that stores data. Including No-SQL
- * databases and directly on the file system. You should even be able to use
- * tapes, although that would be extra slow.
- *
- * Every model contains fields and references. Fields are direct data-types, they
- * allow storing things directly into them, while references are pointers to
- * other models allowing you to store more complex data into them.
- *
- * @author César de la Cal <cesar@magic3w.com>
- */
-class Schema extends \spitfire\storage\database\Schema {}

File Metadata

Mime Type
text/plain
Expires
Apr 11 2021, 4:24 PM (9 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
6153
Default Alt Text
D628.diff (2 KB)

Event Timeline