Skip to content

Commit d4c41ec

Browse files
committed
refactor: redesign whhtmltopdf extra options and add tests
1 parent fcfeac1 commit d4c41ec

File tree

98 files changed

+1558
-935
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1558
-935
lines changed

src/Backend/WkHtmlToPdf/ExtraOption.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@
44

55
namespace KNPLabs\Snappy\Backend\WkHtmlToPdf;
66

7-
interface ExtraOption
7+
/**
8+
* @see https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
9+
*/
10+
abstract class ExtraOption
811
{
9-
public function isRepeatable(): bool;
12+
/**
13+
* @param non-empty-array<string> $command
14+
*/
15+
public function __construct(private readonly bool $repeatable, private readonly array $command) {}
1016

11-
/** @return array<float|int|string> */
12-
public function compile(): array;
17+
final public function isRepeatable(): bool
18+
{
19+
return $this->repeatable;
20+
}
21+
22+
/**
23+
* @return non-empty-array<string>
24+
*/
25+
final public function getCommand(): array
26+
{
27+
return $this->command;
28+
}
1329
}

src/Backend/WkHtmlToPdf/ExtraOption/Allow.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class Allow implements ExtraOption
9+
/**
10+
* Allow the file or files from the specified folder to be loaded.
11+
*/
12+
final class Allow extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return true;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1918
{
20-
return ['--allow', $this->path];
19+
parent::__construct(
20+
repeatable: true,
21+
command: ['--allow', $path]
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/BypassProxyFor.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class BypassProxyFor implements ExtraOption
9+
/**
10+
* Bypass proxy for host.
11+
*/
12+
final class BypassProxyFor extends ExtraOption
1013
{
11-
public function __construct(private readonly string $value) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return true;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $value
16+
*/
17+
public function __construct(string $value)
1918
{
20-
return ['--bypass-proxy-for', $this->value];
19+
parent::__construct(
20+
repeatable: true,
21+
command: ['--bypass-proxy-for', $value],
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/CacheDir.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CacheDir implements ExtraOption
9+
/**
10+
* Web cache directory.
11+
*/
12+
final class CacheDir extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return false;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1918
{
20-
return ['--cache-dir', $this->path];
19+
parent::__construct(
20+
repeatable: false,
21+
command: ['--cache-dir', $path],
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/CheckBoxSvg.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CheckBoxSvg implements ExtraOption
9+
/**
10+
* Use this SVG file when rendering unchecked checkboxes.
11+
*/
12+
final class CheckBoxSvg extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return false;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1918
{
20-
return ['--checkbox-svg', $this->path];
19+
parent::__construct(
20+
repeatable: false,
21+
command: ['--checkbox-svg', $path],
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/CheckboxCheckedSvg.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CheckboxCheckedSvg implements ExtraOption
9+
/**
10+
* Use this SVG file when rendering checked checkboxes.
11+
*/
12+
final class CheckboxCheckedSvg extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return false;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1918
{
20-
return ['--checkbox-checked-svg', $this->path];
19+
parent::__construct(
20+
repeatable: false,
21+
command: ['--checkbox-checked-svg', $path],
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/Cookie.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class Cookie implements ExtraOption
9+
/**
10+
* Set an additional cookie.
11+
*/
12+
final class Cookie extends ExtraOption
1013
{
11-
public function __construct(private readonly string $name, private readonly string $value) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return true;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $name
16+
*/
17+
public function __construct(string $name, string $value)
1918
{
20-
return ['--cookie', $this->name, $this->value];
19+
parent::__construct(
20+
repeatable: true,
21+
command: ['--cookie', $name, urlencode($value)],
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/CookieJar.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
final class CookieJar implements ExtraOption
9+
/**
10+
* Read and write cookies from and to the supplied cookie jar file.
11+
*/
12+
final class CookieJar extends ExtraOption
1013
{
11-
public function __construct(public readonly string $path) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return false;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1918
{
20-
return ['--no-collate'];
19+
parent::__construct(
20+
repeatable: false,
21+
command: ['--cookie-jar', $path],
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/Copies.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
final class Copies implements ExtraOption
9+
/**
10+
* Number of copies to print into the pdf file.
11+
*
12+
* Default: 1
13+
*/
14+
final class Copies extends ExtraOption
1015
{
1116
/**
1217
* @param positive-int $number
1318
*/
14-
public function __construct(private readonly int $number) {}
15-
16-
public function isRepeatable(): bool
17-
{
18-
return false;
19-
}
20-
21-
public function compile(): array
19+
public function __construct(int $number)
2220
{
23-
return ['--copies', $this->number];
21+
parent::__construct(
22+
repeatable: false,
23+
command: ['--copies', (string) $number],
24+
);
2425
}
2526
}

src/Backend/WkHtmlToPdf/ExtraOption/CustomHeader.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CustomHeader implements ExtraOption
9+
/**
10+
* Set an additional HTTP header.
11+
*/
12+
final class CustomHeader extends ExtraOption
1013
{
11-
public function __construct(private readonly string $name, private readonly string $value) {}
12-
13-
public function isRepeatable(): bool
14-
{
15-
return true;
16-
}
17-
18-
public function compile(): array
14+
/**
15+
* @param non-empty-string $name
16+
*/
17+
public function __construct(string $name, string $value)
1918
{
20-
return ['--custom-header', $this->name, $this->value];
19+
parent::__construct(
20+
repeatable: true,
21+
command: ['--custom-header', $name, $value],
22+
);
2123
}
2224
}

src/Backend/WkHtmlToPdf/ExtraOption/CustomHeaderPropagation.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CustomHeaderPropagation implements ExtraOption
9+
/**
10+
* Add HTTP headers specified by CustomHeader for each resource request.
11+
*/
12+
final class CustomHeaderPropagation extends ExtraOption
1013
{
11-
public function isRepeatable(): bool
14+
public function __construct()
1215
{
13-
return false;
14-
}
15-
16-
public function compile(): array
17-
{
18-
return ['--custom-header-propagation'];
16+
parent::__construct(
17+
repeatable: false,
18+
command: ['--custom-header-propagation'],
19+
);
1920
}
2021
}

src/Backend/WkHtmlToPdf/ExtraOption/DefaultHeader.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class DefaultHeader implements ExtraOption
9+
/**
10+
* Add a default header, with the name of the page to the left, and the page number to the right.
11+
*/
12+
final class DefaultHeader extends ExtraOption
1013
{
11-
public function isRepeatable(): bool
14+
public function __construct()
1215
{
13-
return false;
14-
}
15-
16-
public function compile(): array
17-
{
18-
return ['--default-header'];
16+
parent::__construct(
17+
repeatable: false,
18+
command: ['--default-header'],
19+
);
1920
}
2021
}

src/Backend/WkHtmlToPdf/ExtraOption/DisableDottedLines.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
final class DisableDottedLines implements ExtraOption
9+
/**
10+
* Do not use dotted lines in the toc.
11+
*/
12+
final class DisableDottedLines extends ExtraOption
1013
{
11-
public function isRepeatable(): bool
14+
public function __construct()
1215
{
13-
return false;
14-
}
15-
16-
public function compile(): array
17-
{
18-
return ['--disable-dotted-lines'];
16+
parent::__construct(
17+
repeatable: false,
18+
command: ['--disable-dotted-lines'],
19+
);
1920
}
2021
}

0 commit comments

Comments
 (0)