Skip to content

Commit

Permalink
Expands on #7 for numeric conversions of JSON
Browse files Browse the repository at this point in the history
This PR is linked to #7 providing the ability to actively enable
`JSON_NUMERIC_CONVERSION` while passively it is disabled.

Resolves #7
  • Loading branch information
zircote committed Apr 1, 2013
1 parent 7a1a74e commit 41df79b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
37 changes: 36 additions & 1 deletion library/Hal/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
*/
class Resource extends AbstractHal
{
/**
*
*/
const JSON_NUMERIC_CHECK_ON = true;
/**
*
*/
const JSON_NUMERIC_CHECK_OFF = false;

/**
* Internal storage of `Link` objects
* @var array
Expand All @@ -47,6 +56,11 @@ class Resource extends AbstractHal
protected $_embedded = array();

/**
* @var bool
*/
protected $jsonNumericCheck = self::JSON_NUMERIC_CHECK_OFF;
/**
*
* @param string $href
* @param array $data
* @param string|null $title
Expand Down Expand Up @@ -239,7 +253,10 @@ protected function _recurseLinks($links)
*/
public function __toJson()
{
return json_encode($this->toArray(), JSON_NUMERIC_CHECK);
if (defined(JSON_NUMERIC_CHECK) && $this->jsonNumericCheck) {
return json_encode($this->toArray(), JSON_NUMERIC_CHECK);
}
return json_encode($this->toArray());
}

/**
Expand Down Expand Up @@ -377,4 +394,22 @@ protected function _addLink(Link $link)

return $this;
}

/**
* Sets the ability to perform numeric to int conversion of the JSON output.
*
* <b>Example Usage:</b>
* <code>
* $hal->setJsonNumericCheck($jsonNumericCheck = self::JSON_NUMERIC_CHECK_OFF);
* $hal->setJsonNumericCheck($jsonNumericCheck = self::JSON_NUMERIC_CHECK_ON);
* </code>
*
* @param bool $jsonNumericCheck
* @return Resource
*/
public function setJsonNumericCheck($jsonNumericCheck = self::JSON_NUMERIC_CHECK_OFF)
{
$this->jsonNumericCheck = $jsonNumericCheck;
return $this;
}
}
45 changes: 45 additions & 0 deletions tests/library/Hal/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,49 @@ public function testEmptySelfLinkWithOthers()
json_decode($fixture), json_decode((string) $parent)
);
}
public function testJsonNumericConversion()
{
$fixture = <<<'EOF'
{
"_links":{
"search":{
"href":"/dogs?q={text}"
}
},
"data": 98765432
}
EOF;
$fixture1 = <<<'EOF'
{
"_links":{
"search":{
"href":"/dogs?q={text}"
}
},
"data": "98765432"
}
EOF;

$parent = new Resource('', array('data' => '98765432'));
$parent->setLink(new Link('/dogs?q={text}', 'search'));
// Default is off
$this->assertEquals(
json_decode($fixture1), json_decode((string) $parent)
);
$parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_ON);
// Active On
$this->assertEquals(
json_decode($fixture), json_decode((string) $parent)
);
$parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_OFF);
// Active Off
$this->assertEquals(
json_decode($fixture1), json_decode((string) $parent)
);
$parent->setJsonNumericCheck(Resource::JSON_NUMERIC_CHECK_ON);
// State Returned
$this->assertEquals(
json_decode($fixture), json_decode((string) $parent)
);
}
}

0 comments on commit 41df79b

Please sign in to comment.