File: D:/HostingSpaces/SBogers10/sportivo.komma.pro/app/Komma/Blog/BlogService.php
<?php
namespace Komma\Blog;
use Carbon\Carbon;
use Laravelrus\LocalizedCarbon;
class BlogService {
protected $blogRepository;
public function __construct(BlogRepository $blogRepository){
$this->blogRepository = $blogRepository;
}
/**
*
* Get $amount of latest blogitems
*
* @param int $amount
* @param int $skip
* @return array
*/
public function getLatestBlogItems($amount){
$blogArticles = [];
foreach($this->blogRepository->blog($amount) as $article){
setlocale(LC_ALL, 'nl_NL');
$date = Carbon::createFromFormat('Y-m-d H:i:s', $article->published_at);
$blogArticles[] = (object)['date'=>$date->formatLocalized('%d . %b . %Y') ,'header'=>$article->name, 'teaser'=>$article->intro, 'route'=>'/'.$article->route, 'img'=>$article->large_image_url];
}
return $blogArticles;
}
/**
*
* Get $amount of previous blogitems after $slug (blogitem)
*
* @param int $amount
* @param int $skip
* @return array
*/
public function getPreviousBlogItems($slug, $amount){
$blogArticles = [];
foreach($this->blogRepository->getPreviousBlogItems($slug, $amount) as $article){
setlocale(LC_ALL, 'nl_NL');
$date = Carbon::createFromFormat('Y-m-d H:i:s', $article->published_at);
$blogArticles[] = (object)['date'=>$date->formatLocalized('%d . %b . %Y') ,'header'=>$article->name, 'teaser'=>$article->intro, 'route'=>'/'.$article->route, 'img'=>$article->large_image_url];
}
return $blogArticles;
}
/**
*
* Get $amount of blogitems after $skip for in the article block
*
* @param int $amount
* @param int $skip
* @return array
*/
public function getBlogItems($amount=4, $skip=0){
$blogArticles = [];
foreach($this->blogRepository->blogSkip($amount, $skip) as $article){
setlocale(LC_ALL, 'nl_NL');
$date = Carbon::createFromFormat('Y-m-d H:i:s', $article->published_at)->subHour(1);
$data = LocalizedCarbon\LocalizedCarbon::instance($date)->diffForHumans();
$blogArticles[] = (object)['date'=>$data,'header'=>$article->name, 'teaser'=>$article->intro, 'route'=>'/'.$article->route, 'img'=>$article->large_image_url];
}
return $blogArticles;
}
/**
* Get how many blogitems there are
*
* @return int
*/
public function getAmountOfBlogItems(){
return count($this->blogRepository->getAmountOfBlogItems());
}
/**
*
* Get a blogitem or return false when it doesn't exist
* Make it right format for blogArticle
*
* @param $slug
* @return array|bool
*/
public function getBlogItem($slug){
$article = $this->blogRepository->getBlogItem($slug);
if($article!=null)
{
return ['content' => ['header' => $article->name, 'text' => $article->intro, 'text2' => json_decode($article->description), 'date' => $article->published_at, 'small_img' => $article->large_image_url], 'photo' => $article->original_image_url];
}
else{
return false;
}
}
/**
* Get BlogItems name and their routes
*
* @return array
*/
public function getBlogRoutes(){
return $this->blogRepository->blogRoutes();
}
}