Commit 6d25beab authored by Sean Madsen's avatar Sean Madsen

Improve Library::parseIdentifier()

* Extract page paths
* Extract page fragments
* Produce an "editionIdentifier"
parent e51574ab
......@@ -16,7 +16,7 @@ class Library {
/**
* Build a new Library based on a directory of book conf files.
*
* @param strig $configDir
* @param string $configDir
*/
public function __construct($configDir) {
$finder = new Finder();
......@@ -147,18 +147,38 @@ class Library {
*
* @param string $identifier (e.g. "user/en/master", "user/en", "user", "")
*
* @return array The following keys/values are present:
* "bookSlug" => (string/NULL) slug used to identify a book
* "languageCode" => (string/NULL) two letter language code
* "versionDescriptor" => (string/NULL)
* @return array See LibraryTest::identifierProvider() for examples
*/
public static function parseIdentifier($identifier) {
$identifier = preg_replace("#/+#", "/", trim($identifier));
$identifier = trim($identifier, "/");
$parts = explode("/", $identifier);
$result['bookSlug'] = ($parts[0]) ? $parts[0] : NULL;
$result['languageCode'] = isset($parts[1]) ? $parts[1] : NULL;
$result['versionDescriptor'] = isset($parts[2]) ? $parts[2] : NULL;
// Remove junk chars from both ends
$identifier = trim($identifier, "/# \t\n\r\0\x0B");
// Ensure there are no repeated occurrences of "/" or "#"
$identifier = preg_replace("_(/|#)+_", "$1", $identifier);
// Split into 2 parts based on the first "#" character
$hashSplit = explode('#', $identifier, 2);
$fragment = $hashSplit[1] ?? NULL;
$preFragment = $hashSplit[0] ?? NULL;
// Take everything before "#" and split it into 4 parts
$slashSplit = explode("/", $preFragment, 4);
// Assign parts
$result['bookSlug'] = $slashSplit[0] ?? NULL;
$result['languageCode'] = $slashSplit[1] ?? NULL;
$result['versionDescriptor'] = $slashSplit[2] ?? NULL;
$editionParts = [
$result['bookSlug'],
$result['languageCode'],
$result['versionDescriptor']
];
$result['editionIdentifier'] = in_array(FALSE, $editionParts)
? NULL
: implode('/', $editionParts);
$result['path'] = $slashSplit[3] ?? NULL;
$result['fragment'] = $fragment;
return $result;
}
......
......@@ -15,13 +15,15 @@ class LibraryTest extends \PHPUnit_Framework_TestCase {
public function identifierProvider() {
return [
[
'',
[
'bookSlug' => NULL,
'languageCode' => NULL,
'versionDescriptor' => NULL,
'editionIdentifier' => NULL,
'path' => NULL,
'fragment' => NULL,
],
],
......@@ -31,6 +33,33 @@ class LibraryTest extends \PHPUnit_Framework_TestCase {
'bookSlug' => 'dev',
'languageCode' => NULL,
'versionDescriptor' => NULL,
'editionIdentifier' => NULL,
'path' => NULL,
'fragment' => NULL,
],
],
[
" /dev \n",
[
'bookSlug' => 'dev',
'languageCode' => NULL,
'versionDescriptor' => NULL,
'editionIdentifier' => NULL,
'path' => NULL,
'fragment' => NULL,
],
],
[
"/foo bar/baz bat",
[
'bookSlug' => 'foo bar',
'languageCode' => 'baz bat',
'versionDescriptor' => NULL,
'editionIdentifier' => NULL,
'path' => NULL,
'fragment' => NULL,
],
],
......@@ -40,6 +69,9 @@ class LibraryTest extends \PHPUnit_Framework_TestCase {
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => NULL,
'editionIdentifier' => NULL,
'path' => NULL,
'fragment' => NULL,
],
],
......@@ -49,6 +81,9 @@ class LibraryTest extends \PHPUnit_Framework_TestCase {
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => NULL,
'fragment' => NULL,
],
],
......@@ -58,6 +93,9 @@ class LibraryTest extends \PHPUnit_Framework_TestCase {
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => NULL,
'fragment' => NULL,
],
],
......@@ -67,6 +105,9 @@ class LibraryTest extends \PHPUnit_Framework_TestCase {
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => NULL,
'fragment' => NULL,
],
],
......@@ -76,9 +117,97 @@ class LibraryTest extends \PHPUnit_Framework_TestCase {
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => NULL,
'fragment' => NULL,
],
],
[
'dev/en/latest/category/foo/my-page/',
[
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => 'category/foo/my-page',
'fragment' => NULL,
],
],
[
'dev/en/latest/category/foo/my-page#',
[
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => 'category/foo/my-page',
'fragment' => NULL,
],
],
[
'dev/en/latest/category/foo/my-page/#some-section',
[
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => 'category/foo/my-page',
'fragment' => 'some-section',
],
],
[
'dev/en/latest/category/foo/my-page/#some-section#another-section',
[
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => 'category/foo/my-page',
'fragment' => 'some-section#another-section',
],
],
[
'dev/en/latest/category/foo/my-page#some-section',
[
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => 'category/foo/my-page',
'fragment' => 'some-section',
],
],
[
'dev/en/latest/category/foo/my-page.md#some-section',
[
'bookSlug' => 'dev',
'languageCode' => 'en',
'versionDescriptor' => 'latest',
'editionIdentifier' => 'dev/en/latest',
'path' => 'category/foo/my-page.md',
'fragment' => 'some-section',
],
],
[
'dev/#some-section',
[
'bookSlug' => 'dev',
'languageCode' => NULL,
'versionDescriptor' => NULL,
'editionIdentifier' => NULL,
'path' => NULL,
'fragment' => 'some-section',
],
],
];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment