Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
AsciiHex
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 decode
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Pdf\Extract\Filter;
16
17/**
18 * Pdf extract ascii hex filter class
19 *
20 * @category   Pop
21 * @package    Pop\Pdf
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    6.0.0
26 */
27class AsciiHex implements FilterInterface
28{
29
30    /**
31     * Decode an ASCIIHexDecode stream
32     *
33     * @param  string $data
34     * @param  array  $params
35     * @return string
36     */
37    public function decode(string $data, array $params = []): string
38    {
39        $data = rtrim($data);
40
41        if (str_ends_with($data, '>')) {
42            $data = substr($data, 0, -1);
43        }
44
45        $hex = preg_replace('/[^0-9A-Fa-f]/', '', $data);
46
47        if ((strlen($hex) % 2) !== 0) {
48            $hex .= '0';
49        }
50
51        return ($hex === '') ? '' : (string) hex2bin($hex);
52    }
53
54}