PHP code example of loophp / repository-monadic-helper

1. Go to this page and download the library: Download loophp/repository-monadic-helper 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/ */

    

loophp / repository-monadic-helper example snippets




declare(strict_types=1);

namespace App\Repository;

use App\Entity\MyCustomEntity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method MyCustomEntity|null find($id, $lockMode = null, $lockVersion = null)
 * @method MyCustomEntity|null findOneBy(array $criteria, array $orderBy = null)
 * @method MyCustomEntity[]    findAll()
 * @method MyCustomEntity[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class MyCustomEntityRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, MyCustomEntity::class);
    }
}



declare(strict_types=1);

namespace App\Controller;

use App\Entity\MyCustomEntity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use loophp\RepositoryMonadicHelper\MonadicRepositoryFactoryInterface;
use Throwable;

final class MyCustomController
{
    public function __invoke(
        MonadicRepositoryFactoryInterface $monadicRepositoryFactory
    ) {
        $body = $monadicRepositoryFactory
            ->fromEntity(MyCustomEntity::class)
            ->eitherFind(123) // This returns a Either monad.
            ->map(
                static fn (MyCustomEntity $entity): string => $entity->getTitle()
            )
            ->eval(
                static fn (Throwable $exception): string => $exception->getMessage(),
                static fn (string $entity): string => $entity
            );

        return new Response($body);
    }
}



declare(strict_types=1);

namespace App\Repository;

use App\Entity\MyCustomEntity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use loophp\RepositoryMonadicHelper\Doctrine\MonadicServiceEntityRepositoryInterface;
use loophp\RepositoryMonadicHelper\MonadicServiceEntityRepositoryTrait;
use Throwable;

/**
 * @implements MonadicServiceEntityRepositoryInterface<Throwable, MyCustomEntity>
 */
class MyCustomEntityRepository extends ServiceEntityRepository implements MonadicServiceEntityRepositoryInterface
{
    Use MonadicServiceEntityRepositoryTrait;

    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, MyCustomEntity::class);
    }
}



declare(strict_types=1);

namespace App\Repository;

use App\Entity\MyCustomEntity;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use loophp\RepositoryMonadicHelper\MonadicServiceEntityRepository;

/**
 * @extends MonadicServiceEntityRepository<MyCustomEntity>
 */
class MyCustomEntityRepository extends MonadicServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, MyCustomEntity::class);
    }
}

eitherFind(int|string $id): Either<Throwable, MyCustomEntity>

eitherFindAll(): Either<Throwable, list<MyCustomEntity>>

eitherFindBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): Either<Throwable, list<MyCustomEntity>>

eitherFindOneBy(array $criteria): Either<Throwable, MyCustomEntity>