Location: PHPKode > projects > Moc10 PHP Library > library/Moc10/Pdf/Object.php
<?php
/**
 * Moc10 Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.TXT.
 * It is also available through the world-wide-web at this URL:
 * http://www.moc10phplibrary.com/LICENSE.TXT
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @category   Moc10
 * @package    Moc10_Pdf
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 */

/**
 * Moc10_Pdf_Object
 *
 * @category   Moc10
 * @package    Moc10_Pdf
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 * @version    1.9.7
 */

class Moc10_Pdf_Object
{

    /**
     * PDF object index
     * @var int
     */
    public $index = null;

    /**
     * PDF object data
     * @var string
     */
    protected $_data = null;

    /**
     * PDF object definition
     * @var string
     */
    protected $_def = null;

    /**
     * PDF object stream
     * @var string
     */
    protected $_stream = null;

    /**
     * Constructor
     *
     * Instantiate a PDF object.
     *
     * @param  int|string $i
     * @return void
     */
    public function __construct($i)
    {

        // Use default settings for a new PDF object.
        if (is_int($i)) {

            $this->index = $i;
            $this->_data = "\n[{obj_index}] 0 obj\n[{obj_def}]\n[{obj_stream}]\nendobj\n\n";

        } else if (is_string($i)) {

            // Else, determine the object index.
            $this->index = substr($i, 0, strpos($i, ' '));

            // Determine the objects definition and stream, if applicable.
            $s = substr($i, (strpos($i, ' obj') + 4));
            $s = substr($s, 0, strpos($s, 'endobj'));
            if (strpos($s, 'stream') !== false) {
                $def = substr($s, 0, strpos($s, 'stream'));
                $str = substr($s, (strpos($s, 'stream') + 6));
                $str = substr($str, 0, strpos($str, 'endstream'));
                $this->define($def);
                $this->setStream($str);
            } else {
                $this->define($s);
            }

            $this->_data = "\n[{obj_index}] 0 obj\n[{obj_def}]\n[{obj_stream}]\nendobj\n\n";

        }

    }

    /**
     * Method to print the PDF object.
     *
     * @return str
     */
    public function __toString()
    {

        $matches = array();

        // Set the content stream.
        $stream = (!is_null($this->_stream)) ? "stream" . $this->_stream . "endstream\n" : '';

        // Set up the Length definition.
        if (strpos($this->_def, '/Length ') !== false) {
            preg_match('/\/Length\s\d*/', $this->_def, $matches);
            if (isset($matches[0])) {
                $len = $matches[0];
                $len = str_replace('/Length', '', $len);
                $len = str_replace(' ', '', $len);
                $this->_def = str_replace($len, '[{byte_length}]', $this->_def);
            }
        } else {
            $this->_def .= "<</Length [{byte_length}]>>\n";
        }

        // Calculate the byte length of the content stream and swap out the placeholders.
        $byteLength = $this->_calcByteLength($this->_stream);
        $data = str_replace('[{obj_index}]', $this->index, $this->_data);
        $data = str_replace('[{obj_stream}]', $stream, $data);
        $data = str_replace('[{obj_def}]', $this->_def, $data);
        $data = str_replace('[{byte_length}]', $byteLength, $data);

        // Clear Length definition if it is zero.
        if (strpos($data, '<</Length 0>>') !== false) {
            $data = str_replace('<</Length 0>>', '', $data);
        }

        return $data;

    }

    /**
     * Method to define the PDF object.
     *
     * @param  string $str
     * @return void
     */
    public function define($str)
    {

        $this->_def = $str;

    }

    /**
     * Method to return the PDF object definition.
     *
     * @return string
     */
    public function getDef()
    {

        return $this->_def;

    }

    /**
     * Method to set the stream the PDF object.
     *
     * @param  string $str
     * @return void
     */
    public function setStream($str)
    {

        $this->_stream .= $str;

    }

    /**
     * Method to return the PDF object stream.
     *
     * @return string
     */
    public function getStream()
    {

        return $this->_stream;

    }

    /**
     * Method to get the PDF object byte length.
     *
     * @param  string $str
     * @return void
     */
    public function getByteLength()
    {

        return $this->_calcByteLength($this);

    }

    /**
     * Method to calculate the byte length.
     *
     * @param  string $str
     * @return int
     */
    protected function _calcByteLength($str)
    {

        $bytes = str_replace("\n", "", $str);
        return strlen($bytes);

    }

}
Return current item: Moc10 PHP Library