-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
496 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"root": "./tests/dist/", | ||
"id": "my-sample-ebook", | ||
"isbn": "1234567890123", | ||
"title": "My Sample Book", | ||
"author": [ | ||
{ | ||
"name": "Kazuo Ishiguro", | ||
"id": "author-1", | ||
"role": "aut" | ||
}, | ||
{ | ||
"name": "Haruki Murakami", | ||
"id": "author-2", | ||
"role": "aut" | ||
}, | ||
{ | ||
"name": "Mike Jacob", | ||
"id": "translator-1", | ||
"role": "trl", | ||
"type": "contributor" | ||
} | ||
], | ||
"target": "./tests/tmp", | ||
"published": "2023-01-01T23:00:00Z", | ||
"direction": "ltr", | ||
"cover": "./tests/dist/img/cover.jpg" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Hametuha\HamePub\Parser; | ||
|
||
/** | ||
* Parse setting JSON file and ensure it has all required keys. | ||
*/ | ||
trait SettingParser { | ||
|
||
/** | ||
* Get setting from file. | ||
* | ||
* @param string $file_path Path to setting file. | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
public function getSettingFromFile( $file_path = './setting.json' ) { | ||
if ( ! file_exists( $file_path ) ) { | ||
throw new \Exception( 'Setting file not found.' ); | ||
} | ||
$setting = json_decode( file_get_contents( $file_path ), true ); | ||
if ( ! is_array( $setting ) ) { | ||
throw new \Exception( 'Setting file is not valid JSON.' ); | ||
} | ||
$setting = array_replace_recursive( $this->defaultSetting(), $setting ); | ||
// Validate if isbn is not set. | ||
if ( empty( $setting['id']) ) { | ||
throw new \Exception( 'id fields must not be empty: ' . $setting['id'] ); | ||
} | ||
// Validate if published is not set. | ||
if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/u', $setting['published' ] ) ) { | ||
throw new \Exception( 'published field is malformed. Should be GMT in ISO8601 e.g. "2000-01-01T00:00:00Z": ' . $setting['published'] ); | ||
} | ||
// Validate if author is set. | ||
if ( empty( $setting['author'] ) ) { | ||
throw new \Exception( 'At least 1 author should be set.' ); | ||
} | ||
return $setting; | ||
} | ||
|
||
/** | ||
* Default setting. | ||
* | ||
* @return array | ||
*/ | ||
public function defaultSetting() { | ||
return [ | ||
'lang' => 'en', | ||
'id' => '', | ||
'isbn' => '', | ||
'title' => '', | ||
'author' => '', | ||
'published' => '', | ||
'root' => './dist/', | ||
'header' => [ | ||
'max_level' => 3, | ||
'depth' => 2, | ||
], | ||
'toc' => 'Table of Contents', | ||
'url_base' => '#\./#u', | ||
'target' => './tmp', | ||
'direction' => 'default', | ||
'hidden' => [ 'toc' ], | ||
'cover' => '', | ||
]; | ||
} | ||
} |
Oops, something went wrong.