przewiń do treści

PHP, MySQL i MVC. Tworzenie witryn WWW opartych na bazie danych

Włodzimierz Gajda

class TXTArticle
{
    ...
    
    public function __construct($ADirectory)
    {
        $this->directory = $ADirectory;

        $this->loadArticle();
        $this->loadDate();
        $this->loadIntroduction();
        $this->loadTitle();

        $this->loadFigures();
        $this->loadListings();
        $this->loadFrames();
        $this->loadTables();

        $this->loadExamples();
        $this->loadDownload();
        $this->loadAddresses();
    }
    ...
    
    protected function loadArticle()
    {
        $filename = $this->directory . '/article.txt';

        if (file_exists($filename)) {
            $this->article = trim(file_get_contents($filename));
        }
    }
    ...

    protected function loadFigures()
    {
        $this->figures_count = 0;
        $this->figures = array();

        $filename = $this->directory . '/figures.txt';

        if (file_exists($filename)) {
            $tmp = trim(file_get_contents($filename));
            $d = string2HArray($tmp);
            $this->figures_count = $d['rows'];
            for ($i = 0; $i < $this->figures_count; $i++) {
                $tmp_figure = array();
                $tmp_figure['no'] = $i + 1;
                $tmp_figure['ext'] = $d['items'][$i][1];
                $tmp_figure['filename'] = $tmp_figure['no'] . '.' . $tmp_figure['ext'];
                $tmp_figure['caption'] = $d['items'][$i][0];
                $tmp_figure['image'] = file_get_contents(
                    $this->directory . '/figures/' . $tmp_figure['filename']
                );
                $tmp_figure['image'] = base64_encode($tmp_figure['image']);

                $this->figures[$i + 1] = $tmp_figure;
            }
        }
    }
    ...

    public function getXml()
    {
        $serializer = new XML_Serializer();

        $options = array(
            'addDecl'  => true,
            'indent'   => '    ',
            'rootName' => 'artykul',
            'encoding' => 'utf-8',
            'defaultTagName' => 'item',
        );

        $serializer->setOptions($options);
        $serializer->serialize($this);
        $this->xml = $serializer->getSerializedData();
        return $this->xml;
    }

    public function saveXml($filename, $compress = true)
    {
        if ($compress) {
            if (substr($filename, -3) != '.gz') {
                $filename .= '.gz';
            }
            $gzfile = gzopen($filename, 'w9');
            if ($gzfile === false) {
                throw new Exception('TxtArticle::saveXML() exception');
            }
            if ($this->xml == '') {
                $this->getXml();
            }
            gzwrite($gzfile, $this->xml);
            gzclose($gzfile);
        } else {
            if ($this->xml == '') {
                $this->getXml();
            }
            file_put_contents($filename, $this->xml);
        }
    }

}

Listing 31.4. Zarys klasy TXTArticle

Rozdział 31. Skompresowane dokumenty XML zawierające dane binarne

listing-31-04.txt

Reklama

Szkolenia z Symfony 2.0