mirror of
https://github.com/quentin-g-dev/afup.git
synced 2026-03-24 17:22:06 +01:00
43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace AppBundle\TechLetter\Model;
|
|
|
|
class TechLetterFactory
|
|
{
|
|
public static function createTechLetterFromJson($json)
|
|
{
|
|
$array = json_decode($json, true);
|
|
|
|
$articles = $projects = [];
|
|
if (isset($array['projects'])) {
|
|
foreach ($array['projects'] as $project) {
|
|
$projects[] = new Project($project['url'], $project['name'], $project['description']);
|
|
}
|
|
}
|
|
|
|
if (isset($array['articles'])) {
|
|
foreach ($array['articles'] as $article) {
|
|
$language = isset($article['language']) ? $article['language'] : 'en';
|
|
$articles[] = new Article($article['url'], $article['title'], $article['host'], $article['readingTime'], $article['excerpt'], $language);
|
|
}
|
|
}
|
|
|
|
$firstNews = $secondNews = null;
|
|
|
|
if (isset($array['firstNews']) && $array['firstNews'] !== null) {
|
|
$firstNews = new News($array['firstNews']['url'], $array['firstNews']['title'], \DateTimeImmutable::createFromFormat('Y-m-d', $array['firstNews']['date']));
|
|
}
|
|
|
|
if (isset($array['secondNews']) && $array['secondNews'] !== null) {
|
|
$secondNews = new News($array['secondNews']['url'], $array['secondNews']['title'], \DateTimeImmutable::createFromFormat('Y-m-d', $array['secondNews']['date']));
|
|
}
|
|
|
|
return new TechLetter(
|
|
$firstNews,
|
|
$secondNews,
|
|
$articles,
|
|
$projects
|
|
);
|
|
}
|
|
}
|