Created
January 10, 2019 14:26
-
-
Save BerezhniyDmitro/1ec0fbd45243387a41e9a0ddf80bbcef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\BusinessType\Mapper; | |
use App\Models\BusinessType\Entities\BusinessAdditionalInfo; | |
use App\Models\BusinessType\Entities\BusinessAnalytics; | |
use App\Models\BusinessType\Entities\BusinessArticle; | |
use App\Models\BusinessType\Entities\BusinessMinBudgets; | |
use App\Models\BusinessType\Entities\BusinessTypeMain; | |
use App\Models\BusinessType\Entities\BusinessTypePage; | |
use App\Models\BusinessType\Entities\BusinessTypeSeo; | |
use App\Models\BusinessType\Entities\BusinessTypeSiteMap; | |
use App\Models\BusinessType\Entities\BusinessTypeSocial; | |
use App\Models\BusinessType\Entities\Diagram\Diagram; | |
use App\Models\BusinessType\Entities\Diagram\DiagramItem; | |
use App\Models\Hydrator\Hydrator; | |
use App\Models\Promotion\Vocabulary\SpecificatedStringField; | |
use App\ValueObject\Link\LinkArticle; | |
use App\ValueObject\Link\LinkImage; | |
use App\ValueObject\Title; | |
use App\ValueObject\Url; | |
use DateTime; | |
/** | |
* Class BusinessTypePageMapper Маппер для востановление агрегата бизнес страницы из бд | |
*/ | |
class BusinessTypePageMapper | |
{ | |
/** | |
* @var Hydrator | |
*/ | |
private $hydrator; | |
/** | |
* BusinessTypePageMapper constructor. | |
* @param Hydrator $hydrator | |
*/ | |
public function __construct(Hydrator $hydrator) | |
{ | |
$this->hydrator = $hydrator; | |
} | |
/** | |
* @param Hydrator $hydrator | |
* @return BusinessTypePageMapper | |
*/ | |
public static function instance(Hydrator $hydrator) | |
{ | |
return new self($hydrator); | |
} | |
/** | |
* @param $raw_data | |
* @return object | |
* @throws \ReflectionException | |
*/ | |
public function map_page($raw_data) | |
{ | |
$page = $raw_data['page']; | |
$array_map = []; | |
foreach ($raw_data['articles'] as $key => $article) { | |
$array_map[$key] = $this->hydrator->hydrate(BusinessArticle::class, [ | |
'title' => new Title($article->title), | |
'link_article' => new LinkArticle($article->link), | |
'link_image' => new LinkImage($article->image_path), | |
]); | |
} | |
$articles = $array_map; | |
$analytics = array_map(function ($analytic) { | |
return $this->hydrator->hydrate(BusinessAnalytics::class, [ | |
'language_id' => $analytic->language, | |
'region' => $analytic->region, | |
'average_cost_click' => empty($analytic->average_cost_click) ? 0 : $analytic->average_cost_click, | |
'average_cost_url' => empty($analytic->average_cost_url) ? null : new Url($analytic->average_cost_url), | |
]); | |
}, $raw_data['analytics']); | |
$diagram_items = array_map(function ($item) { | |
return $this->hydrator->hydrate(DiagramItem::class, [ | |
'title' => new Title($item->title), | |
'percentage' => $item->percentage, | |
]); | |
}, $raw_data['diagram_items']); | |
$min_budgets = array_map(function ($item) { | |
return $this->hydrator->hydrate(BusinessMinBudgets::class, [ | |
'region' => $item->region, | |
'budget' => $item->budget, | |
'lang_id' => $item->language_id, | |
]); | |
}, $raw_data['min_budgets']); | |
$page = $this->hydrator->hydrate(BusinessTypePage::class, [ | |
'id' => $page->id, | |
'lang_id' => $page->language_id, | |
'domain_id' => $page->domain_id, | |
'type_page' => $page->page_type_id, | |
'title' => new Title($page->page_title), | |
'short_description' => $page->short_description, | |
'url' => $page->url, | |
'is_visible' => $page->is_visible, | |
'visibility' => $page->is_visible, | |
'seo' => empty($page->seo_title) ? | |
null : | |
$this->hydrator->hydrate(BusinessTypeSeo::class, [ | |
'title' => $page->seo_title, | |
'description' => $page->seo_description, | |
'keywords' => $page->keywords, | |
'robots' => $page->robots, | |
'canonical' => $page->canonical, | |
'breadcrumbs' => $page->breadcrumbs, | |
'head_meta_tags' => $page->head_meta_tags, | |
]), | |
'social' => $this->hydrator->hydrate(BusinessTypeSocial::class, [ | |
'title' => new SpecificatedStringField( | |
empty($page->social_title) ? '' : $page->social_title, $page->is_used_title_from_seo | |
), | |
'description' => new SpecificatedStringField( | |
$page->social_description, $page->is_used_description_from_seo | |
), | |
'image' => $page->image, | |
'og_type' => $page->og_type, | |
'twitter_card' => $page->twitter_card, | |
]), | |
'sitemap' => empty($page->page_sitemap_page_id) ? | |
null : | |
$this->hydrator->hydrate(BusinessTypeSiteMap::class, [ | |
'is_use_html' => (int)$page->is_html_show, | |
'is_use_xml' => (int)$page->is_xml_show, | |
'priority' => $page->priority, | |
'change_frequency' => (string)$page->change_frequency, | |
'last_modified' => empty($page->last_modified) ? new DateTime() : new DateTime($page->last_modified), | |
]), | |
'main' => $this->hydrator->hydrate(BusinessTypeMain::class, [ | |
'analytics' => $analytics, | |
'additional_info' => $this->hydrator->hydrate(BusinessAdditionalInfo::class, [ | |
'first_url_screen' => empty($page->first_url_screen) ? new LinkImage('') : new LinkImage($page->first_url_screen), | |
'background_url' => empty($page->background_url) ? new LinkImage('') : new LinkImage($page->background_url), | |
'theme_context' => $page->theme_context, | |
'theme_seo' => $page->theme_seo, | |
'description_seo' => $page->description_seo, | |
'description_context' => $page->description_context, | |
'achievements' => $page->achievements, | |
'custom_title' => $page->custom_title, | |
'custom_root_title' => $page->custom_root_title, | |
]), | |
'clients' => $raw_data['clients'], | |
'cases' => $raw_data['cases'], | |
'business_types' => $raw_data['business_types'], | |
'employees' => $raw_data['employees'], | |
'diagram' => empty($diagram_items) ? null : new Diagram($diagram_items), | |
'min_budgets' => $min_budgets, | |
'articles' => $articles, | |
]), | |
]); | |
return $page; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment