Skip to content

Instantly share code, notes, and snippets.

@huubl
Forked from Log1x/Rating.php
Created December 1, 2020 13:08
Show Gist options
  • Select an option

  • Save huubl/d73a59f7d2be8bab65a9227f27254949 to your computer and use it in GitHub Desktop.

Select an option

Save huubl/d73a59f7d2be8bab65a9227f27254949 to your computer and use it in GitHub Desktop.

Revisions

  1. @Log1x Log1x revised this gist Jul 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.blade.php
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    <x-rating />
    <x-rating rating="3.4" size="20" type="thumbs-up" />
    <x-rating rating="3.4" size="20" type="thumbsUp" />
  2. @Log1x Log1x created this gist Jul 6, 2020.
    87 changes: 87 additions & 0 deletions Rating.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    <?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');
    }
    }
    2 changes: 2 additions & 0 deletions example.blade.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    <x-rating />
    <x-rating rating="3.4" size="20" type="thumbs-up" />
    8 changes: 8 additions & 0 deletions rating.blade.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <fa-rating
    :glyph="{{ $type }}"
    :item-size="{{ $size }}"
    active-color="currentColor"
    :increment="0.1"
    :rating="{{ $rating }}"
    v-model="rating">
    </fa-rating>