<?php

class FileGenerator {

    private $lines = array();

    public function  __construct() {

    }

    public function AddLine($string) {
        $this->lines[] = $string;
    }

    public function GenerateFileContents() {
        $output = null;
        if(count($this->lines)>1) {
            foreach($this->lines as $line) {
                $output .= $line.'\n';
            }
        } else {
            $output = $this->lines[0];
        }

        return $output;
    }

    public function SaveFile($file) {

        if (is_writable($file)) {

            if (!$fh = fopen($file, 'w')) {
                throw new UserException('Nie moge utworzyc pliku: '.$file);
            }
            if (fwrite($fh, $this->GenerateFileContents()) === FALSE) {
                throw new UserException('Nie moge pisac do pliku pliku: '.$file);
            }


            fclose($fh);

        } else {
            throw new UserException('Nie moge pisac do pliku: '.$file);
        }

    }
}

?>