Skip to content

Commit

Permalink
Add isJsonable() method
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Oct 22, 2016
1 parent a6b7e73 commit 7325e01
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Database/Model.php
Expand Up @@ -326,6 +326,16 @@ public static function fetched($callback)
static::registerModelEvent('fetched', $callback);
}

/**
* Checks if an attribute is jsonable or not.
*
* @return array
*/
public function isJsonable($key)
{
return in_array($key, $this->jsonable);
}

/**
* Get the jsonable attributes name
*
Expand Down Expand Up @@ -1257,7 +1267,7 @@ public function getAttributeValue($key)
* Return valid json (boolean, array) if valid, otherwise
* jsonable fields will return a string for invalid data.
*/
if (in_array($key, $this->jsonable) && !empty($attr)) {
if ($this->isJsonable($key) && !empty($attr)) {
$_attr = json_decode($attr, true);
if (json_last_error() === JSON_ERROR_NONE) {
$attr = $_attr;
Expand Down Expand Up @@ -1294,7 +1304,7 @@ public function setAttribute($key, $value)
$value = $_value;

// Handle jsonable
if (in_array($key, $this->jsonable) && (!empty($value) || is_array($value))) {
if ($this->isJsonable($key) && (!empty($value) || is_array($value))) {
$value = json_encode($value);
}

Expand Down

0 comments on commit 7325e01

Please sign in to comment.