PHP code example of saxulum / saxulum-model-importer

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

    

saxulum / saxulum-model-importer example snippets

{.php}
class Reader implements ReaderInterface
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @ReaderModelInterface[]|array
     */
    public function getReaderModels($offset, $limit)
    {
        $qb = $this->em->getRepository(ReaderEntity::class)->createQueryBuilder('r');
        $qb->setFirstResult($offset);
        $qb->setMaxResults($limit);

        return $qb->getQuery()->getResult();
    }

    public function clearReaderModels()
    {
        $this->em->clear(ReaderEntity::class);
    }
}
{.php}
class ReaderEntity implements ReaderModelInterface
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var string
     */
    protected $name;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return int
     */
    public function getImportIdentifier()
    {
        return $this->getId();
    }
}
{.php}
class Writer implements WriterInterface
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @param ReaderModelInterface $readerModel
     *
     * @return WriterModelInterface|null
     */
    public function findWriterModel(ReaderModelInterface $readerModel)
    {
        return $this->em->getRepository(WriterEntity::class)
            ->findOneBy(['importIdentifier' => $readerModel->getImportIdentifier()]);
    }

    /**
     * @param ReaderModelInterface $readerModel
     *
     * @return WriterModelInterface
     *
     * @throws NotImportableException
     */
    public function createWriterModel(ReaderModelInterface $readerModel)
    {
        $writerModel = new WriterEntity();
        $writerModel->setName($readerModel->getName());

        return $writerModel;
    }

    /**
     * @param WriterModelInterface $writerModel
     * @param ReaderModelInterface $readerModel
     *
     * @throws NotImportableException
     */
    public function updateWriterModel(WriterModelInterface $writerModel, ReaderModelInterface $readerModel)
    {
        $writerModel->setName($readerModel->getName());
    }

    /**
     * @param WriterModelInterface $writerModel
     *
     * @throws NotImportableException
     */
    public function persistWriterModel(WriterModelInterface $writerModel)
    {
        $this->em->persist($writerModel);
    }

    public function flushWriterModels(array $writeModels)
    {
        $this->em->flush($writeModels);
    }

    public function clearWriterModels()
    {
        $this->em->clear(WriterEntity::class);
    }

    /**
     * @param \DateTime $lastImportDate
     */
    public function removeWriterModels(\DateTime $lastImportDate)
    {
        $qb = $this->em->createQueryBuilder();
        $qb->delete(WriterEntity::class, 'w');
        $qb->where(
            $qb->expr()->orX(
                $qb->expr()->isNull('w.lastImportDate'),
                $qb->expr()->neq('w.lastImportDate', ':lastImportDate')
            )
        );
        $qb->setParameter('lastImportDate', $lastImportDate);

        $qb->getQuery()->execute();
    }
}
{.php}
class WriterEntity implements WriterModelInterface
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var string
     */
    protected $name;

    /**
     * @var int
     */
    protected $importIdentifier;

    /**
     * @var \DateTime
     */
    protected $lastImportDate;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param int $importIdentifier
     */
    public function setImportIdentifier($importIdentifier)
    {
        $this->importIdentifier = $importIdentifier;
    }

    /**
     * @return int
     */
    public function getImportIdentifier()
    {
        return $this->importIdentifier;
    }

    /**
     * @param \DateTime $lastImportDate
     */
    public function setLastImportDate(\DateTime $lastImportDate)
    {
        $this->lastImportDate = $lastImportDate;
    }

    /**
     * @return \DateTime
     */
    public function getLastImportDate()
    {
        return $this->lastImportDate;
    }
}