You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

7 years ago
<?php
namespace core\helpers;
use core\entities\post\Post;
3 years ago
use Exception;
7 years ago
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use Yii;
class PostHelper
{
public static function statusList(): array
{
return [
Post::STATUS_DRAFT => Yii::t('post', 'Draft'),
Post::STATUS_ACTIVE => Yii::t('post', 'Active'),
];
}
3 years ago
/**
* @param $status
* @return string
* @throws Exception
*/
7 years ago
public static function statusName($status): string
{
return ArrayHelper::getValue(self::statusList(), $status);
}
3 years ago
/**
* @param $status
* @return string
* @throws Exception
*/
7 years ago
public static function statusLabel($status): string
{
3 years ago
$class = match ($status) {
Post::STATUS_DRAFT => 'label label-default',
Post::STATUS_ACTIVE => 'label label-success',
default => 'label label-default',
};
7 years ago
return Html::tag('span', ArrayHelper::getValue(self::statusList(), $status), [
'class' => $class,
]);
}
public static function parseYoutubeUrl($url)
{
$urls = parse_url($url);
//url is http://youtu.be/xxxx
if($urls['host'] == 'youtu.be'){
$id = ltrim($urls['path'],'/');
}
//url is http://www.youtube.com/embed/xxxx
else if(strpos($urls['path'],'embed') == 1){
3 years ago
$parts = explode('/',$urls['path']);
$id = end($parts);
7 years ago
}
//url is xxxx only
3 years ago
else if (!str_contains($url, '/')) {
7 years ago
$id = $url;
}
//http://www.youtube.com/watch?feature=player_embedded&v=m-t4pcO99gI
//url is http://www.youtube.com/watch?v=xxxx
3 years ago
else {
7 years ago
parse_str($urls['query']);
3 years ago
/* @var $v string */
7 years ago
$id = $v;
if(!empty($feature)){
3 years ago
$parts = explode('v=',$urls['query']);
$id = end($parts);
7 years ago
}
}
return $id;
}
}