Type converters
Type converters are the classes that
Parse string representations of database types returned by Postgres into native PHP types;
Generate string representations from native PHP types that can be used as query parameters.
These classes are pretty low-level and it is generally not needed to manually instantiate them: this is handled by
an implementation of TypeConverterFactory. In the “wrapper” part of the package, it is seldom needed to
use them directly at all: calling input() and output() is handled by Result and
Connection / PreparedStatement classes, respectively.
Type converter implementations
The below classes and interfaces, except builtin ones prefixed by a backslash,
belong to either \sad_spirit\pg_wrapper\converters or \sad_spirit\pg_wrapper\types
(classes representing complex types) namespace. Those are omitted for brevity.
StubConverterThis is the only implementation of
TypeConverterthat does not extendBaseConverter. As its name implies, it does not perform conversion and is returned bymethods of
StubTypeConverterFactory,DefaultTypeConverterFactory::getConverterForTypeOID()if a proper converter cannot be determined.
BaseConverterAbstract base class for converters, handles
nullvalues and provides several helper methods for parsing.BooleanConverterConverts Postgres
booleanvalues from / to PHP’sboolByteaConverterConverts binary data for
byteafields from / to string representation usinghexencoding.EnumConverterConverts values of Postgres
ENUMtype from / to cases of PHP’s string-backed enum. The converter instance is configured with name of enum:$converter = new EnumConverter(SomeEnum::class);
datetime\IntervalConverterPostgres
intervaltype <->\DateIntervalJSONConverterJSON string <-> Any PHP value that can be serialized.
StringConverterPostgres character types <-> PHP
string. This does not perform any conversion ininput(), casts PHP value to string inoutput().BaseNumericConverterAbstract base class for numeric converters, implements
ConnectionAwareto check server version and allow non-decimal literals and separators whenoutput()targets Postgres 16+NumericConverterAs
numerictype of Postgres can have almost unlimited precision,input()keeps the value as string, only returning special “float” values forInfinityandNaNFloatConverterPostgres float types <-> PHP’s
float
IntegerConverterPostgres integer types <-> PHP’s
int(the value may be left as string byinput()on 32bit builds of PHP, asint8values may overflow)
datetime\BaseDateTimeConverterAbstract base class for date and time converters, implements
ConnectionAwareto check server’sDateStylesetting and select proper format specification. Itsinput()method and those of its subclasses return instances of\DateTimeImmutable.datetime\DateConverter- convertsdatetypedatetime\TimeConverter- convertstimetypedatetime\TimeTzConverter- convertstimetz(time with time zone) typedatetime\TimeStampConverter- convertstimestamptypedatetime\TimeStampTzConverter- convertstimestamptz(timestamp with time zone) type
ContainerConverterAbstract base class for converters of “container” types: those are composed of multiple values of some “base” type. Usually the converter for “container” type uses the converter for “base” type to parse / generate parts of the value.
containers\ArrayConverterPostgres array <-> PHP array. The converter instance is configured by an instance of base type converter, e.g.
$dateArrayConverter = new ArrayConverter(new DateConverter()); // This will return an array of \DateTimeImmutable instances $dateArrayConverter->input($value);
containers\CompositeConverterPostgres composite (row) type <-> PHP array. The converter instance is configured by an array representing the composite type fields, thus for type defined like this
create type foo as (id integer, name text, added timestamp with time zone);
a converter may be created like this
$fooConverter = new CompositeConverter([ 'id' => new IntegerConverter(), 'name' => new StringConverter(), 'added' => new TimeStampTzConverter() ]);
containers\HstoreConverterhstoretype from contrib/hstore Postgres extension <->array<string, ?string>. This type for storing key => value pairs was quite useful before JSON support was added to Postgres.containers\IntegerVectorConverterint2vectororoidvector<->array<int|string>. These types are used only in system catalogs and are not documented.geometric\LineConverterline<-> instance ofLine. This converter does not extendgeometric\BaseGeometricConverteras thelinetype is represented by threefloatcoefficients of linear equation and does not depend onpointtype.containers\MultiRangeConverterMultirange type (Postgres 14+) <-> instance of
MultiRange. The converter instance is configured by an instance of range subtype converter and possibly a classname of customMultiRangesubclass:$intMultiRangeConverter = new MultiRangeConverter(new IntegerConverter()); $customMultiRangeConverter = new MultiRangeConverter(new CustomConverter(), CustomMultiRange::class);
geometric\PointConverterPostgres
pointtype <-> instance ofPoint.containers\RangeConverterRange type <-> instance of
Range. The converter instance is configured by an instance of range subtype converter and possibly a classname of customRangesubclass:$dateRangeConverter = new RangeConverter(new DateConverter()); $customRangeConverter = new RangeConverter(new CustomConverter(), CustomRange::class);
TidConverterPostgres
tidtype (represents physical location of a row within a table) <-> instance ofTid.geometric\BaseGeometricConverterAbstract base class for converters of geometric types. All the types converted by subclasses of this are based on the
pointtype and usePointConverterto process their own string representations.geometric\BoxConverterPostgres
boxtype <-> instance ofBoxgeometric\CircleConverterPostgres
circletype <-> instance ofCirclegeometric\LSegConverterPostgres
lsegtype <-> instance ofLineSegmentgeometric\PathConverterPostgres
pathtype <-> instance ofPathgeometric\PolygonConverterPostgres
polygontype <-> instance ofPolygon