Skip to content

Commit

Permalink
fixed bug with options without values
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Isler committed Sep 20, 2016
1 parent 97555ef commit d1cb1a4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

### Fixed

## [2.0.2] - 2016-09-20
### Fixed
- Fixed a bug with options without values

## [2.0.1] - 2016-05-19
### Fixed
- Fixed a critical bug
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ $input->setUrl('https://www.google.com');

$converter = new Converter($input, new DownloadOutput());

$converter->setOption('n');
$converter->setOption('d', '300');

$converter->setOptions([
'no-background',
'margin-bottom' => '100',
'margin-top' => '100',
]);
Expand Down
11 changes: 9 additions & 2 deletions src/Spiritix/HtmlToPdf/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ public function setOptions($options)
}

foreach ($options as $key => $value) {

// Convert key-only options to regular ones
if (is_numeric($key) && !empty($value)) {
$key = $value;
$value = '';
}

$this->setOption($key, $value);
}

Expand Down Expand Up @@ -225,9 +232,9 @@ private function buildCommand()
}

$key = escapeshellcmd(trim($key));
$value = escapeshellarg(trim($value));
$value = trim($value);

$optionsString .= $key . ' ' . $value . ' ';
$optionsString .= $key . (empty($value) ? '' : ' ' . escapeshellarg($value)) . ' ';
}

$command = $this->getBinaryPath() . ' ' . $optionsString . ' - -';
Expand Down
7 changes: 7 additions & 0 deletions tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ public function setUp()

public function testOptions()
{
$this->converter->setOption('n');
$this->converter->setOption('R', '500');
$this->converter->setOption('margin-top', '100');

$this->converter->setOptions([
'ignore-load-errors',
'B' => '50',
'margin-left' => '10',
]);

$value = $this->converter->getOption('n');
$this->assertEquals('', $value);

$value = $this->converter->getOption('R');
$this->assertEquals('500', $value);

Expand All @@ -41,8 +46,10 @@ public function testOptions()

$options = $this->converter->getOptions();
$this->assertEquals([
'n' => '',
'R' => '500',
'margin-top' => '100',
'ignore-load-errors' => '',
'B' => '50',
'margin-left' => '10',
], $options);
Expand Down

0 comments on commit d1cb1a4

Please sign in to comment.