Source for file Filter.php

Documentation is available at Filter.php

  1. <?php
  2. /*
  3.  *  $Id: Record.php 1298 2007-05-01 19:26:03Z zYne $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21.  
  22. /**
  23.  * Doctrine_Record_Filter
  24.  * Filters and prepares the record data
  25.  *
  26.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  27.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  28.  * @package     Doctrine
  29.  * @category    Object Relational Mapping
  30.  * @link        www.phpdoctrine.com
  31.  * @since       1.0
  32.  * @version     $Revision: 1298 $
  33.  */
  34. {
  35.     /**
  36.      * @var Doctrine_Record $_record        the record object this filter belongs to
  37.      */
  38.     protected $_record;
  39.     /**
  40.      * constructor
  41.      *
  42.      * @param Doctrine_Record $_record      the record object this filter belongs to
  43.      */
  44.     public function __construct(Doctrine_Record $record)
  45.     {
  46.         $this->_record = $record;
  47.     }
  48.     /**
  49.      * getRecord
  50.      *
  51.      * @return Doctrine_Record $_record     the record object this filter belongs to
  52.      */
  53.     public function getRecord()
  54.     {
  55.         return $this->_record;
  56.     }
  57.     /**
  58.      * setDefaultValues
  59.      * sets the default values for records internal data
  60.      *
  61.      * @param boolean $overwrite                whether or not to overwrite the already set values
  62.      * @return boolean 
  63.      */
  64.     public function assignDefaultValues($data$overwrite false)
  65.     {
  66.         $table $this->_record->getTable();
  67.  
  68.         if $table->hasDefaultValues()) {
  69.             return false;
  70.         }
  71.         $modified array();
  72.         foreach ($data as $column => $value{
  73.             $default $table->getDefaultValueOf($column);
  74.  
  75.             if ($default === null{
  76.                 $default self::$_null;
  77.             }
  78.  
  79.             if ($value === self::$_null || $overwrite{
  80.                 $this->_record->rawSet($column$default);
  81.                 $modified[]    $column;
  82.                 $this->_record->state(Doctrine_Record::STATE_TDIRTY);
  83.             }
  84.         }
  85.         $this->_record->setModified($modified);
  86.     }
  87.     /**
  88.      * prepareIdentifiers
  89.      * prepares identifiers for later use
  90.      *
  91.      * @param boolean $exists               whether or not this record exists in persistent data store
  92.      * @return void 
  93.      */
  94.     private function prepareIdentifiers($exists true)
  95.     {
  96.         $id $this->_table->getIdentifier();
  97.         $this->_id   array();
  98.         if (count($id1{
  99.             foreach ($id as $name{
  100.                 if ($this->_data[$name=== self::$_null{
  101.                     $this->_id[$namenull;
  102.                 else {
  103.                     $this->_id[$name$this->_data[$name];
  104.                 }
  105.             }
  106.         else {
  107.             if (isset($this->_data[$id]&& $this->_data[$id!== self::$_null{
  108.                 $this->_id[$id$this->_data[$id];
  109.             }
  110.         }
  111.     }
  112.     /**
  113.      * getPrepared
  114.      *
  115.      * returns an array of modified fields and values with data preparation
  116.      * adds column aggregation inheritance and converts Records into primary key values
  117.      *
  118.      * @param array $array 
  119.      * @return array 
  120.      */
  121.     public function getPrepared(array $array array()) {
  122.         $a array();
  123.  
  124.         if (empty($array)) {
  125.             $array $this->_modified;
  126.         }
  127.         foreach ($array as $k => $v{
  128.             $type $this->_table->getTypeOf($v);
  129.  
  130.             if ($this->_data[$v=== self::$_null{
  131.                 $a[$vnull;
  132.                 continue;
  133.             }
  134.  
  135.             switch ($type{
  136.                 case 'array':
  137.                 case 'object':
  138.                     $a[$vserialize($this->_data[$v]);
  139.                     break;
  140.                 case 'gzip':
  141.                     $a[$vgzcompress($this->_data[$v],5);
  142.                     break;
  143.                 case 'boolean':
  144.                     $a[$v$this->getTable()->getConnection()->convertBooleans($this->_data[$v]);
  145.                 break;
  146.                 case 'enum':
  147.                     $a[$v$this->_table->enumIndex($v,$this->_data[$v]);
  148.                     break;
  149.                 default:
  150.                     if ($this->_data[$vinstanceof Doctrine_Record{
  151.                         $this->_data[$v$this->_data[$v]->getIncremented();
  152.                     }
  153.  
  154.                     $a[$v$this->_data[$v];
  155.             }
  156.         }
  157.         $map $this->_table->inheritanceMap;
  158.         foreach ($map as $k => $v{
  159.             $old $this->get($kfalse);
  160.  
  161.             if ((string) $old !== (string) $v || $old === null{
  162.                 $a[$k$v;
  163.                 $this->_data[$k$v;
  164.             }
  165.         }
  166.  
  167.         return $a;
  168.     }
  169. }