<?php

declare(strict_types=1);

namespace App\Service\Export\Encoder;

use App\Enum\Export\ExportFormatEnum;
use Phpdocx\Create\CreateDocx;

/**
 * Encode HTML to docx format
 */
class DocXEncoder implements EncoderInterface
{

  public function support(string $format): bool
  {
    return $format === ExportFormatEnum::DOCX()->getValue();
  }

  /**
   * Encode the given HTML content into docX, and
   * return the encoded content
   *
   * @param string $html
   * @param array<mixed> $options
   * @return string
   */
  public function encode(string $html, array $options = []): string
  {
    $docx = new CreateDocx();
    $docx->addText($html);
    $tempFile = tempnam(sys_get_temp_dir(), 'docx');
    $docx->createDocx($tempFile);
    $docxContent = file_get_contents($tempFile);
    unlink($tempFile);
    return $docxContent;
  }
}
