From 06a23a936cc58e1b489719e45067ff5b614a5c4a Mon Sep 17 00:00:00 2001 From: Jamiel <20553479+jamielsharief@users.noreply.github.com> Date: Sat, 31 Oct 2020 10:53:09 +0100 Subject: [PATCH] Added pretty print option --- demo.php | 7 ++++--- src/Block.php | 12 ++++++++++-- src/Blockchain.php | 8 ++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/demo.php b/demo.php index 01ee772..11df1a8 100644 --- a/demo.php +++ b/demo.php @@ -24,7 +24,8 @@ // Create the instance $blockchain = new Blockchain('demo-coin', $path, [ 'difficulty' => $difficulty, - 'lookback' => $lookback + 'lookback' => $lookback, + 'pretty' => true ]); // Insert Blocks @@ -48,13 +49,13 @@ print("\nLast Block:\n"); $latest = $blockchain->last(); -print($latest->toJson()); +print($latest->toJson(['pretty' => true])); print("\nGoing through all Blocks..\n"); foreach ($blockchain->all(['reverse' => false]) as $block) { print("Block #{$block->index}\n"); - print($block->toJson() . "\n"); + print($block->toJson(['pretty' => true]) . "\n"); } print("\nThere are {$blockchain->count()} Blocks in the Blockchain\n"); diff --git a/src/Block.php b/src/Block.php index 20bdbe7..065f7d8 100644 --- a/src/Block.php +++ b/src/Block.php @@ -171,11 +171,19 @@ private function hashTransactions(array $transactions): array /** * Returns the Block as a JSON string * + * @param array $options The following options keys are supported: + * - pretty: default:false toggle JSON pretty print * @return string */ - public function toJson(): string + public function toJson(array $options = []): string { - return json_encode($this->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + $options += ['pretty' => false]; + $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES; + if ($options['pretty']) { + $jsonOptions |= JSON_PRETTY_PRINT; + } + + return json_encode($this->toArray(), $jsonOptions); } /** diff --git a/src/Blockchain.php b/src/Blockchain.php index 4619662..1abcc86 100644 --- a/src/Blockchain.php +++ b/src/Blockchain.php @@ -37,6 +37,8 @@ class Blockchain */ protected bool $exists = false; + protected bool $pretty; + /** * @param string $name the name of the Blockchain lower case letters numbers, dashes. * @param string $path Path to the folder where the Blockchain is stored. @@ -44,10 +46,11 @@ class Blockchain * - difficulty: difficulty level for proof of work, set to 0 to disable * - version: the version number of the Blockchain * - lookback: the number of Blocks to lookback at to see if there are duplicate Transactions. + * - pretty: default:false. Writes Blocks using JSON pretty print */ public function __construct(string $name, string $path, array $options = []) { - $options += ['difficulty' => 8, 'version' => 1,'lookback' => 25]; + $options += ['difficulty' => 8, 'version' => 1,'lookback' => 25,'pretty' => false]; if (! preg_match('/^[a-z0-9-]+$/i', $name)) { throw new InvalidArgumentException('Invalid Blockchain name'); @@ -63,6 +66,7 @@ public function __construct(string $name, string $path, array $options = []) $this->lookback = $options['lookback']; $this->exists = is_dir($this->path); + $this->pretty = $options['pretty']; } /** @@ -339,7 +343,7 @@ protected function writeBlock(Block $block): void { $path = $this->blockPath($block->index); - if (! $this->fs->write($path, $block->toJson(), true)) { + if (! $this->fs->write($path, $block->toJson(['pretty' => $this->pretty]), true)) { throw new BlockchainException('Error writing Block'); } $this->index->add($block);