Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Add support for the native JSON column type as introduced in MySQL 5.7 #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/Pheasant/Types/JsonType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Pheasant\Types;

use Pheasant\Exception;

/**
* A JSON type
* @see https://dev.mysql.com/doc/refman/5.7/en/json.html
*/
class JsonType extends BaseType
{
/* (non-phpdoc)
* @see \Pheasant\Type::columnSql
*/
public function columnSql($column, $platform)
{
return $platform->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;
}
}
2 changes: 1 addition & 1 deletion tests/Pheasant/Tests/DomainObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
1 change: 1 addition & 0 deletions tests/Pheasant/Tests/Examples/Animal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}

Expand Down
16 changes: 15 additions & 1 deletion tests/Pheasant/Tests/TypeMarshallingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,26 @@ 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
$object->weight = null;

$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');
}
}
6 changes: 6 additions & 0 deletions tests/Pheasant/Tests/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}