Skip to content

Commit

Permalink
Added pretty print option
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Oct 31, 2020
1 parent 5846091 commit 06a23a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 4 additions & 3 deletions demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
// Create the instance
$blockchain = new Blockchain('demo-coin', $path, [
'difficulty' => $difficulty,
'lookback' => $lookback
'lookback' => $lookback,
'pretty' => true
]);

// Insert Blocks
Expand All @@ -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");
Expand Down
12 changes: 10 additions & 2 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Blockchain.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ 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.
* @param array $options The following options keys are supported
* - 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');
Expand All @@ -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'];
}

/**
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 06a23a9

Please sign in to comment.