. */ /** * Doctrine_Lib has not commonly used static functions, mostly for debugging purposes * * @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 */ class Doctrine_Lib { /** * @param integer $state the state of record * @see Doctrine_Record::STATE_* constants * @return string string representation of given state */ public static function getRecordStateAsString($state) { switch ($state) { 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; } } /** * returns a string representation of Doctrine_Record object * @param Doctrine_Record $record * @return string */ public static function getRecordAsString(Doctrine_Record $record) { $r[] = "
";
        $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[] = "
"; return implode("\n",$r)."
"; } public static function getRecordAsXml(Doctrine_Record $record, $xml = null) { $recordname = $record->getTable()->name; if ($xml == null) { $xml = new SimpleXMLElement("<" . $recordname . ">"); } $xml->addChild("id", $record->getOID()); $xml_options = $record->option("xml"); foreach ($record->getData() as $field => $value) { if (isset($xml_options["ignore_fields"]) && !in_array($field, $xml_options["ignore_fields"])) { $xml->addChild($field, $value); } } 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); if ($relation_type == Doctrine_Relation::ONE) { $related_xml = $xml->addChild($name); Doctrine_Lib::getRecordAsXml($related_records, $related_xml); } else { $xml_collection = $xml->addChild($name . "s"); //this could be fixed to plurelize in some better way i guess foreach ($related_records as $related_name => $related_record) { $related_xml = $xml_collection->addChild($name); Doctrine_Lib::getRecordAsXml($related_record, $related_xml); } } } } return $xml->asXML(); } /** * getStateAsString * returns a given connection state as string * @param integer $state connection state */ public static function getConnectionStateAsString($state) { switch ($state) { case Doctrine_Transaction::STATE_SLEEP: return "open"; break; case Doctrine_Transaction::STATE_BUSY: return "busy"; break; case Doctrine_Transaction::STATE_ACTIVE: return "active"; break; } } /** * returns a string representation of Doctrine_Connection object * @param Doctrine_Connection $connection * @return string */ public static function getConnectionAsString(Doctrine_Connection $connection) { $r[] = '
';
        $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);

        $r[] = "
"; return implode("\n",$r)."
"; } /** * returns a string representation of Doctrine_Table object * @param Doctrine_Table $table * @return string */ public static function getTableAsString(Doctrine_Table $table) { $r[] = "
";
        $r[] = "Component   : ".$table->getComponentName();
        $r[] = "Table       : ".$table->getTableName();
        $r[] = "
"; return implode("\n",$r)."
"; } /** * @return string */ public static function formatSql($sql) { $e = explode("\n",$sql); $color = "367FAC"; $l = $sql; $l = str_replace("SELECT ", "SELECT
",$l); $l = str_replace("FROM ", "FROM
",$l); $l = str_replace(" LEFT JOIN ", "
LEFT JOIN ",$l); $l = str_replace(" INNER JOIN ", "
INNER JOIN ",$l); $l = str_replace(" WHERE ", "
WHERE ",$l); $l = str_replace(" GROUP BY ", "
GROUP BY ",$l); $l = str_replace(" HAVING ", "
HAVING ",$l); $l = str_replace(" AS ", " AS
",$l); $l = str_replace(" ON ", " ON ",$l); $l = str_replace(" ORDER BY ", " ORDER BY
",$l); $l = str_replace(" LIMIT ", " LIMIT
",$l); $l = str_replace(" OFFSET ", " OFFSET
",$l); $l = str_replace(" ", "
",$l); return $l; } /** * returns a string representation of Doctrine_Collection object * @param Doctrine_Collection $collection * @return string */ public static function getCollectionAsString(Doctrine_Collection $collection) { $r[] = "
";
        $r[] = get_class($collection);

        foreach ($collection as $key => $record) {
            $r[] = "Key : ".$key." ID : ".$record->obtainIdentifier();
        }
        $r[] = "
"; return implode("\n",$r); } }