Skip to content

Commit

Permalink
add toArray and toJson method
Browse files Browse the repository at this point in the history
  • Loading branch information
klgd committed Jun 12, 2017
1 parent 84a7ae7 commit d436868
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/HasAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function getAttribute($key = null, $default = null)
}

/**
* Determine if the given key exists in the attributes.
*
* @param string|int $key
*
* @return bool
Expand All @@ -50,6 +52,8 @@ public function existsAttribute($key)
}

/**
* Check if an key or keys exist in the attributes using "dot" notation.
*
* @param string|array $keys
*
* @return bool
Expand All @@ -58,4 +62,35 @@ public function hasAttribute($keys)
{
return Arr::has($this->attributes, $keys);
}

/**
* Convert the Object instance to an array.
*
* @return array
*/
public function toArray()
{
return $this->attributes;
}

/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}

/**
* Convert the Object instance to JSON.
*
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}
}

0 comments on commit d436868

Please sign in to comment.