From af981f80ebe93ed40178ccb9101c097a6ca54e1c Mon Sep 17 00:00:00 2001 From: Giovambattista Fazioli Date: Mon, 6 Mar 2017 13:46:57 +0100 Subject: [PATCH] fix json function --- README.md | 3 ++- composer.json | 2 +- src/Morris/MorrisBase.php | 27 ++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ceeecf2..22bf354 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ], diff --git a/composer.json b/composer.json index c4be31f..0b702a7 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Morris/MorrisBase.php b/src/Morris/MorrisBase.php index 675f25a..f5a3780 100644 --- a/src/Morris/MorrisBase.php +++ b/src/Morris/MorrisBase.php @@ -53,6 +53,12 @@ class MorrisBase */ protected $data = []; + protected $functions = [ + 'hoverCallback', + 'formatter', + 'dateFormat' + ]; + /** * Create an instance of Morris class * @@ -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; } @@ -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 + ); } /**