Skip to content

Commit

Permalink
Fix NULL browser not allowed in builders
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Mar 18, 2022
1 parent 12f4e2b commit fda687d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.0.0-beta3] - 2022-03-18

### Fixed

- NULL browser not allowed in builders

## [1.0.0-beta2] - 2022-03-18

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function setCreator(Creator $creator): void
/**
* @inheritDoc
*/
public function setBrowser(Browser $browser): void
public function setBrowser(?Browser $browser): void
{
$this->browser = $browser;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public function build(): Log
$log = new Log(
version: $this->version,
creator: $this->creator ?? throw new RuntimeException('Missing "creator" field'),
browser: $this->browser ?? throw new RuntimeException('Missing "browser" field'),
browser: $this->browser,
pages: $this->pages,
entries: $this->entries,
comment: $this->comment,
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public function setCreator(Creator $creator): void;
/**
* Set browser.
*
* @param Browser $browser
* @param Browser|null $browser
*
* @return void
*/
public function setBrowser(Browser $browser): void;
public function setBrowser(?Browser $browser): void;

/**
* Add page.
Expand Down
23 changes: 14 additions & 9 deletions src/Builder/BuilderStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function reset(): void
throw new RuntimeException('Unable to truncate resource');
}

$defaultData = '{"log":{"version":1.2,"creator":null,"browser":null,"pages":[],"entries":[]}}';
$defaultData = '{"log":{"version":1.2,"creator":null,"pages":[],"entries":[]}}';

if (false === fwrite($this->fp, $defaultData)) {
throw new RuntimeException('Unable to write initial data on resource');
Expand All @@ -59,10 +59,10 @@ public function reset(): void
$this->positions = [
self::SECTION_VERSION => [18, 3],
self::SECTION_CREATOR => [32, 4],
self::SECTION_BROWSER => [47, 4],
self::SECTION_PAGES => [61, 0],
self::SECTION_ENTRIES => [74, 0],
self::SECTION_COMMENT => [75, 0],
self::SECTION_BROWSER => [36, 0],
self::SECTION_PAGES => [46, 0],
self::SECTION_ENTRIES => [59, 0],
self::SECTION_COMMENT => [60, 0],
];
}

Expand All @@ -85,7 +85,7 @@ protected function write(string $section, string $data, bool $concat, ?string $s
}

$data = ($separator ?? '') . $data;
$dataLength = $written= strlen($data);
$dataLength = $written = strlen($data);

if ($dataLength > 0) {
$written = b_fwritei(
Expand Down Expand Up @@ -149,9 +149,14 @@ public function setCreator(Creator $creator): void
/**
* @inheritDoc
*/
public function setBrowser(Browser $browser): void
public function setBrowser(?Browser $browser): void
{
$this->write(self::SECTION_BROWSER, json_encode($browser), false);
if (null === $browser) {
$this->write(self::SECTION_BROWSER, '', false);
return;
}

$this->write(self::SECTION_BROWSER, ',"browser":' . json_encode($browser), false);
}

/**
Expand Down Expand Up @@ -184,6 +189,6 @@ public function setComment(?string $comment): void
return;
}

$this->write(self::SECTION_COMMENT,',"comment":' . json_encode($comment), false);
$this->write(self::SECTION_COMMENT, ',"comment":' . json_encode($comment), false);
}
}

0 comments on commit fda687d

Please sign in to comment.