AbstractWriter.php 1.41 KiB
<?php
/*
 * © 2016 IRSTEA
 * Guillaume Perréal <guillaume.perreal@irstea.fr>
 * Tous droits réservés.
 */
namespace Irstea\PlantUmlBundle\Writer;
/**
 * Description of AbstractWriter
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
abstract class AbstractWriter implements WriterInterface
    /**
     * @var int
    private $indentation = 0;
    /**
     * @var string
    private $buffer;
    public function dedent($n = 1)
        $this->indentation = max(0, $this->indentation - $n);
        return $this;
    public function indent($n = 1)
        $this->indentation += $n;
        return $this;
    public function writeFormatted($format)
        $args = func_get_args();
        array_shift($args);
        $this->write(vsprintf($format, $args));
        return $this;
    public function write($data)
        $buffer = $this->buffer . $data;
        $indentation = str_repeat("  ", $this->indentation);
        $current = 0;
        while($current < strlen($buffer) && false !== ($next = strpos($buffer, "\n", $current))) {
            $next++;
            $this->doWrite($indentation . substr($buffer, $current, $next - $current));
            $current = $next;
        $this->buffer = substr($buffer, $current);
        return $this;
    /**
     * @param string $data
    abstract protected function doWrite($data);