Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\VarDumper\Caster; Chris@0: Chris@0: use Symfony\Component\VarDumper\Cloner\Stub; Chris@0: Chris@0: /** Chris@0: * Casts PDO related classes to array representation. Chris@0: * Chris@0: * @author Nicolas Grekas
Chris@0: */ Chris@0: class PdoCaster Chris@0: { Chris@17: private static $pdoAttributes = [ Chris@17: 'CASE' => [ Chris@0: \PDO::CASE_LOWER => 'LOWER', Chris@0: \PDO::CASE_NATURAL => 'NATURAL', Chris@0: \PDO::CASE_UPPER => 'UPPER', Chris@17: ], Chris@17: 'ERRMODE' => [ Chris@0: \PDO::ERRMODE_SILENT => 'SILENT', Chris@0: \PDO::ERRMODE_WARNING => 'WARNING', Chris@0: \PDO::ERRMODE_EXCEPTION => 'EXCEPTION', Chris@17: ], Chris@0: 'TIMEOUT', Chris@0: 'PREFETCH', Chris@0: 'AUTOCOMMIT', Chris@0: 'PERSISTENT', Chris@0: 'DRIVER_NAME', Chris@0: 'SERVER_INFO', Chris@17: 'ORACLE_NULLS' => [ Chris@0: \PDO::NULL_NATURAL => 'NATURAL', Chris@0: \PDO::NULL_EMPTY_STRING => 'EMPTY_STRING', Chris@0: \PDO::NULL_TO_STRING => 'TO_STRING', Chris@17: ], Chris@0: 'CLIENT_VERSION', Chris@0: 'SERVER_VERSION', Chris@0: 'STATEMENT_CLASS', Chris@0: 'EMULATE_PREPARES', Chris@0: 'CONNECTION_STATUS', Chris@0: 'STRINGIFY_FETCHES', Chris@17: 'DEFAULT_FETCH_MODE' => [ Chris@0: \PDO::FETCH_ASSOC => 'ASSOC', Chris@0: \PDO::FETCH_BOTH => 'BOTH', Chris@0: \PDO::FETCH_LAZY => 'LAZY', Chris@0: \PDO::FETCH_NUM => 'NUM', Chris@0: \PDO::FETCH_OBJ => 'OBJ', Chris@17: ], Chris@17: ]; Chris@0: Chris@0: public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested) Chris@0: { Chris@17: $attr = []; Chris@0: $errmode = $c->getAttribute(\PDO::ATTR_ERRMODE); Chris@0: $c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); Chris@0: Chris@0: foreach (self::$pdoAttributes as $k => $v) { Chris@0: if (!isset($k[0])) { Chris@0: $k = $v; Chris@17: $v = []; Chris@0: } Chris@0: Chris@0: try { Chris@17: $attr[$k] = 'ERRMODE' === $k ? $errmode : $c->getAttribute(\constant('PDO::ATTR_'.$k)); Chris@0: if ($v && isset($v[$attr[$k]])) { Chris@0: $attr[$k] = new ConstStub($v[$attr[$k]], $attr[$k]); Chris@0: } Chris@0: } catch (\Exception $e) { Chris@0: } Chris@0: } Chris@0: if (isset($attr[$k = 'STATEMENT_CLASS'][1])) { Chris@0: if ($attr[$k][1]) { Chris@0: $attr[$k][1] = new ArgsStub($attr[$k][1], '__construct', $attr[$k][0]); Chris@0: } Chris@0: $attr[$k][0] = new ClassStub($attr[$k][0]); Chris@0: } Chris@0: Chris@0: $prefix = Caster::PREFIX_VIRTUAL; Chris@17: $a += [ Chris@0: $prefix.'inTransaction' => method_exists($c, 'inTransaction'), Chris@0: $prefix.'errorInfo' => $c->errorInfo(), Chris@0: $prefix.'attributes' => new EnumStub($attr), Chris@17: ]; Chris@0: Chris@0: if ($a[$prefix.'inTransaction']) { Chris@0: $a[$prefix.'inTransaction'] = $c->inTransaction(); Chris@0: } else { Chris@0: unset($a[$prefix.'inTransaction']); Chris@0: } Chris@0: Chris@0: if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) { Chris@0: unset($a[$prefix.'errorInfo']); Chris@0: } Chris@0: Chris@0: $c->setAttribute(\PDO::ATTR_ERRMODE, $errmode); Chris@0: Chris@0: return $a; Chris@0: } Chris@0: Chris@0: public static function castPdoStatement(\PDOStatement $c, array $a, Stub $stub, $isNested) Chris@0: { Chris@0: $prefix = Caster::PREFIX_VIRTUAL; Chris@0: $a[$prefix.'errorInfo'] = $c->errorInfo(); Chris@0: Chris@0: if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) { Chris@0: unset($a[$prefix.'errorInfo']); Chris@0: } Chris@0: Chris@0: return $a; Chris@0: } Chris@0: }