Skip to content

Commit

Permalink
Updating to not json encode empty objects as [] unless you allow them.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Wyke committed Dec 7, 2015
1 parent 0bb97d6 commit 678e979
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Synthesize.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ public function jsonSerialize(){

foreach($this as $strKey => $objValue){
if($this->options($strKey)->json){
if(!is_null($objValue->jsonSerialize()) || $this->options($strKey)->jsonnull){
$arrData[$strKey] = $objValue->jsonSerialize();
$mixValue = $objValue->jsonSerialize();
$mixJSON = json_encode($mixValue);
if((!is_null($mixValue) && $mixJSON!='[]') || $this->options($strKey)->jsonnull){
$arrData[$strKey] = $mixValue;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Synthesizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public function __call($strName, Array $arrArguments){
* @return mixed
*/
public function __get($strProperty){
if($this->getSynthesize()->hasProperty($strProperty)){
if(property_exists($this, $strProperty)){
return $this->{$strProperty};
}else if($this->getSynthesize()->hasProperty($strProperty)){
return $this->getSynthesize()->asValue($strProperty);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Type/Fixtures/EmptyObjectFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Frozensheep\Synthesize\Tests\Type\Fixtures;

use Frozensheep\Synthesize\Synthesizer;

class EmptyObjectFixture implements \JsonSerializable {

//include the Sythesizer trait
use Synthesizer;

//set the synthesized variables
protected $arrSynthesize = array(
'int' => array('type' => 'int'),
'int1' => array('type' => 'int')
);
}
2 changes: 2 additions & 0 deletions tests/Type/Fixtures/ObjectFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Frozensheep\Synthesize\Tests\Type\Fixtures;

use Frozensheep\Synthesize\Synthesizer;
use Frozensheep\Synthesize\Tests\Type\Fixtures\EmptyObjectFixture;

class ObjectFixture implements \JsonSerializable {

Expand All @@ -19,5 +20,6 @@ class ObjectFixture implements \JsonSerializable {
'object5' => array('type' => 'object', 'class' => '\DateTime', 'json' => false),
'object6' => array('type' => 'object', 'class' => '\DateTime'),
'object7' => array('type' => 'object', 'class' => '\DateTime', 'autoinit' => false),
'object8' => array('type' => 'object', 'class' => 'Frozensheep\Synthesize\Tests\Type\Fixtures\EmptyObjectFixture')
);
}
29 changes: 29 additions & 0 deletions tests/Type/SynthesizeOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Frozensheep\Synthesize\Tests\Type;

use Frozensheep\Synthesize\Type\SynthesizeOption;

class SynthesizeOptionTest extends \PHPUnit_Framework_TestCase {

protected $objSynthesizeOption;

protected function setUp(){
$this->objSynthesizeOption = new SynthesizeOption();
}

public function testAllowedProperties(){
$arrProperties = array_flip($this->objSynthesizeOption->keys());
$this->assertArrayHasKey('type', $arrProperties);
$this->assertArrayHasKey('default', $arrProperties);
$this->assertArrayHasKey('min', $arrProperties);
$this->assertArrayHasKey('max', $arrProperties);
$this->assertArrayHasKey('format', $arrProperties);
$this->assertArrayHasKey('class', $arrProperties);
$this->assertArrayHasKey('json', $arrProperties);
$this->assertArrayHasKey('jsonnull', $arrProperties);
$this->assertArrayHasKey('autoinit', $arrProperties);

$this->assertEquals(array('type','default','min','max','format','class','json','jsonnull','autoinit'), $this->objSynthesizeOption->keys());
}
}

0 comments on commit 678e979

Please sign in to comment.