PHP code example of cjmellor / engageify

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

    

cjmellor / engageify example snippets




use Cjmellor\Engageify\Concerns\HasEngagements;

class BlogPost extends Model
{
    use HasEngagements;

    // ...
}

// Like
$post->like();

// Dislike
$post->dislike();

// Upvote
$post->upvote();

// Downvote
$post->downvote();

use Cjmellor\Engageify\Contracts\EngagementType;

enum Reaction: string implements EngagementType
{
    case Bookmark = 'bookmark';
    case Celebrate = 'celebrate';
}

// config/engageify.php
'types' => App\Enums\Reaction::class,

$post->engage(Reaction::Bookmark);

$post->engagementCount(Reaction::Bookmark); // 1

$post->disengage(Reaction::Bookmark);

// config/engageify.php
'types' => [
    App\Enums\Stars::class,   // Rateable
    App\Enums\Vote::class,    // Exclusive + HasWeight
    App\Enums\Reaction::class, // plain (like, bookmark…)
],

$film->engage(Stars::Rating, 4);
$film->engage(Vote::Up);
$film->like();

enum Reaction: string implements EngagementType
{
    case ThumbsUp = '👍';
    case Tada = '🎉';
}

$post->engage(Reaction::Bookmark, actor: $user);
$post->disengage(Reaction::Bookmark, actor: $user);

$post->like(actor: $user);
$post->unlike(actor: $user);
$post->toggleLike(actor: $user);

use Cjmellor\Engageify\Contracts\EngagementType;
use Cjmellor\Engageify\Contracts\HasWeight;

enum Vote: string implements EngagementType, HasWeight
{
    case Up = 'up';
    case Down = 'down';

    public function weight(): int
    {
        return match ($this) {
            self::Up => 1,
            self::Down => -1,
        };
    }
}

$post->engage(Vote::Up);   // stores the derived value 1
$post->engage(Vote::Down); // stores the derived value -1

$post->score(Vote::Up);     // total stored weight for upvotes
$post->averageOf(Vote::Up); // mean stored weight for upvotes

use Cjmellor\Engageify\Contracts\EngagementType;
use Cjmellor\Engageify\Contracts\Exclusive;
use Cjmellor\Engageify\Contracts\HasWeight;

enum Vote: string implements EngagementType, Exclusive, HasWeight
{
    case Up = 'up';
    case Down = 'down';

    public function group(): string
    {
        return 'vote';
    }

    public function weight(): int
    {
        return match ($this) {
            self::Up => 1,
            self::Down => -1,
        };
    }
}

$post->engage(Vote::Down); // down
$post->engage(Vote::Up);   // switches: down removed, up recorded (one transaction)
$post->engage(Vote::Up);   // re-recording the active member toggles it off

$post->netScore('vote');   // e.g. 42
$post->breakdown('vote');  // ['up' => 50, 'down' => 8]

use Cjmellor\Engageify\Contracts\EngagementType;
use Cjmellor\Engageify\Contracts\Rateable;

enum Rating: string implements EngagementType, Rateable
{
    case Stars = 'stars';

    public function min(): float
    {
        return 1.0;
    }

    public function max(): float
    {
        return 5.0;
    }

    public function step(): ?float
    {
        return 1.0;
    }
}

$film->engage(Rating::Stars, 4);   // stored, validated against 1–5 step 1
$film->engage(Rating::Stars, 6);   // throws InvalidRatingException (out of range)
$film->engage(Rating::Stars, 2.5); // throws InvalidRatingException (off step)

$film->averageRating(Rating::Stars);     // mean rating
$film->ratingCount(Rating::Stars);       // number of ratings
$film->ratingDistribution(Rating::Stars); // ['5.00' => 12, '4.00' => 3, ...]
$film->bayesianAverage(Rating::Stars);    // mean pulled toward the global average for low-count items

$comment->unlike();

$comment->toggleLike();

// Likes
$post->likes();

// Dislikes
$post->dislikes();

// Upvotes
$post->upvotes();

// Downvotes
$post->downvotes();

// Most-liked posts first
Post::query()->orderByEngagementCount(EngagementTypes::Like)->get();

// Highest net vote score first
Post::query()->orderByScore('vote')->get();

// Hot — net score balanced against age (stored, time-anchored; no cron)
Post::query()->hot()->get();

// Top — items created within a window, ranked by net score ('all' = no window)
Post::query()->top('week')->get();

// Bayesian — honest average that pulls low-count items toward the global mean
Film::query()->orderByBayesian(Rating::Stars)->get();

use Cjmellor\Engageify\Concerns\EngagesWith;

class User extends Authenticatable
{
    use EngagesWith;
}

$user->hasEngaged($post, EngagementTypes::Like);     // bool
$user->engagementValueFor($post, Vote::Up);          // the stored value, or null
$user->ratingFor($film, Rating::Stars);              // alias for engagementValueFor
$user->engagements;                                  // every engagement this user authored

$feed = Post::query()->withUserEngagement(EngagementTypes::Like)->get();

$feed->first()->is_engaged; // true/false — no extra query per row

$post->likes(showUsers: true);

use Cjmellor\Engageify\Concerns\HasViews;

class Thread extends Model
{
    use HasViews;
}

$thread->recordView();          // counts once per viewer per cooldown window
$thread->viewCount();           // lifetime total
Thread::query()->orderByMostViewed()->get(); // most viewed, all-time

$thread->viewsInLast(7);                            // views in the last 7 days
Thread::query()->orderByMostViewed('week')->get();  // most viewed this week

public Model $actor,
public Model $engageable,
public EngagementType $type,
public Engagement $engagement,

public Model $actor,
public Model $engageable,
public EngagementType $type,
bash
php artisan vendor:publish --tag="engageify-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="engageify-config"
bash
   php artisan vendor:publish --tag="engageify-migrations"
   php artisan migrate
   
bash
   php artisan engageify:recount