<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Rvf;

use Kufer\Web\Classes\Converter;

class TextToRvfConverter implements Converter\TextConverter, Converter\RvfConverter
{
  
  protected $text;

  public function __construct(string $text)
  {
    $this->text = $text;
  }

  public function toRvf(): string
  {
    
    $paragraphEnd = mb_convert_encoding('&#x2029;', 'UTF-16LE', 'HTML-ENTITIES');
    $text = "-8 1 3 1\r\n"; 
    $ary = \explode("\n", \StringOperations::str_fast_parallel_replace($this->text, array("\r" => '')));
    $arycnt = \count($ary);
    $text .= "0 $arycnt 0 8 0 0\r\n";
    foreach ($ary as $part) {
      $text .= \mb_convert_encoding($part, 'UTF-16LE', \CodingOperations::getInternalEncoding())
        . $paragraphEnd;
    }

    return $text;
  }

  public function toText(): string
  {
    return $this->text;
  }
}