From b8cc83690d777600ee607bbd7b518d7bc020a23f Mon Sep 17 00:00:00 2001 From: Bjorn Post Date: Mon, 25 Jul 2016 21:07:31 +0200 Subject: [PATCH] Add support for the native JSON column type as introduced in MySQL 5.7 --- lib/Pheasant/Types/JsonType.php | 48 ++++++++++++++++++++ tests/Pheasant/Tests/DomainObjectTest.php | 2 +- tests/Pheasant/Tests/Examples/Animal.php | 1 + tests/Pheasant/Tests/TypeMarshallingTest.php | 16 ++++++- tests/Pheasant/Tests/TypesTest.php | 6 +++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 lib/Pheasant/Types/JsonType.php diff --git a/lib/Pheasant/Types/JsonType.php b/lib/Pheasant/Types/JsonType.php new file mode 100644 index 0000000..6ef420f --- /dev/null +++ b/lib/Pheasant/Types/JsonType.php @@ -0,0 +1,48 @@ +columnSql($column, 'json', $this->options()); + } + + /* (non-phpdoc) + * @see \Pheasant\Type::unmarshal + */ + public function unmarshal($value) + { + $value = json_decode($value); + + if (json_last_error()) { + throw new Exception('Could not unmarshal json: ' . json_last_error_msg()); + } + + return $value; + } + + /* (non-phpdoc) + * @see \Pheasant\Type::marshal + */ + public function marshal($value) + { + $value = json_encode($value); + + if (json_last_error()) { + throw new Exception('Could not marshal json: ' . json_last_error_msg()); + } + + return $value; + } +} diff --git a/tests/Pheasant/Tests/DomainObjectTest.php b/tests/Pheasant/Tests/DomainObjectTest.php index e022933..c1a367d 100644 --- a/tests/Pheasant/Tests/DomainObjectTest.php +++ b/tests/Pheasant/Tests/DomainObjectTest.php @@ -54,7 +54,7 @@ public function testDefaultProperties() $animal = new Animal(); $this->assertEquals($animal->type, 'llama'); $this->assertEquals($animal->toArray(), - array('id'=>NULL, 'type'=>'llama', 'name'=>null)); + array('id' => NULL, 'type' => 'llama', 'name' => null, 'meta' => null)); $llama = new Animal(array('type'=>'llama')); $frog = new Animal(Array('type'=>'frog')); diff --git a/tests/Pheasant/Tests/Examples/Animal.php b/tests/Pheasant/Tests/Examples/Animal.php index dfa012c..63759d9 100644 --- a/tests/Pheasant/Tests/Examples/Animal.php +++ b/tests/Pheasant/Tests/Examples/Animal.php @@ -18,6 +18,7 @@ public static function initialize($builder, $pheasant) 'id' => new Types\IntegerType(11, 'primary auto_increment'), 'type' => new Types\StringType(255, 'required default=llama'), 'name' => new Types\StringType(255), + 'meta' => new Types\JsonType(), )); } diff --git a/tests/Pheasant/Tests/TypeMarshallingTest.php b/tests/Pheasant/Tests/TypeMarshallingTest.php index 7fbbf23..7ed26c9 100644 --- a/tests/Pheasant/Tests/TypeMarshallingTest.php +++ b/tests/Pheasant/Tests/TypeMarshallingTest.php @@ -130,7 +130,8 @@ public function testDecimalTypesAreMarshalledCorrectInLocale() setlocale(LC_ALL, $prevLocale); } - public function testVariableIsNullable() { + public function testVariableIsNullable() + { $object = new DomainObject; $object->weight = 88.5; // var type = double $object->weight = ''; // var type = string @@ -138,4 +139,17 @@ public function testVariableIsNullable() { $this->assertSame($object->weight, null); } + + public function testJsonTypesAreUnmarshalled() + { + $object = new Animal(array( + 'type' => 'Llama', + 'meta' => array('foo' => 'bar') + )); + $object->save(); + + $llamaById = Animal::byId(1); + $this->assertInstanceOf('stdClass', $llamaById->meta); + $this->assertSame($llamaById->meta->foo, 'bar'); + } } diff --git a/tests/Pheasant/Tests/TypesTest.php b/tests/Pheasant/Tests/TypesTest.php index 4d9452b..6314aae 100644 --- a/tests/Pheasant/Tests/TypesTest.php +++ b/tests/Pheasant/Tests/TypesTest.php @@ -107,4 +107,10 @@ public function assertMysqlColumnSql($sql, $type) { $this->assertEquals($type->columnSql('test', new \Pheasant\Database\MysqlPlatform()), $sql); } + + public function testJson() + { + $type = new Types\JsonType(); + $this->assertMysqlColumnSql('`test` json', $type); + } }