<?xml version="1.0" encoding="UTF-8"?>
    <!-- EN-Revision: 24249 -->
    <!-- Reviewed: no -->
<sect1 id="zend.codegenerator.examples">
    <title>Ejemplos de Zend_CodeGenerator</title>

    <example id="zend.codegenerator.examples.class">
        <title>Generando clases PHP</title>

        <para>El siguiente ejemplo genera una clase vacía con una clase de
            nivel DocBlock.</para>

        <programlisting language="php"><![CDATA[
$foo      = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
    'shortDescription' => 'Sample generated class',
    'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
    'tags'             => array(
        array(
            'name'        => 'version',
            'description' => '$Rev:$',
        ),
        array(
            'name'        => 'license',
            'description' => 'New BSD',
        ),
    ),
));
$foo->setName('Foo')
    ->setDocblock($docblock);
echo $foo->generate();
]]></programlisting>

        <para>El código anterior resultará en lo siguiente:</para>

        <programlisting language="php"><![CDATA[
/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 *
 */
class Foo
{

}
]]></programlisting>
    </example>

    <example id="zend.codegenerator.examples.class-properties">
        <title>Generando clases PHP con propiedades de clase</title>

        <para>Basándonos en el ejemplo anterior, ahora agreguemos propiedades a
            nuestra clase
            generada.</para>

        <programlisting language="php"><![CDATA[
$foo      = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
    'shortDescription' => 'Sample generated class',
    'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
    'tags'             => array(
        array(
            'name'        => 'version',
            'description' => '$Rev:$',
        ),
        array(
            'name'        => 'license',
            'description' => 'New BSD',
        ),
    ),
));
$foo->setName('Foo')
    ->setDocblock($docblock)
    ->setProperties(array(
        array(
            'name'         => '_bar',
            'visibility'   => 'protected',
            'defaultValue' => 'baz',
        ),
        array(
            'name'         => 'baz',
            'visibility'   => 'public',
            'defaultValue' => 'bat',
        ),
        array(
            'name'         => 'bat',
            'const'        => true,
            'defaultValue' => 'foobarbazbat',
        ),
    ));
echo $foo->generate();
]]></programlisting>

        <para>Lo anterior resulta en la siguiente definición de clase:</para>

        <programlisting language="php"><![CDATA[
/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 *
 */
class Foo
{

    protected $_bar = 'baz';

    public $baz = 'bat';

    const bat = 'foobarbazbat';

}
]]></programlisting>
    </example>

    <example id="zend.codegenerator.examples.class-methods">
        <title>Generando clases PHP con métodos de clase</title>

        <para>
            <classname>Zend_CodeGenerator_Php_Class</classname>
            le permite
            adjuntar métodos con contenido opcional a sus clases. Los métodos
            pueden
            adjuntarse tanto como arrys o como instancias concretas de
            <classname>Zend_CodeGenerator_Php_Method</classname>
            .
        </para>

        <programlisting language="php"><![CDATA[
$foo      = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
    'shortDescription' => 'Sample generated class',
    'longDescription'  => 'This is a class generated with Zend_CodeGenerator.',
    'tags'             => array(
        array(
            'name'        => 'version',
            'description' => '$Rev:$',
        ),
        array(
            'name'        => 'license',
            'description' => 'New BSD',
        ),
    ),
));
$foo->setName('Foo')
    ->setDocblock($docblock)
    ->setProperties(array(
        array(
            'name'         => '_bar',
            'visibility'   => 'protected',
            'defaultValue' => 'baz',
        ),
        array(
            'name'         => 'baz',
            'visibility'   => 'public',
            'defaultValue' => 'bat',
        ),
        array(
            'name'         => 'bat',
            'const'        => true,
            'defaultValue' => 'foobarbazbat',
        ),
    ))
    ->setMethods(array(
        // Método pasado como array
        array(
            'name'       => 'setBar',
            'parameters' => array(
                array('name' => 'bar'),
            ),
            'body'       => '$this->_bar = $bar;' . "\n" . 'return $this;',
            'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
                'shortDescription' => 'Set the bar property',
                'tags'             => array(
                    new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
                        'paramName' => 'bar',
                        'datatype'  => 'string'
                    )),
                    new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                        'datatype'  => 'string',
                    )),
                ),
            )),
        ),
        // Método pasado como instancia concreta
        new Zend_CodeGenerator_Php_Method(array(
            'name' => 'getBar',
            'body'       => 'return $this->_bar;',
            'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
                'shortDescription' => 'Retrieve the bar property',
                'tags'             => array(
                    new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                        'datatype'  => 'string|null',
                    )),
                ),
            )),
        )),
    ));

