Skip to content

Commit

Permalink
add new lang (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv authored Oct 30, 2023
1 parent 77241de commit 4e1d41d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.7 (October 30, 2023)
* Add additional languages.

## 1.0.6 (October 28, 2023)
* Fix suppression of html parsing warnings.

Expand Down
67 changes: 67 additions & 0 deletions src/FetchMeditation/GermanJFT.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace FetchMeditation;

use FetchMeditation\Utilities\HttpUtility;

class GermanJFT extends JFT
{
public function getLanguage(): JFTLanguage
{
return $this->settings->language;
}

public function fetch(): JFTEntry
{
$data = HttpUtility::httpGet('https://narcotics-anonymous.de/artikel/nur-fuer-heute/');
$doc = new \DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML('<?xml encoding="UTF-8">' . $data);
libxml_clear_errors();
libxml_use_internal_errors(false);
$xpath = new \DOMXPath($doc);
$container = $xpath->query('//div[@id="jft-container"]')->item(0);
// Find the div with id "jft-container"
$elements = array();

// Loop through the child nodes of the container
foreach ($container->childNodes as $node) {
// Check if the node is an element
if ($node->nodeType === XML_ELEMENT_NODE) {
// Get the ID of the element
$id = $node->getAttribute('id');
// Check if the ID is "jft-content"
if ($id === 'jft-content') {
// Initialize an empty sub-array for "jft-content"
$contentArray = array();
// Loop through the child nodes of "jft-content"
foreach ($node->childNodes as $contentNode) {
// Check if the child node is a <p> element
if ($contentNode->nodeName === 'p') {
// Add the <p> element's content to the sub-array
$contentArray[] = trim($contentNode->nodeValue);
}
}
// Add the sub-array to the main array with "jft-content" as the key
$elements[$id] = $contentArray;
} else {
// Add other elements to the array with their IDs as keys
$elements[$id] = str_replace("\n", "", $node->nodeValue);
}
}
}
$elements['page'] = '';
$elements['copyright'] = '';
$elements['jft-content'] = array_values(array_filter($elements['jft-content']));
return new JFTEntry(
$elements['jft-date'],
$elements['jft-title'],
$elements['page'],
$elements['jft-quote'],
$elements['jft-quote-source'],
$elements['jft-content'],
$elements['jft-thought'],
$elements['copyright']
);
}
}
1 change: 1 addition & 0 deletions src/FetchMeditation/JFT.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static function getInstance(JFTSettings $settings): JFT
return match ($settings->language) {
JFTLanguage::English => new EnglishJFT($settings),
JFTLanguage::French => new FrenchJFT($settings),
JFTLanguage::German => new GermanJFT($settings),
JFTLanguage::Italian => new ItalianJFT($settings),
JFTLanguage::Japanese => new JapaneseJFT($settings),
JFTLanguage::Portuguese => new PortugueseJFT($settings),
Expand Down
1 change: 1 addition & 0 deletions src/FetchMeditation/JFTLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum JFTLanguage
{
case English;
case French;
case German;
case Italian;
case Japanese;
case Portuguese;
Expand Down

0 comments on commit 4e1d41d

Please sign in to comment.