Lib.php 9.84 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
lsmith's avatar
lsmith committed
2
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  $Id$
 *
 * 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 LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
/**
 * Doctrine_Lib has not commonly used static functions, mostly for debugging purposes
zYne's avatar
zYne committed
23 24 25 26 27 28 29 30 31
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
32 33
class Doctrine_Lib
{
doctrine's avatar
doctrine committed
34 35 36 37 38
    /**
     * @param integer $state                the state of record
     * @see Doctrine_Record::STATE_* constants
     * @return string                       string representation of given state
     */
lsmith's avatar
lsmith committed
39 40
    public static function getRecordStateAsString($state)
    {
lsmith's avatar
lsmith committed
41
        switch ($state) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        case Doctrine_Record::STATE_PROXY:
            return "proxy";
            break;
        case Doctrine_Record::STATE_CLEAN:
            return "persistent clean";
            break;
        case Doctrine_Record::STATE_DIRTY:
            return "persistent dirty";
            break;
        case Doctrine_Record::STATE_TDIRTY:
            return "transient dirty";
            break;
        case Doctrine_Record::STATE_TCLEAN:
            return "transient clean";
            break;
57
        }
doctrine's avatar
doctrine committed
58 59 60 61 62 63
    }
    /**
     * returns a string representation of Doctrine_Record object
     * @param Doctrine_Record $record
     * @return string
     */
lsmith's avatar
lsmith committed
64 65
    public static function getRecordAsString(Doctrine_Record $record)
    {
zYne's avatar
zYne committed
66 67 68 69 70 71 72 73
        $r[] = '<pre>';
        $r[] = 'Component  : ' . $record->getTable()->getComponentName();
        $r[] = 'ID         : ' . $record->obtainIdentifier();
        $r[] = 'References : ' . count($record->getReferences());
        $r[] = 'State      : ' . Doctrine_Lib::getRecordStateAsString($record->getState());
        $r[] = 'OID        : ' . $record->getOID();
        $r[] = 'data       : ' . Doctrine::dump($record->getData(), false);
        $r[] = '</pre>';
doctrine's avatar
doctrine committed
74 75
        return implode("\n",$r)."<br />";
    }
76 77 78 79 80 81 82 83 84 85 86 87
    /**
     * Return an collection of records as XML. 
     * 
     * @see getRecordAsXml for options to set in the record class to control this.
     *
     * @param Doctrine_Collection $collection
     * @param SimpleXMLElement $xml
     * @return string Xml as string 
     */

    public static function getCollectionAsXml(Doctrine_Collection $collection, SimpleXMLElement $incomming_xml = null){

zYne's avatar
zYne committed
88
        $collectionName = Doctrine_Lib::plurelize($collection->getTable()->name);
89

zYne's avatar
zYne committed
90 91
        if ( ! isset($incomming_xml)) {
            $xml = new SimpleXMLElement("<" . $collectionName . "></" . $collectionName . ">");
92
        } else {
zYne's avatar
zYne committed
93
            $xml = $incomming_xml->addChild($collectionName);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        }
        foreach ($collection as $key => $record) {
            Doctrine_Lib::getRecordAsXml($record, $xml);
        }
        return $xml->asXML();
    }

    public static function plurelize($string){
        return $string . "s";
    }

    /**
     * Return a recrd as XML. 
     *
     * In order to control how this is done set the "xml" option in a record. 
     * This option is an array that has the keys "ignore_fields" and "include_relations". Both of these are arrays that list the name of fields/relations to include/process. 
     *
     * If you want to insert this xml as a part inside another xml send a 
     * SimpleXMLElement to the function. Because of the nature of SimpleXML the 
     * content you add to this element will be avilable after the function is 
     * complete.
     *
     * @param Doctrine_Record $record
     * @param SimpleXMLElement $xml
     * @return string Xml as string
     */
    public static function getRecordAsXml(Doctrine_Record $record, SimpleXMlElement $incomming_xml = NULL)
121 122
    {
        $recordname = $record->getTable()->name;
123
        if (!isset($incomming_xml)) {
124
            $xml = new SimpleXMLElement("<" . $recordname . "></" . $recordname . ">");
125 126
        }else{
            $xml = $incomming_xml->addChild($recordname);
127
        }
128 129 130
				foreach($record->obtainIdentifier() as $pk_field => $pk_value){
					$xml->addChild($pk_field,$pk_value); 
				}
131 132 133
        $xml_options = $record->option("xml");
        foreach ($record->getData() as $field => $value) {
            if (isset($xml_options["ignore_fields"]) && !in_array($field, $xml_options["ignore_fields"])) {
134 135 136 137 138
                if ($value instanceOf Doctrine_Null) {
                    $xml->addChild($field);
                } else {	
                    $xml->addChild($field, $value);
                }
139 140 141 142 143 144 145 146 147 148
            }
        }
        if (!isset($xml_options["include_relations"])) {
            return $xml->asXML();
        }
        $relations = $record->getTable()->getRelations();
        foreach ($relations as $name => $relation) {
            if (in_array($name, $xml_options["include_relations"])) {
                $relation_type = $relation->getType();
                $related_records = $record->get($name);
149 150
                if ($relation_type == Doctrine_Relation::ONE && $related_records instanceOf Doctrine_Record) {
                    Doctrine_Lib::getRecordAsXml($related_records, $xml);
151
                } else {
152
                    Doctrine_Lib::getCollectionAsXml($related_records, $xml);
153 154 155 156 157 158 159
                }
            }
        }
        return $xml->asXML();
    }


