en_btr_backend/app/Handlers/LexiconHandler.php

105 lines
2.8 KiB
PHP

<?php
namespace App\Handlers;
use Illuminate\Support\Facades\URL;
/**
* LexiconHandler.php
*
* @author: Leonard Smith <leonard@acornwebconsultants.com>
* Date: 9/19/20
* Time: 8:56 AM
*/
class LexiconHandler
{
use BookXmlFilesTrait, LexiconEntryTrait;
public function getEntriesJson($book, $chapter, $verse)
{
$entries = $this->getEntriesByVerse($book, $chapter, $verse);
$json = [];
foreach ($entries as $id => $entry) {
$json[] = self::formatForJson($id, $entry);
}
return $json;
}
public static function getEntryById($id)
{
$content = self::getLexicalContent($id);
return self::formatForJson($id, $content);
}
public function formatForJson($id, $entry)
{
list($lemma, $commentary) = $this->parseEntry($entry);
return [
'type' => 'lexical-entries',
'id' => $id,
'attributes' => [
'strongs-number' => $id,
'lemma' => $lemma,
'commentary' => $commentary,
],
'links' => [
'self' => URL::route('lexicon-entry', $id),
],
];
}
public function getEntriesByVerse($book, $chapter, $verse)
{
$xmlFile = $this->getBookXmlFile('/ulb/', $book);
$document = new \DOMDocument;
$document->load($xmlFile);
$verseNodes = $document->getElementsByTagName('verse');
$entryArray = [];
foreach ($verseNodes as $vn) {
if ($vn->getAttribute('name') === ucfirst($book) . ' ' . $chapter . ':' . $verse) {
$entries = $vn->getElementsByTagName('w');
foreach ($entries as $entry) {
$strongsNumber = $entry->getAttribute('lemma');
$entryArray[$strongsNumber] = LexiconHandler::getLexicalContent($strongsNumber);
}
}
}
return $entryArray;
}
public function getLexicalContent($strongsNumber)
{
$filepath = $this->getFilePath($strongsNumber);
if (file_exists($filepath)) {
$contents = file_get_contents($filepath);
} else {
$contents = 'CONTENT NOT FOUND: ' . $filepath;
}
return $contents;
}
public function getFilePath($strongsNumber)
{
return $this->getFolderName($strongsNumber) . lcfirst($strongsNumber) . '.md';
}
public function getFolderName($strongsNumber)
{
$intPortion = substr($strongsNumber, 1);
$intValue = intval($intPortion);
$upperValue = ceil($intValue / 10) * 10;
$lowerValue = (floor($intValue / 10) * 10) + 1;
return storage_path() . '/gwt/' . 'g' . $lowerValue . '-' . 'g' . $upperValue . '/';
}
}