Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- added *.swp files to be ignored

- added new ArraySerializer Serialization class
- changed JsonSerializer which extends ArraySerializer to dry things up
- added ArraySerializer tests
  • Loading branch information...
commit b669e413b198aceae03075988e5b9fcfd7338092 1 parent 318425f
Ashley McKenzie authored
View
1  .gitignore
@@ -3,3 +3,4 @@
.settings
*.log
test/*.db
+*.swp
View
14 lib/Model.php
@@ -1623,6 +1623,18 @@ public function to_csv(array $options=array())
}
/**
+ * Returns an Array representation of this model.
+ *
+ * @see Serialization
+ * @param array $options An array containing options for json serialization (see {@link Serialization} for valid options)
+ * @return array Array representation of the model
+ */
+ public function to_array(array $options=array())
+ {
+ return $this->serialize('Array', $options);
+ }
+
+ /**
* Creates a serializer based on pre-defined to_serializer()
*
* An options array can take the following parameters:
@@ -1635,7 +1647,7 @@ public function to_csv(array $options=array())
* <li><b>include:</b> a string or array of associated models to include in the final serialized product.</li>
* </ul>
*
- * @param string $type Either Xml or Json
+ * @param string $type Either Xml, Json or Array
* @param array $options Options array for the serializer
* @return string Serialized representation of the model
*/
View
20 lib/Serialization.php
@@ -240,17 +240,33 @@ private function check_include()
};
/**
+ * Array serializer.
+ *
+ * @package ActiveRecord
+ */
+class ArraySerializer extends Serialization
+{
+ public static $include_root = false;
+
+ public function to_s()
+ {
+ return self::$include_root ? array(strtolower(get_class($this->model)) => $this->to_a()) : $this->to_a();
+ }
+}
+
+/**
* JSON serializer.
*
* @package ActiveRecord
*/
-class JsonSerializer extends Serialization
+class JsonSerializer extends ArraySerializer
{
public static $include_root = false;
public function to_s()
{
- return json_encode(self::$include_root ? array(strtolower(get_class($this->model)) => $this->to_a()) : $this->to_a());
+ parent::$include_root = self::$include_root;
+ return json_encode(parent::to_s());
}
}
View
26 test/SerializationTest.php
@@ -9,6 +9,7 @@ class SerializationTest extends DatabaseTest
public function tear_down()
{
parent::tear_down();
+ ActiveRecord\ArraySerializer::$include_root = false;
ActiveRecord\JsonSerializer::$include_root = false;
}
@@ -123,6 +124,31 @@ public function test_to_xml()
$this->assert_equals($book->attributes(),get_object_vars(new SimpleXMLElement($book->to_xml())));
}
+ public function test_to_array()
+ {
+ $book = Book::find(1);
+ $array = $book->to_array();
+ $this->assert_equals($book->attributes(), $array);
+ }
+
+ public function test_to_array_include_root()
+ {
+ ActiveRecord\ArraySerializer::$include_root = true;
+ $book = Book::find(1);
+ $array = $book->to_array();
+ $book_attributes = array('book' => $book->attributes());
+ $this->assert_equals($book_attributes, $array);
+ }
+
+ public function test_to_array_except()
+ {
+ $book = Book::find(1);
+ $array = $book->to_array(array('except' => array('special')));
+ $book_attributes = $book->attributes();
+ unset($book_attributes['special']);
+ $this->assert_equals($book_attributes, $array);
+ }
+
public function test_works_with_datetime()
{
Author::find(1)->update_attribute('created_at',new DateTime());
Please sign in to comment.
Something went wrong with that request. Please try again.