doctrine's avatar
doctrine committed
160 161
    /**
     * getStateAsString
zYne's avatar
zYne committed
162 163
     * returns a given connection state as string
     * @param integer $state        connection state
doctrine's avatar
doctrine committed
164
     */
lsmith's avatar
lsmith committed
165 166
    public static function getConnectionStateAsString($state)
    {
lsmith's avatar
lsmith committed
167
        switch ($state) {
168 169 170 171 172 173 174 175 176
        case Doctrine_Transaction::STATE_SLEEP:
            return "open";
            break;
        case Doctrine_Transaction::STATE_BUSY:
            return "busy";
            break;
        case Doctrine_Transaction::STATE_ACTIVE:
            return "active";
            break;
177
        }
doctrine's avatar
doctrine committed
178 179
    }
    /**
zYne's avatar
zYne committed
180 181
     * returns a string representation of Doctrine_Connection object
     * @param Doctrine_Connection $connection
doctrine's avatar
doctrine committed
182 183
     * @return string
     */
lsmith's avatar
lsmith committed
184 185
    public static function getConnectionAsString(Doctrine_Connection $connection)
    {
186 187 188 189 190 191
        $r[] = '<pre>';
        $r[] = 'Doctrine_Connection object';
        $r[] = 'State               : ' . Doctrine_Lib::getConnectionStateAsString($connection->transaction->getState());
        $r[] = 'Open Transactions   : ' . $connection->transaction->getTransactionLevel();
        $r[] = 'Table in memory     : ' . $connection->count();
        $r[] = 'Driver name         : ' . $connection->getDbh()->getAttribute(Doctrine::ATTR_DRIVER_NAME);
doctrine's avatar
doctrine committed
192 193 194 195 196 197 198 199 200

        $r[] = "</pre>";
        return implode("\n",$r)."<br>";
    }
    /**
     * returns a string representation of Doctrine_Table object
     * @param Doctrine_Table $table
     * @return string
     */
lsmith's avatar
lsmith committed
201 202
    public static function getTableAsString(Doctrine_Table $table)
    {
doctrine's avatar
doctrine committed
203
        $r[] = "<pre>";
204 205
        $r[] = "Component   : ".$table->getComponentName();
        $r[] = "Table       : ".$table->getTableName();
doctrine's avatar
doctrine committed
206 207 208
        $r[] = "</pre>";
        return implode("\n",$r)."<br>";
    }
doctrine's avatar
doctrine committed
209 210 211
    /**
     * @return string
     */
lsmith's avatar
lsmith committed
212 213
    public static function formatSql($sql)
    {
doctrine's avatar
doctrine committed
214 215 216
        $e = explode("\n",$sql);
        $color = "367FAC";
        $l = $sql;
zYne's avatar
zYne committed
217 218 219 220 221 222 223 224 225 226 227 228 229
        $l = str_replace("SELECT ", "<font color='$color'><b>SELECT </b></font><br \>  ",$l);
        $l = str_replace("FROM ", "<font color='$color'><b>FROM </b></font><br \>",$l);
        $l = str_replace(" LEFT JOIN ", "<br \><font color='$color'><b> LEFT JOIN </b></font>",$l);
        $l = str_replace(" INNER JOIN ", "<br \><font color='$color'><b> INNER JOIN </b></font>",$l);
        $l = str_replace(" WHERE ", "<br \><font color='$color'><b> WHERE </b></font>",$l);
        $l = str_replace(" GROUP BY ", "<br \><font color='$color'><b> GROUP BY </b></font>",$l);
        $l = str_replace(" HAVING ", "<br \><font color='$color'><b> HAVING </b></font>",$l);
        $l = str_replace(" AS ", "<font color='$color'><b> AS </b></font><br \>  ",$l);
        $l = str_replace(" ON ", "<font color='$color'><b> ON </b></font>",$l);
        $l = str_replace(" ORDER BY ", "<font color='$color'><b> ORDER BY </b></font><br \>",$l);
        $l = str_replace(" LIMIT ", "<font color='$color'><b> LIMIT </b></font><br \>",$l);
        $l = str_replace(" OFFSET ", "<font color='$color'><b> OFFSET </b></font><br \>",$l);
        $l = str_replace("  ", "<dd>",$l);
lsmith's avatar
lsmith committed
230

doctrine's avatar
doctrine committed
231 232
        return $l;
    }
doctrine's avatar
doctrine committed
233 234 235 236 237
    /**
     * returns a string representation of Doctrine_Collection object
     * @param Doctrine_Collection $collection
     * @return string
     */
lsmith's avatar
lsmith committed
238 239
    public static function getCollectionAsString(Doctrine_Collection $collection)
    {
doctrine's avatar
doctrine committed
240 241 242
        $r[] = "<pre>";
        $r[] = get_class($collection);

lsmith's avatar
lsmith committed
243
        foreach ($collection as $key => $record) {
244
            $r[] = "Key : ".$key." ID : ".$record->obtainIdentifier();
doctrine's avatar
doctrine committed
245 246 247 248 249
        }
        $r[] = "</pre>";
        return implode("\n",$r);
    }
}