PHP code example of mhndev / rate

1. Go to this page and download the library: Download mhndev/rate 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/ */

    

mhndev / rate example snippets



use mhndev\rate\Abstracts\Entity;
use mhndev\rate\DiscreteNumberValue;
use mhndev\rate\Interfaces\iRateableEntity;

class User
{
    use \mhndev\rate\Traits\UserTrait;

    /**
     * @param $value
     * @param iRateableEntity $entity
     * @param $type
     */
    function doRate($value, iRateableEntity $entity, $type)
    {
        echo 'rate is done in our storage system.';
    }
}

//you can either extend your entity from AbstractEntity class
class Post extends Entity
{

}
//or use EntityTrait in your Entity

class Post implements iRateableEntity
{
    use \mhndev\rate\Traits\EntityTrait;
}


class Comment extends Entity
{

}

//or use EntityTrait in your Entity
class Comment implements iRateableEntity
{
    use \mhndev\rate\Traits\EntityTrait;
}

$rateValue = (new DiscreteNumberValue())->setPossibleValues([1,2,3]);
$post = (new Post())->setRateValue($rateValue);
//or
Post::setRateValue($rateValue);

$post->setPossibleRateTypes(['like', 'rate']);


$rateValue = (new DiscreteNumberValue())->setPossibleValues([-1,1]);
$comment = (new Comment())->setRateValue($rateValue);

//or
Comment::setRateValue($rateValue);
// by default possible rate types are "rate" and "like" so if you want your rate types to be just like default you don't need to call setPossibleRateTypes method on entity pbject

$post->setPossibleRateTypes(['like', 'rate', 'test']);
//or
Post::setPossibleRateTypes(['like', 'rate', 'test']);


$user = new User;

$user->rate(3, $post);
$user->like($post);
$user->rate(1,$comment);
$user->dislike($comment);