Skip to content

Commit

Permalink
Extract read_patterns_from_file method
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed May 16, 2024
1 parent e8930b5 commit d2a6016
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 74 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"phpstan/phpstan": "^1.9",
"phpbench/phpbench": "^0.17||^1.0@dev",
"mikey179/vfsstream": "~1",
"mundschenk-at/phpunit-cross-version": "dev-master",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/extension-installer": "^1.2"
Expand Down
86 changes: 60 additions & 26 deletions src/class-hyphenator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
namespace PHP_Typography;

use PHP_Typography\Exceptions\Invalid_Encoding_Exception;
use PHP_Typography\Exceptions\Invalid_File_Exception;
use PHP_Typography\Exceptions\Invalid_Hyphenation_Pattern_File_Exception;
use PHP_Typography\Exceptions\Invalid_JSON_Exception;

use PHP_Typography\Hyphenator\Trie_Node;
use PHP_Typography\Text_Parser\Token;

Expand All @@ -42,6 +46,8 @@
* @author Peter Putzer <github@mundschenk.at>
*
* @since 3.4.0
*
* @phpstan-type Pattern_File array{ patterns: array<string,string>, exceptions: array<string,string> }
*/
class Hyphenator {

Expand Down Expand Up @@ -159,45 +165,73 @@ protected static function get_object_hash( $data ): string {
*
* @param string $lang Has to correspond to a filename in 'lang'.
*
* @return bool Whether loading the pattern file was successful.
* @return void
*
* @throws \RuntimeException Throws an exception when the language file cannot be read correctly.
*/
public function set_language( string $lang ): bool {
public function set_language( string $lang ): void {
if ( isset( $this->language ) && $this->language === $lang ) {
return true; // Bail out, no need to do anything.
return; // Bail out, no need to do anything.
}

$success = false;
$language_file_name = __DIR__ . '/lang/' . $lang . '.json';
try {
$pattern_file = $this->read_patterns_from_file( __DIR__ . '/lang/' . $lang . '.json' );

if ( \file_exists( $language_file_name ) ) {
$raw_language_file = \file_get_contents( $language_file_name );
$this->pattern_trie = Trie_Node::build_trie( $pattern_file['patterns'] );
$this->pattern_exceptions = $pattern_file['exceptions'];
$this->language = $lang;
} catch ( \RuntimeException $e ) {
// Clean up object state.
$this->pattern_trie = null;
$this->pattern_exceptions = [];
$this->language = null;

if ( false !== $raw_language_file ) {
$language_file = \json_decode( $raw_language_file, true );
throw $e;
} finally {
// Make sure hyphenationExceptions is not set to force remerging of patgen and custom exceptions.
$this->merged_exception_patterns = null;
}
}

if ( false !== $language_file ) {
$this->language = $lang;
$this->pattern_trie = Trie_Node::build_trie( $language_file['patterns'] );
$this->pattern_exceptions = $language_file['exceptions'] ?? [];
/**
* Reads the hyphenation patterns and exceptions from a given pattern file and builds the
* corresponding trie and exceptions array.
*
* @since 7.0.0
*
* @param string $file The full path to the pattern file.
*
* @return Pattern_File
*
* @throws Invalid_File_Exception Throws an exception if the pattern file cannot be read.
* @throws Invalid_JSON_Exception Throws an exception if the pattern file cannot decoded.
* @throws Invalid_Hyphenation_Pattern_File_Exception Throws an exception if the pattern file is structurally invalid.
*/
protected function read_patterns_from_file( string $file ): array {
$pattern_file = @\file_get_contents( $file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- return value is checked.

$success = true;
}
}
}
if ( false !== $pattern_file ) {
$pattern_file = \json_decode( $pattern_file, true );

// Clean up.
if ( ! $success ) {
$this->language = null;
$this->pattern_trie = null;
$this->pattern_exceptions = [];
}
if ( null !== $pattern_file ) {
if ( ! isset( $pattern_file['patterns'] ) || ! \is_array( $pattern_file['patterns'] ) ) {
throw new Invalid_Hyphenation_Pattern_File_Exception( "Invalid pattern file {$file}" );
}

// Make sure hyphenationExceptions is not set to force remerging of patgen and custom exceptions.
$this->merged_exception_patterns = null;
if ( ! isset( $pattern_file['exceptions'] ) || ! \is_array( $pattern_file['exceptions'] ) ) {
$pattern_file['exceptions'] = [];
}

return $success;
return $pattern_file;
} else {
throw new Invalid_JSON_Exception( "Error decoding JSON from language file {$file}" );
}
} else {
throw new Invalid_File_Exception( "Could not open language file {$file}" );
}
}


/**
* Hyphenates parsed text tokens.
*
Expand Down
37 changes: 37 additions & 0 deletions src/exceptions/class-invalid-file-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of PHP-Typography.
*
* Copyright 2024 Peter Putzer.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***
*
* @package mundschenk-at/php-typography
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/

namespace PHP_Typography\Exceptions;

/**
* An invalid filename or path was given.
*
* @author Peter Putzer <github@mundschenk.at>
*
* @since 7.0.0
*/
class Invalid_File_Exception extends \UnexpectedValueException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of PHP-Typography.
*
* Copyright 2024 Peter Putzer.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***
*
* @package mundschenk-at/php-typography
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/

namespace PHP_Typography\Exceptions;

/**
* A hyphenation pattern file has an invalid structure.
*
* @author Peter Putzer <github@mundschenk.at>
*
* @since 7.0.0
*/
class Invalid_Hyphenation_Pattern_File_Exception extends \RuntimeException {
}
37 changes: 37 additions & 0 deletions src/exceptions/class-invalid-json-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of PHP-Typography.
*
* Copyright 2024 Peter Putzer.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***
*
* @package mundschenk-at/php-typography
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/

namespace PHP_Typography\Exceptions;

/**
* An invalid JSON string was given.
*
* @author Peter Putzer <github@mundschenk.at>
*
* @since 7.0.0
*/
class Invalid_JSON_Exception extends \UnexpectedValueException {
}
Loading

0 comments on commit d2a6016

Please sign in to comment.