Skip to content

Commit

Permalink
fix json function
Browse files Browse the repository at this point in the history
  • Loading branch information
gfazioli committed Mar 6, 2017
1 parent 64a4856 commit af981f8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ With this Service Provider, you can draw amazing char by usign [Morris.js]( http
## Requirements

* PHP 5.5.9 or greater
* Laravel 5.2 or greater
* Laravel 5.4 or greater

## Installation

Expand Down Expand Up @@ -64,6 +64,7 @@ echo Morris::area( 'morris-area' )
->xkey( [ 'y' ] )
->ykeys( [ 'a', 'b' ] )
->labels( [ 'Series A', 'Series B' ] )
->hoverCallback( 'function(index, options, content){var row = options.data[index];return "sin(" + row.x + ") = " + row.y;}' )
->data( [
[ "y" => '2006', "a" => 100, "b" => 90 ],
[ "y" => '2007', "a" => 75, "b" => 65 ],
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gfazioli/laravel-morris-php",
"version": "1.0.3",
"version": "1.0.4",
"homepage": "https://github.com/gfazioli/laravel-morris-php",
"license": "MIT",
"type": "library",
Expand Down
27 changes: 26 additions & 1 deletion src/Morris/MorrisBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class MorrisBase
*/
protected $data = [];

protected $functions = [
'hoverCallback',
'formatter',
'dateFormat'
];

/**
* Create an instance of Morris class
*
Expand Down Expand Up @@ -83,6 +89,11 @@ public function toArray()
if ( '__' == substr( $property, 0, 2 ) || '' === $value || is_null( $value ) || ( is_array( $value ) && empty( $value ) ) ) {
continue;
}

if ( in_array( $property, $this->functions ) && substr( $value, 0, 8 ) == 'function' ) {
$value = "%{$property}%";
}

$return[ $property ] = $value;
}

Expand All @@ -98,7 +109,21 @@ public function toArray()
*/
public function toJSON()
{
return json_encode( $this->toArray() );
$json = json_encode( $this->toArray() );

return str_replace(
[
'"%hoverCallback%"',
'"%formatter%"',
'"%dateFormat%"',
],
[
$this->hoverCallback,
$this->formatter,
$this->dateFormat,
],
$json
);
}

/**
Expand Down

0 comments on commit af981f8

Please sign in to comment.