Skip to content

Instantly share code, notes, and snippets.

@huubl
Forked from Log1x/Rating.php
Created December 1, 2020 13:08
Show Gist options
  • Save huubl/d73a59f7d2be8bab65a9227f27254949 to your computer and use it in GitHub Desktop.
Save huubl/d73a59f7d2be8bab65a9227f27254949 to your computer and use it in GitHub Desktop.
Rating component
<x-rating />
<x-rating rating="3.4" size="20" type="thumbsUp" />
<fa-rating
:glyph="{{ $type }}"
:item-size="{{ $size }}"
active-color="currentColor"
:increment="0.1"
:rating="{{ $rating }}"
v-model="rating">
</fa-rating>
<?php
namespace App\View\Components;
use Roots\Acorn\View\Component;
class Rating extends Component
{
/**
* The rating value.
*
* @return int
*/
public $rating;
/**
* The rating type.
*
* @return string
*/
public $type;
/**
* The rating size.
*
* @return int
*/
public $size;
/**
* Create the component instance.
*
* @param string $type
* @param int $size
* @param int $rating
* @return void
*/
public function __construct($type = 'star', $size = 16, $rating = null)
{
$this->type = $type;
$this->size = $size;
$this->rating = $rating;
if (empty($this->$rating)) {
$this->rating = get_field('rating') ?? get_sub_field('rating') ?? 1;
}
}
/**
* The formatted rating.
*
* @return string
*/
public function rating()
{
return $this->format($this->rating);
}
/**
* Format a number to the first decimal.
*
* @param int $x
* @param int $y
* @return string
*/
protected function format($x, $y = 1)
{
if (! is_numeric($x)) {
$x = 1;
}
return number_format(
min(max($x, 1), 5),
$y
);
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
return $this->view('components.rating');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment