Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Adapter.php
<?php
/**
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the new BSD license.
 *
 * @package     Ace
 * @license     http://www.opensource.org/licenses/BSD-3-Clause New BSD license
 * @since       1.0
 */
abstract class Ace_Form_Adapter extends Ace_Form
{
    /**
     * The form object
     * @var mixed 
     */
    protected $object = null;

    /**
     * True if the form is new
     * @var boolean 
     */
    protected $is_new = true;

    /**
     * Returns the current model name.
     * 
     * @return string
     */
    abstract public function getModelName();

    /**
     * Returns the default connection for the current model.
     *
     * @return mixed A database connection
     */
    abstract public function getConnection();

    /**
     * Updates the values of the object with the cleaned up values.
     *
     * If you want to add some logic before updating or update other associated
     * objects, this is the method to override.
     *
     * @param array $values An array of values
     */
    abstract protected function doUpdateObject($values);

    /**
     * Processes cleaned up values.
     *
     * @param  array $values An array of values
     * @return array An array of cleaned up values
     */
    abstract public function processValues($values);

    /**
     * Retrieves the form object
     *
     * @return mixed
     */
    public function getObject()
    {
        return $this->object;
    }

    /**
     * Camelize a field
     *
     * @param string $text
     * @return string
     */
    protected function camelize($text)
    {
        return preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'), array("'::'.strtoupper('\\1')", "strtoupper('\\2')"), $text);
    }

    /**
     * Returns if the form is new
     *
     * @return boolean
     */
    public function isNew()
    {
        return $this->is_new;
    }

    /**
     * Saves the current object to the database.
     *
     * The object saving is done in a transaction and handled by the doSave() method.
     *
     * @param  mixed $connection An optional connection object
     * @return mixed The current saved object
     */
    public function save($connection = null)
    {
        if(!$this->isValid())
        {
            throw new RuntimeException('The form is not valid, Ace_Form_Adapter cannot do save');
        }

        if(is_null($connection))
        {
            $connection = $this->getConnection();
        }

        try
        {
            $connection->beginTransaction();
            $this->doSave($connection);
            $connection->commit();
        }
        catch(Exception $e)
        {
            $connection->rollBack();
            throw $e;
        }

        return $this->getObject();
    }

    /**
     * Updates and saves the current object.
     *
     * If you want to add some logic before saving or save other associated
     * objects, this is the method to override.
     *
     * @param mixed $connection An optional connection object
     */
    protected function doSave($connection = null)
    {
        if(is_null($connection))
        {
            $connection = $this->getConnection();
        }

        $this->updateObject();
        $this->getObject()->save($connection);
    }

    /**
     * Updates the values of the object with the cleaned up values.
     *
     * @param  array $values An array of values
     * @return mixed The current updated object
     */
    public function updateObject($values = null)
    {
        if(is_null($values))
        {
            $values = $this->values;
        }

        $values = $this->processValues($values);

        $this->doUpdateObject($values);

        return $this->getObject();
    }
}
Return current item: Ace Framework