PHP code example of emleons / sim-rating

1. Go to this page and download the library: Download emleons/sim-rating library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

emleons / sim-rating example snippets



use Emleons\SimRating\Rating;

// Initialize with rating counts (all five keys recommended; missing keys default to 0)
$ratings = [
    'one_star' => 10,
    'two_star' => 20,
    'three_star' => 30,
    'four_star' => 40,
    'five_star' => 50
];

$rating = new Rating($ratings);

// Display 5-star rating
echo $rating->render(); // Defaults to HTML stars

// As interactive HTML stars
echo $rating->render('html', [
    'interactive' => true
]);

// As JSON data
$jsonOutput = $rating->render('json');
// {
//   "average": 3.67,
//   "total": 150,
//   "distribution": {
//     "five_star": 33.33,
//     "four_star": 26.67,
//     ...
//   }
// }

// As percentage bars
echo $rating->render('html', [
    'type' => 'bars',
    'bar_height' => '25px'
]);

new Rating($ratings, [
    'type' => 'stars',       // Display type
    'color' => '#6a5acd',    // Star color
    'size' => '1.5rem',      // Size
    'show_average' => false, // Hide average
    'show_total' => true     // Show count
]);

new Rating($ratings, [
    'type' => 'bars',
    'color' => '#4a90e2',          // Bar color
    'bar_height' => '20px',        // Bar thickness
    'bar_show_percentages' => true,// Show percentages
    'show_summary' => true         // Show summary
]);

$rating->setOptions([
    'color' => '#e74c3c', // Change to red
    'size' => '2rem',     // Larger size
    'type' => 'bars'      // Switch to bars
]);

// Get current options
$currentOptions = $rating->getOptions();

@php
    $rating = new \Emleons\SimRating\Rating($product->ratings, [
        'interactive' => true,
        'show_summary' => false
    ]);
@endphp

{!! $rating->render() !!}

// Malicious option values are neutralised, never emitted verbatim.
$rating = new Rating(['five_star' => 1], [
    'color' => '"><script>alert(1)</script>', // falls back to #ffc107
]);

echo $rating->render(); // safe HTML

// Partial ratings are allowed by default (missing buckets = 0).
//    Enable 'strict' to y

// Unknown rating keys always throw
new Rating(['six_star' => 5]); // throws: Invalid rating key: six_star

// Invalid colour values are safely rejected and fall back to the default
new Rating($ratings, ['color' => 'red; } body { display:none']); // uses #ffc107

// Wrong option name (silently ignored)
new Rating($ratings, ['colour' => '#fff']); // Should be 'color'

// Render without the per-instance <style> block.
echo (new Rating($data, ['type' => 'bars', 'inline_styles' => false]))->render();

echo (new Rating($data, ['instance_id' => 'product-42', 'inline_styles' => false]))->render();

echo $rating->render('html'); // Default star display

echo $rating->render('html', ['type' => 'bars']);

echo $rating->render('svg'); // A standalone <svg> element

echo $rating->render('json');
// {
//   "average": 3.67,
//   "total": 150,
//   "distribution": {
//     "one_star": 6.67,
//     "two_star": 13.33,
//     ...
//   }
// }

use Emleons\SimRating\Enum\DisplayType;

echo $rating->render(DisplayType::Json);

// A one-off colour change that does not affect future renders.
echo $rating->render('html', ['color' => '#e74c3c']);

// Throw unless all five buckets are provided.
$rating = new Rating($data, ['strict' => true]);

// In controller
public function show(Product $product)
{
    return view('products.show', [
        'rating' => new \Emleons\SimRating\Rating($product->ratings)
    ]);
}

// In Blade
{!! $rating->render() !!}

// In controller
public function show(Product $product): Response
{
    return $this->render('product/show.html.twig', [
        'rating' => new \Emleons\SimRating\Rating($product->getRatings())
    ]);
}

// In Twig
{{ rating.render()|raw }}

// In your Controller:
public function showProduct($productId)
{
    $productModel = new \App\Models\ProductModel();
    $product = $productModel->find($productId);
    
    $rating = new \Emleons\SimRating\Rating($product['ratings'], [
        'color' => '#f39c12', 
        'size' => '1.5rem'
    ]);
    
    return view('product_view', [
        'product' => $product,
        'ratingStars' => $rating->render('html')
    ]);
}

// In your View (PHP syntax):
<div class="product-rating">
    <?= $ratingStars