PHP code example of opengento / module-document

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

    

opengento / module-document example snippets



namespace Vendor\Module\Model\Document\Processor;

use Opengento\Document\Api\Data\DocumentInterface;
use Opengento\Document\Api\Data\DocumentTypeInterface;
use Opengento\Document\Model\Document\Filesystem\File;
use Opengento\Document\Model\Document\Filesystem\Format;
use Opengento\Document\Model\Document\ProcessorInterface;
use Opengento\Document\Model\DocumentBuilder;
use function basename;
use function dirname;

final class CustomProcessor implements ProcessorInterface
{
    public const CODE = 'custom';

    /**
     * @var DocumentBuilder
     */
    private $documentBuilder;

    /**
     * @var File
     */
    private $fileHelper;

    public function __construct(
        DocumentBuilder $documentBuilder,
        File $fileHelper
    ) {
        $this->documentBuilder = $documentBuilder;
        $this->fileHelper = $fileHelper;
    }

    public function execute(DocumentTypeInterface $documentType, string $filePath): DocumentInterface
    {
        // $filePath is the path where the source file is currently saved.
        // You can change the destination path if want to.
        // Edit the file path value with $this->documentBuilder->setFilePath($newDestPath).
        // You can also rename the file with $this->documentBuilder->setFileName($newFileName)

        $destFilePath = $this->fileHelper->getFileDestPath($documentType, $filePath);
        $fileName = basename($destFilePath);

        $this->documentBuilder->setTypeId($documentType->getId());
        $this->documentBuilder->setCode(Format::formatCode($fileName));
        $this->documentBuilder->setName(Format::formatName($fileName));
        $this->documentBuilder->setFileName($fileName);
        $this->documentBuilder->setFilePath(dirname($this->fileHelper->getRelativeFilePath($destFilePath)));

        return $this->documentBuilder->create();
    }
}