what is the right way to write those checks. i have a array, some time values are not set or they are empty. right now that code bit hard to read, what can i do to make that look more clear and trust-able. or i’m just going to hard on this . any suggestions?
<?php
private static function filter_yt_data($yt_api_response = null)
{
$data = array();
$api = json_decode($yt_api_response);
$api = $api->data->items[0];
$data = array(
'id' => $api->id,
'author' => e($api->uploader),
'label' => e($api->category),
'title' => e($api->title),
'description' => e($api->description),
'duration' => $api->duration,
'view_count' => $api->viewCount,
'access' => $api->accessControl->embed,
'favorite_count'=> $api->favoriteCount
);
if($api->accessControl->comment == 'allowed')
{
$data['comment_count'] = $api->commentCount;
}
else
{
$data['comment_count'] = 0;
}
if($api->ratingCount)
{
$data['rating_count'] = $api->ratingCount;
}
else
{
$data['rating_count'] = 0;
}
if($api->accessControl->rate == 'allowed')
{
$data['like_count'] = $api->likeCount;
}
else
{
$data['like_count'] = 0;
}
if($api->thumbnail->hqDefault)
{
$data['thumbnail'] = $api->thumbnail->hqDefault;
}
elseif($api->thumbnail->sqDefault)
{
$data['thumbnail'] = $api->thumbnail->sqDefault;
}else
{
$data['thumbnail'] = null;
}
return $data;
}
?>
could also be:
This is called ternary operator
Syntax:
in that way you could replace all your if-else statements.