Source for file Formatter.php

Documentation is available at Formatter.php

  1. <?php
  2. /*
  3.  *  $Id$
  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. Doctrine::autoload('Doctrine_Connection_Module');
  22. /**
  23.  * Doctrine_Formatter
  24.  *
  25.  * @package     Doctrine
  26.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  27.  * @category    Object Relational Mapping
  28.  * @link        www.phpdoctrine.com
  29.  * @since       1.0
  30.  * @version     $Revision$
  31.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  32.  */
  33. {
  34.     /**
  35.      * Quotes pattern (% and _) characters in a string)
  36.      *
  37.      * EXPERIMENTAL
  38.      *
  39.      * WARNING: this function is experimental and may change signature at
  40.      * any time until labelled as non-experimental
  41.      *
  42.      * @param   string  the input string to quote
  43.      *
  44.      * @return  string  quoted string
  45.      */
  46.     public function escapePattern($text)
  47.     {
  48.         if ($this->string_quoting['escape_pattern']{
  49.             $tmp $this->conn->string_quoting;
  50.  
  51.             $text str_replace($tmp['escape_pattern']
  52.                                 $tmp['escape_pattern'.
  53.                                 $tmp['escape_pattern']$text);
  54.  
  55.             foreach ($this->wildcards as $wildcard{
  56.                 $text str_replace($wildcard$tmp['escape_pattern'$wildcard$text);
  57.             }
  58.         }
  59.         return $text;
  60.     }
  61.     /**
  62.      * convertBooleans
  63.      * some drivers need the boolean values to be converted into integers
  64.      * when using DQL API
  65.      *
  66.      * This method takes care of that conversion
  67.      *
  68.      * @param array $item 
  69.      * @return void 
  70.      */
  71.     public function convertBooleans($item)
  72.     {
  73.         if (is_array($item)) {
  74.             foreach ($item as $k => $value{
  75.                 if (is_bool($value)) {
  76.                     $item[$k= (int) $value;
  77.                 }
  78.             }
  79.         else {
  80.             if (is_bool($item)) {
  81.                 $item = (int) $item;
  82.             }
  83.         }
  84.         return $item;
  85.     }
  86.     /**
  87.      * Quote a string so it can be safely used as a table or column name
  88.      *
  89.      * Delimiting style depends on which database driver is being used.
  90.      *
  91.      * NOTE: just because you CAN use delimited identifiers doesn't mean
  92.      * you SHOULD use them.  In general, they end up causing way more
  93.      * problems than they solve.
  94.      *
  95.      * Portability is broken by using the following characters inside
  96.      * delimited identifiers:
  97.      *   + backtick (<kbd>`</kbd>) -- due to MySQL
  98.      *   + double quote (<kbd>"</kbd>) -- due to Oracle
  99.      *   + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
  100.      *
  101.      * Delimited identifiers are known to generally work correctly under
  102.      * the following drivers:
  103.      *   + mssql
  104.      *   + mysql
  105.      *   + mysqli
  106.      *   + oci8
  107.      *   + pgsql
  108.      *   + sqlite
  109.      *
  110.      * InterBase doesn't seem to be able to use delimited identifiers
  111.      * via PHP 4.  They work fine under PHP 5.
  112.      *
  113.      * @param string $str           identifier name to be quoted
  114.      * @param bool $checkOption     check the 'quote_identifier' option
  115.      *
  116.      * @return string               quoted identifier string
  117.      */
  118.     public function quoteIdentifier($str$checkOption true)
  119.     {
  120.         if ($checkOption && $this->conn->getAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER)) {
  121.             return $str;
  122.         }
  123.         $tmp $this->conn->identifier_quoting;
  124.         $str str_replace($tmp['end'],
  125.                            $tmp['escape'.
  126.                            $tmp['end']$str);
  127.  
  128.         return $tmp['start'$str $tmp['end'];
  129.     }
  130.     /**
  131.      * quote
  132.      * quotes given input parameter
  133.      *
  134.      * @param mixed $input      parameter to be quoted
  135.      * @param string $type 
  136.      * @return mixed 
  137.      */
  138.     public function quote($input$type null)
  139.     {
  140.         if ($type == null{
  141.             $type gettype($input);
  142.         }
  143.         switch ($type{
  144.             case 'integer':
  145.             case 'enum':
  146.             case 'boolean':
  147.             case 'double':
  148.             case 'float':
  149.             case 'bool':
  150.             case 'int':
  151.                 return $input;
  152.             case 'array':
  153.             case 'object':
  154.                 $input serialize($input);
  155.             case 'string':
  156.             case 'char':
  157.             case 'varchar':
  158.             case 'text':
  159.             case 'gzip':
  160.             case 'blob':
  161.             case 'clob':
  162.                 $this->conn->connect();
  163.  
  164.                 return $this->conn->getDbh()->quote($input);
  165.         }
  166.     }
  167.     /**
  168.      * Removes any formatting in an sequence name using the 'seqname_format' option
  169.      *
  170.      * @param string $sqn string that containts name of a potential sequence
  171.      * @return string name of the sequence with possible formatting removed
  172.      */
  173.     public function fixSequenceName($sqn)
  174.     {
  175.         $seqPattern '/^'.preg_replace('/%s/''([a-z0-9_]+)',  $this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT)).'$/i';
  176.         $seqName    preg_replace($seqPattern'\\1'$sqn);
  177.  
  178.         if ($seqName && strcasecmp($sqn$this->getSequenceName($seqName))) {
  179.             return $seqName;
  180.         }
  181.         return $sqn;
  182.     }
  183.     /**
  184.      * Removes any formatting in an index name using the 'idxname_format' option
  185.      *
  186.      * @param string $idx string that containts name of anl index
  187.      * @return string name of the index with possible formatting removed
  188.      */
  189.     public function fixIndexName($idx)
  190.     {
  191.         $indexPattern   '/^'.preg_replace('/%s/''([a-z0-9_]+)'$this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT)).'$/i';
  192.         $indexName      preg_replace($indexPattern'\\1'$idx);
  193.         if ($indexName && strcasecmp($idx$this->getIndexName($indexName))) {
  194.             return $indexName;
  195.         }
  196.         return $idx;
  197.     }
  198.     /**
  199.      * adds sequence name formatting to a sequence name
  200.      *
  201.      * @param string    name of the sequence
  202.      * @return string   formatted sequence name
  203.      */
  204.     public function getSequenceName($sqn)
  205.     {
  206.         return sprintf($this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT),
  207.             preg_replace('/[^a-z0-9_\$.]/i''_'$sqn));
  208.     }
  209.     /**
  210.      * adds index name formatting to a index name
  211.      *
  212.      * @param string    name of the index
  213.      * @return string   formatted index name
  214.      */
  215.     public function getIndexName($idx)
  216.     {
  217.         return sprintf($this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT),
  218.                 preg_replace('/[^a-z0-9_\$]/i''_'$idx));
  219.     }
  220. }