<commit_before><?php

namespace App\CHD;

use Exception;

class Signatures
{
    public function get($petition, $page = 1, $init = true)
    {
        if (is_null($page)) {
            $page = 1;
        }
        $data = app('Request')->get(
            app('Path')->get(
                'PetitionSignatureList/p=petition_id='.$petition,
                '?sortDirection=ASC&pageNumber='.$page
            )
        );

        $signatures = [];

        $signaturePattern = '/<tr class="table_column_content">(?:\ )?(?P<signature>.*?)(?:\ )?<\/tr>/';
        if (preg_match_all($signaturePattern, $data, $signature)) {
            $signature = $signature['signature'];

            $signatures = $this->handlePageSignatures($signature, $page);

            if ($init) {
                $paginationPathPattern = '/for="pageNumber"[^\/]*\/\s*(?P<lastPage>\d+)/';

                if (preg_match($paginationPathPattern, $data, $lastPage)) {
                    $lastPage = $lastPage['lastPage'];

                    for ($page = $page + 1; $page <= $lastPage; ++$page) {
                        $signatures = array_merge($signatures, $this->get($petition, $page, false));
                    }
                }
            }
        }

        return $signatures;
    }

    public function handlePageSignatures($signaturesRaw, $page)
    {
        $signatures = [];

        foreach ($signaturesRaw as $index_on_page => $signature) {
            $signatureMetaPattern  = '/(?:(?:<td[^>]*)>(?P<data>.[^<]*))<\/td>/';
            if (!preg_match_all($signatureMetaPattern, $signature, $signatureMeta)) {
                throw new Exception('couldn\'t find any signature data');
            }

            switch (count($signatureMeta['data'])) {
                case 1:
                    $signatureMeta = [
                        'lastname'      => null,
                        'firstname'     => null,
                        'city'          => null,
                        'postcode'      => null,
                        'page_number'   => $page,
                        'index_on_page' => $index_on_page,
                    ];
                    break;
                case 4:
                    $signatureMeta = [
                        'lastname'      => trim($signatureMeta['data'][0]),
                        'firstname'     => trim($signatureMeta['data'][1]),
                        'city'          => trim($signatureMeta['data'][2]),
                        'postcode'      => trim(strtolower($signatureMeta['data'][3]), 'l- '),
                        'page_number'   => $page,
                        'index_on_page' => $index_on_page,
                    ];
                    break;
                default:
                    throw new Exception('unknown amount of signature details'.print_r($signatureMeta, true).print_r($signature, true));

            }

            $signatures[] = $signatureMeta;
        }

        return $signatures;
    }
}<commit_msg>fix for special characters in signature fields<commit_after><?php

namespace App\CHD;

use Exception;

class Signatures
{
    public function get($petition, $page = 1, $init = true)
    {
        if (is_null($page)) {
            $page = 1;
        }
        $data = app('Request')->get(
            app('Path')->get(
                'PetitionSignatureList/p=petition_id='.$petition,
                '?sortDirection=ASC&pageNumber='.$page
            )
        );

        $signatures = [];

        $signaturePattern = '/<tr class="table_column_content">(?:\ )?(?P<signature>.*?)(?:\ )?<\/tr>/';
        if (preg_match_all($signaturePattern, $data, $signature)) {
            $signature = $signature['signature'];

            $signatures = $this->handlePageSignatures($signature, $page);

            if ($init) {
                $paginationPathPattern = '/for="pageNumber"[^\/]*\/\s*(?P<lastPage>\d+)/';

                if (preg_match($paginationPathPattern, $data, $lastPage)) {
                    $lastPage = $lastPage['lastPage'];

                    for ($page = $page + 1; $page <= $lastPage; ++$page) {
                        $signatures = array_merge($signatures, $this->get($petition, $page, false));
                    }
                }
            }
        }

        return $signatures;
    }

    public function handlePageSignatures($signaturesRaw, $page)
    {
        $signatures = [];

        foreach ($signaturesRaw as $index_on_page => $signature) {
            $data = [];

            $columns = explode('</td> <td', $signature);

            $signatureMetaPattern = '/(?:.*[^>])>(?P<value>.[^<]*)/';

            switch (count($columns)) {
                case 1:
                    $signatureMeta = [
                        'lastname'      => null,
                        'firstname'     => null,
                        'city'          => null,
                        'postcode'      => null,
                        'page_number'   => $page,
                        'index_on_page' => $index_on_page,
                    ];
                    break;
                case 4:
                    foreach ($columns as $key => $column) {
                        preg_match($signatureMetaPattern, $column, $signatureMeta);
                        $data[$key] = $signatureMeta['value'];
                    }

                    $signatureMeta = [
                        'lastname'      => trim($data[0]),
                        'firstname'     => trim($data[1]),
                        'city'          => trim($data[2]),
                        'postcode'      => trim(strtolower($data[3]), 'l- '),
                        'page_number'   => $page,
                        'index_on_page' => $index_on_page,
                    ];
                    break;
                default:
                    throw new Exception('unknown amount of signature details'.print_r($signatureMeta, true).print_r($signature, true));

            }

            $signatures[] = $signatureMeta;
        }

        return $signatures;
    }
}
{'fraction_non_alphanumeric': 0.15019828155981493, 'fraction_numerical': 0.002974223397224058, 'mean_word_length': 1.3653771004298554, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '743890', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1551, 'n_words': 278}