echo $foo->generate();
]]></programlisting>

        <para>Lo anterior genera la siguiente salida:</para>

        <programlisting language="php"><![CDATA[
/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 */
class Foo
{

    protected $_bar = 'baz';

    public $baz = 'bat';

    const bat = 'foobarbazbat';

    /**
     * Set the bar property
     *
     * @param string bar
     * @return string
     */
    public function setBar($bar)
    {
        $this->_bar = $bar;
        return $this;
    }

    /**
     * Retrieve the bar property
     *
     * @return string|null
     */
    public function getBar()
    {
        return $this->_bar;
    }

}
]]></programlisting>
    </example>

    <example id="zend.codegenerator.examples.file">
        <title>Generando archivos PHP</title>

        <para>
            <classname>Zend_CodeGenerator_Php_File</classname>
            puede ser
            utilizada para generar el contenido de un archivo
            <acronym>PHP</acronym>
            . Usted puede incluir clases, así como el
            contenido arbitrario del cuerpo. Cuando acople
            clases, debe adjuntar
            instancias concretas de
            <classname>Zend_CodeGenerator_Php_Class</classname>
            o un array
            definiendo la clase.
        </para>

        <para>
            En el ejemplo siguiente, asumiremos que ha definido
            <varname>$foo</varname>
            como una de las definiciones de clase
            del ejemplo anterior.
        </para>

        <programlisting language="php"><![CDATA[
$file = new Zend_CodeGenerator_Php_File(array(
    'classes'  => array($foo);
    'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
        'shortDescription' => 'Foo class file',
        'tags'             => array(
            array(
                'name'        => 'license',
                'description' => 'New BSD',
            ),
        ),
    )),
    'body'     => 'define(\'APPLICATION_ENV\', \'testing\');',
));
]]></programlisting>

        <para>
            Llamando a
            <methodname>generate()</methodname>
            generará el código
            -- pero no lo grabará en un archivo. Usted mismo deberá capturar y
            grabar los contenidos en un archivo. Por ejemplo:
        </para>

        <programlisting language="php"><![CDATA[
$code = $file->generate();
file_put_contents('Foo.php', $code);
]]></programlisting>

        <para>Lo anterior generará el siguiente archivo:</para>

        <programlisting language="php"><![CDATA[
<?php
/**
 * Foo class file
 *
 * @license New BSD
 */

/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 */
class Foo
{

    protected $_bar = 'baz';

    public $baz = 'bat';

    const bat = 'foobarbazbat';

    /**
     * Set the bar property
     *
     * @param string bar
     * @return string
     */
    public function setBar($bar)
    {
        $this->_bar = $bar;
        return $this;
    }

    /**
     * Retrieve the bar property
     *
     * @return string|null
     */
    public function getBar()
    {
        return $this->_bar;
    }

}

define('APPLICATION_ENV', 'testing');
]]></programlisting>
    </example>

    <example id="zend.codegenerator.examples.reflection-file">
        <title>Sembrando la generación de código para un archivo PHP via
            reflection</title>
        <para>
            You can add
            <acronym>PHP</acronym>
            code to an existing
            <acronym>PHP</acronym>
            file using the code generator. To do so,
            you need to first do reflection on it. The
            static method
            <methodname>fromReflectedFileName()</methodname>
            allows you to
            do this.
        </para>

        <programlisting language="php"><![CDATA[
$generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
$body = $generator->getBody();
$body .= "\n\$foo->bar();";
file_put_contents($path, $generator->generate());
]]></programlisting>
    </example>

    <example id="zend.codegenerator.examples.reflection-class">
        <title>Sembrando la generación de clases PHP via reflection</title>
        <para>
            You may add code to an existing class. To do so, first use the
            static
            <methodname>fromReflection()</methodname>
            method to map the
            class into a generator object. From there, you may add additional
            properties or methods, and then regenerate the class.
        </para>

        <programlisting language="php"><![CDATA[
$generator = Zend_CodeGenerator_Php_Class::fromReflection(
    new Zend_Reflection_Class($class)
);
$generator->setMethod(array(
    'name'       => 'setBaz',
    'parameters' => array(
        array('name' => 'baz'),
    ),
    'body'       => '$this->_baz = $baz;' . "\n" . 'return $this;',
    'docblock'   => new Zend_CodeGenerator_Php_Docblock(array(
        'shortDescription' => 'Set the baz property',
        'tags'             => array(
            new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
                'paramName' => 'baz',
                'datatype'  => 'string'
            )),
            new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                'datatype'  => 'string',
            )),
        ),
    )),
));
$code = $generator->generate();
]]></programlisting>
    </example>

    <example id="zend.codegenerator.examples.reflection-method">
        <title>Sembrando la generación de métodos PHP via reflection</title>
        <para>
            You may add code to an existing class. To do so, first use the
            static
            <methodname>fromReflection()</methodname>
            method to map the
            class into a generator object. From there, you may add additional
            properties or methods, and then regenerate the class.
        </para>
    </example>
</sect1>
