Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 103 additions & 102 deletions src/ElpParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* ElpParser.php
*
Expand Down Expand Up @@ -33,70 +34,70 @@ class ELPParser implements \JsonSerializable
{
/**
* Path to the .elp file
*
*
* @var string
*/
protected string $filePath;

/**
* ELP file version (2 or 3)
*
*
* @var int
*/
protected int $version;

/**
* Extracted content and metadata
*
*
* @var array
*/
protected array $content = [];

/**
* Raw extracted strings
*
*
* @var array
*/
protected array $strings = [];

/**
* Title of the ELP content
*
*
* @var string
*/
protected string $title = '';

/**
* Description of the ELP content
*
*
* @var string
*/
protected string $description = '';

/**
* Author of the ELP content
*
*
* @var string
*/
protected string $author = '';

/**
* License of the ELP content
*
*
* @var string
*/
protected string $license = '';

/**
* Language of the ELP content
*
*
* @var string
*/
protected string $language = '';

/**
* Learning resource type
*
*
* @var string
*/
protected string $learningResourceType = '';
Expand All @@ -105,7 +106,7 @@ class ELPParser implements \JsonSerializable
* Create a new ELPParser instance
*
* @param string $filePath Path to the .elp file
*
*
* @throws Exception If file cannot be opened or is invalid
* @return void
*/
Expand All @@ -119,7 +120,7 @@ public function __construct(string $filePath)
* Static method to create an ELPParser from a file path
*
* @param string $filePath Path to the .elp file
*
*
* @throws Exception If file cannot be opened or is invalid
* @return self
*/
Expand All @@ -137,7 +138,7 @@ public static function fromFile(string $filePath): self
protected function parse(): void
{
$zip = new ZipArchive();

if (!file_exists($this->filePath)) {
throw new Exception('File does not exist.');
}
Expand Down Expand Up @@ -181,7 +182,7 @@ protected function parse(): void
* Parse the XML content and extract relevant information
*
* @param string $xmlContent XML content as a string
*
*
* @throws Exception If XML parsing fails
* @return void
*/
Expand Down Expand Up @@ -210,7 +211,7 @@ protected function parseXML(string $xmlContent): void
* Extract strings from the XML document
*
* @param SimpleXMLElement $xml XML document
*
*
* @return void
*/
protected function extractStrings(SimpleXMLElement $xml): void
Expand All @@ -223,7 +224,7 @@ protected function extractStrings(SimpleXMLElement $xml): void
* Recursively extract all text strings from XML
*
* @param SimpleXMLElement $element XML element to extract from
*
*
* @return array Extracted strings
*/
protected function recursiveStringExtraction(SimpleXMLElement $element): array
Expand Down Expand Up @@ -279,7 +280,7 @@ public function getStrings(): array
* Extract metadata from version 3 XML format
*
* @param SimpleXMLElement $xml XML document
*
*
* @return void
*/
protected function extractVersion3Metadata(SimpleXMLElement $xml): void
Expand All @@ -290,24 +291,24 @@ protected function extractVersion3Metadata(SimpleXMLElement $xml): void
$value = (string)$property->value;

switch ($key) {
case 'pp_title':
$this->title = $value;
break;
case 'pp_description':
$this->description = $value;
break;
case 'pp_author':
$this->author = $value;
break;
case 'license':
$this->license = $value;
break;
case 'lom_general_language':
$this->language = $value;
break;
case 'pp_learningResourceType':
$this->learningResourceType = $value;
break;
case 'pp_title':
$this->title = $value;
break;
case 'pp_description':
$this->description = $value;
break;
case 'pp_author':
$this->author = $value;
break;
case 'license':
$this->license = $value;
break;
case 'lom_general_language':
$this->language = $value;
break;
case 'pp_learningResourceType':
$this->learningResourceType = $value;
break;
}
}
}
Expand Down Expand Up @@ -377,7 +378,7 @@ public function getLearningResourceType(): string
* Extract metadata from version 2 XML format
*
* @param SimpleXMLElement $xml XML document
*
*
* @return void
*/
protected function extractVersion2Metadata(SimpleXMLElement $xml): void
Expand All @@ -402,35 +403,35 @@ protected function extractVersion2Metadata(SimpleXMLElement $xml): void
} elseif ($currentKey !== null) {
// Extract the value based on the type of element
switch ($elementName) {
case 'unicode':
$metadata[$currentKey] = (string)$element['value'];
break;
case 'bool':
$metadata[$currentKey] = ((string)$element['value']) === '1';
break;
case 'int':
$metadata[$currentKey] = (int)$element['value'];
break;
case 'list':
// Handle lists if necessary
$listValues = [];
foreach ($element->children() as $listItem) {
if ($listItem->getName() === 'unicode') {
$listValues[] = (string)$listItem['value'];
case 'unicode':
$metadata[$currentKey] = (string)$element['value'];
break;
case 'bool':
$metadata[$currentKey] = ((string)$element['value']) === '1';
break;
case 'int':
$metadata[$currentKey] = (int)$element['value'];
break;
case 'list':
// Handle lists if necessary
$listValues = [];
foreach ($element->children() as $listItem) {
if ($listItem->getName() === 'unicode') {
$listValues[] = (string)$listItem['value'];
}
// Add handling for other types of elements within the list if necessary
}
// Add handling for other types of elements within the list if necessary
}
$metadata[$currentKey] = $listValues;
break;
case 'dictionary':
// Handle nested dictionaries if necessary
// This may require a recursive function
// For simplicity, it can be omitted or implemented as needed
break;
$metadata[$currentKey] = $listValues;
break;
case 'dictionary':
// Handle nested dictionaries if necessary
// This may require a recursive function
// For simplicity, it can be omitted or implemented as needed
break;
// Add other cases as needed
default:
// Handle unknown types or ignore them
break;
default:
// Handle unknown types or ignore them
break;
}

// Reset the current key after assigning the value
Expand All @@ -445,7 +446,6 @@ protected function extractVersion2Metadata(SimpleXMLElement $xml): void
$this->license = $metadata['license'] ?? '';
$this->language = $metadata['_lang'] ?? '';
$this->learningResourceType = $metadata['_learningResourceType'] ?? '';

}


Expand Down Expand Up @@ -542,7 +542,7 @@ public function getMetadata(): array
$meta = [
[
'schema' => 'Package',
'content' => [
'content' => [
'title' => $data['_title'] ?? '',
'lang' => $data['_lang'] ?? '',
'description' => [
Expand Down Expand Up @@ -618,40 +618,40 @@ protected function parseElement(SimpleXMLElement $element): mixed
$name = $element->getName();

switch ($name) {
case 'unicode':
case 'string':
return (string) $element['value'];
case 'int':
return (int) $element['value'];
case 'bool':
return ((string) $element['value']) === '1';
case 'list':
$list = [];
foreach ($element->children() as $child) {
$list[] = $this->parseElement($child);
}
return $list;
case 'dictionary':
$dict = [];
$key = null;
foreach ($element->children() as $child) {
$cname = $child->getName();
if (($cname === 'string' || $cname === 'unicode') && (string) $child['role'] === 'key') {
$key = (string) $child['value'];
} elseif ($key !== null) {
$dict[$key] = $this->parseElement($child);
$key = null;
case 'unicode':
case 'string':
return (string) $element['value'];
case 'int':
return (int) $element['value'];
case 'bool':
return ((string) $element['value']) === '1';
case 'list':
$list = [];
foreach ($element->children() as $child) {
$list[] = $this->parseElement($child);
}
}
return $dict;
case 'instance':
return $this->parseElement($element->dictionary);
case 'none':
return null;
case 'reference':
return ['ref' => (string) $element['key']];
default:
return null;
return $list;
case 'dictionary':
$dict = [];
$key = null;
foreach ($element->children() as $child) {
$cname = $child->getName();
if (($cname === 'string' || $cname === 'unicode') && (string) $child['role'] === 'key') {
$key = (string) $child['value'];
} elseif ($key !== null) {
$dict[$key] = $this->parseElement($child);
$key = null;
}
}
return $dict;
case 'instance':
return $this->parseElement($element->dictionary);
case 'none':
return null;
case 'reference':
return ['ref' => (string) $element['key']];
default:
return null;
}
}

Expand Down Expand Up @@ -726,14 +726,14 @@ protected function slug(string $text): string
* Extract contents of an ELP file to a specified directory
*
* @param string $destinationPath Directory to extract contents to
*
*
* @throws Exception If extraction fails
* @return void
*/
public function extract(string $destinationPath): void
{
$zip = new ZipArchive();

if ($zip->open($this->filePath) !== true) {
throw new Exception("Unable to open ELP file for extraction");
}
Expand Down Expand Up @@ -826,8 +826,9 @@ function removeAccents(string $text, string $locale = ''): string
'Ố' => 'O', 'ố' => 'o', 'Ớ' => 'O', 'ớ' => 'o', 'Ứ' => 'U', 'ứ' => 'u',
];

if ('de_DE' === $locale || 'de_DE_formal' === $locale
|| 'de_CH' === $locale || 'de_CH_informal' === $locale
if (
'de_DE' === $locale || 'de_DE_formal' === $locale
|| 'de_CH' === $locale || 'de_CH_informal' === $locale
|| 'de_AT' === $locale
) {
$chars['Ä'] = 'Ae';
Expand Down
3 changes: 2 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
*/

expect()->extend(
'toBeOne', function () {
'toBeOne',
function () {
return $this->toBe(1);
}
);
Expand Down
Loading
Loading