PHP code example of snowbuilds / laravel-mirror

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

    

snowbuilds / laravel-mirror example snippets


use SnowBuilds\Mirror\Concerns\Recommendations;
use SnowBuilds\Mirror\Mirror;

class Post extends Model
{
    use Recommendations;

    public function registerRecommendations(): void
    {
        $this->registerStrategy(Post::class)
            ->levenshtein('title');
    }
}

public function registerRecommendations(): void
{
    $this->registerStrategy(Post::class)
        ->levenshtein('title', 2)
        ->euclidean('tags', 1);
}

class User extends Model
{
    use Recommendations;

    public function registerRecommendations(): void
    {
        $this->registerStrategy(Post::class)
            ->levenshtein('biography', 'title', 1) // compare biography to post title
            ->euclidean('communities', 'tags', 3); // compare communities to post tags
    }
}

class User extends Model
{
    public function registerRecommendations(): void
    {
        $this->registerStrategy(Post::class)
            ->using(function (User $a, Post $b) {
                return Algorithm::levenshtein($a->name, $b->name);
            });
    }
}

public function registerRecommendations(): void
{
    $this->registerStrategy(Post::class)
        ->using(function ($a, $b) {
            return Algorithm::levenshtein($a->title, $b->title);
        })
        ->using(function ($a, $b) {
            return Algorithm::euclidean($a->tags, $b->tags);
        })
        ->weights([2,1]);
}


public function registerRecommendations(): void
{
    $this->registerStrategy(Post::class)
        ->using([
            'titles' => fn ($a, $b) => Algorithm::levenshtein($a->title, $b->title),
            'tags' => fn ($a, $b) => Algorithm::levenshtein($a->tags, $b->tags),
        ])
        ->weights([
            'titles' => 2,
            'tags' => 1,
        ]);
}

// ServiceProvider.php
ScoringStrategy::macro('huggingFace', function (...$args) {
  return $this->registerAlgorithm(
    fn($a, $b) => HuggingFace::invokeEmbedding($a, $b),
    ...$args
  );
});

// Model.php
class User extends Model 
{
    public function registerRecommendations(): void
    {
        $this->registerStrategy(User::class)
            ->euclidean('follewers')
            ->huggingFace('activity')
            ->levenshtein('bio');
    }
}

class User extends Authenticatable
{
    use Recommendations;

    public function recommendedRecipes() {
        return $this->morphRecommendation(Recipe::class);
    }
}

class Kernel extends ConsoleKernel
{
    protected function schedule(Schedule $schedule): void
    {
        $schedule->command('mirror:sync')->daily();
    }
}
bash
php artisan vendor:publish --provider="SnowBuilds\Mirror\MirrorServiceProvider"
bash
php artisan mirror:sync