# Data types

The SurrealDB SDK for PHP enables simple and advanced querying of a remote database.

The PHP SDK translates all SurrealQL datatypes into native PHP types, or a custom implementation. This document describes all datatypes, and links to their respective documentation.

## Data types overview

<table>
    <thead>
        <tr>
            <th colspan="1" scope="col">Datatype</th>
            <th colspan="1" scope="col">Kind</th>
            <th colspan="2" scope="col">Documentation</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="1" scope="row">String</td>
            <td colspan="1" scope="row">Native</td>
            <td colspan="2" scope="row">
                <a href="https://www.php.net/manual/en/language.types.string.php">
                    <code>string</code> on php.net
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Int</td>
            <td colspan="1" scope="row">Native</td>
            <td colspan="2" scope="row">
                <a href="https://www.php.net/manual/en/language.types.integer.php">
                    <code>integer</code> on php.net
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Float</td>
            <td colspan="1" scope="row">Native</td>
            <td colspan="2" scope="row">
                <a href="https://www.php.net/manual/en/language.types.float.php">
                    <code>float</code> on php.net
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Bool</td>
            <td colspan="1" scope="row">Native</td>
            <td colspan="2" scope="row">
                <a href="https://www.php.net/manual/en/language.types.boolean.php">
                    <code>bool</code> on php.net
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">null</td>
            <td colspan="1" scope="row">Native</td>
            <td colspan="2" scope="row">
                <a href="https://www.php.net/manual/en/language.types.null.php">
                    <code>null</code> on php.net
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Array / Associative</td>
            <td colspan="1" scope="row">Native</td>
            <td colspan="2" scope="row">
                <a href="https://www.php.net/manual/en/language.types.array.php">
                    <code>array</code> on php.net
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Datetime</td>
            <td colspan="1" scope="row">Native</td>
            <td colspan="2" scope="row">
                <a href="https://www.php.net/manual/en/class.datetime">
                    <code>DateTime</code> on php.net
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Binary</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <a href="https://github.com/welpie21/cbor.php/blob/main/src/utils/CborByteString.php">
                    <code>CborByteString</code>
                </a>
            </td>
        </tr>
		<tr>
            <td colspan="1" scope="row">None</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <a href="https://github.com/surrealdb/surrealdb.php/blob/main/src/Cbor/Types/None.php">
                    <code>[None](#none)</code>
                </a>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">RecordId</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <code>[RecordId](#recordid)</code>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Uuid</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <code>[Uuid](#uuid)</code>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Duration</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <code>[Duration](#duration)</code>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Geometry</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <code>[Geometry](#geometry)</code>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Decimal</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <code>[Decimal](#decimal)</code>
            </td>
        </tr>
        <tr>
            <td colspan="1" scope="row">Table</td>
            <td colspan="1" scope="row">Custom</td>
            <td colspan="2" scope="row">
                <code>[Table](#table)</code>
            </td>
        </tr>
    </tbody>
</table>

<br /><br />

##  `RecordId`

When you receive a RecordId from SurrealDB, it will always be represented as a `RecordId` class.
This class holds a `tb` and `id` field, representing the table name, and a unique identifier for the record on that table.
A `RecordId` can be converted into a string, and will be represented as such when it's converted to JSON.

```php title="Signature"
new RecordId(string $tb, string|int|array $id)
```

### Working with a `RecordId`

```php title="Constructing"
// table is "person"
// unique identifier on the table is "john"
$rid = new RecordId("person", "john");
```

```php title="Extracting data"
// Simple
$rid = new RecordId("person", "john");
$rid->tb // "person"
$rid->id // "john"

// Complex
$rid = new RecordId("recording", ["city" => "London", "data" => 123 ]);

$rid->id         // [ "city" => "London", "data" => 123 ]
$rid->id["city"] // "London"
$rid->id["data"] // 123
```

### Convert to String

The PHP SDK efficiently handles escaping the `tb` and `id` parts in Record Id's into their string counterparts.
Below are some examples

```php title="Simple"
$rid = (new RecordId("table", 123))->toString();
// 'table:123'
$rid = (new RecordId("table", "abc"))->toString();
// 'table:abc'
```

```php title="Complex characters"
$rid = (new RecordId("table", "123"))->toString();
// 'table:`123`'
$rid = (new RecordId("table", "123withletters"))->toString();
// 'table:123withletters'
$rid = (new RecordId("table", "complex-string"))->toString();
// 'table:`complex-string`'
$rid = (new RecordId("table-name", 123))->toString();
// '`table-name`:123'
```

```php title="Objects and Arrays"
$rid = (new RecordId("table", ["city" => "London"]))->toString();
// 'table:{ city: "London" }'
$rid = (new RecordId("table", ["London"]))->toString();
// 'table:["London"]'
```

### Send back string

If you need to send back a Record Id in string format, you can do so with the `StringRecordId` class.

We do not implement the parsing of Record Ids in the PHP SDK, as that would mean that we need to be able to parse any SurrealQL value,
which comes with a cost. Instead you can send it over as a string with `StringRecordId`, allowing the server to handle the parsing.

```php
new StringRecordId("person:john");
```

<br />

## `Geometry`

When a Geometry is sent back from SurrealDB, be it a `Point`, `Line`, `Polygon`, `MultiPoint`, `MultiLine`, `MultiPolygon` or `Collection`, it will be represented as a derivative of the `Geometry` class.

### Methods

Below, are all the methods implemented across all geometry derivatives.

#### `->toJSON()`

Used to convert a geometry to a GeoJSON representation

```php title="Signature"
Geometry->toJson();
```

```php title="Example"
$line = new GeometryLine([
    new GeometryPoint([1, 2]),
    new GeometryPoint([3, 4]),
]);

$line->toJson();                    // '{ type: "LineString", coordinates: [ [1, 2], [3, 4] ] }'
json_encode($line);                 // '{ type: "LineString", coordinates: [ [1, 2], [3, 4] ] }'
```

#### `->is()`

Used to convert a check if geometry X is exactly equal to geometry Y

```php title="Signature"
Geometry->is(Geometry $geometry)
```

```php title="Example"
$point1 = new GeometryPoint([1, 2]);
$point2 = new GeometryPoint([3, 4]);
$line = new GeometryLine([$point1, $point2]);

$point1->is($point1);      // true
$point1->is($point2);      // false
$point1->is($line);        // false

// Checks the inner values, does not need to be the same instance
$duplicate = new GeometryPoint([1, 2]);
$point1->is($duplicate);   // true
```

#### `->clone()`

Used to deeply clone a geometry. Creates a new replica of the original instance, but changing the new instance won't affect the other.

```php title="Signature"
Geometry->clone()
```

### Properties

#### `->coordinates`

A getter property, representing the coordinates as shown in GeoJSON format for X Geometry

```php title="Signature"
Geometry.coordinates
```

### Derivatives

#### `GeometryPoint`

A [point](/docs/reference/query-language/language-primitives/data-types/geometries.md#point) in space, made up of a long and lat coordinate, automatically converted to a float.

```php title="Signature"
new GeometryPoint([int|float|Decimal $long, int|float|Decimal $lat]);
```

#### `GeometryLine`

A line, made up of two or more points

```php title="Signature"
new GeometryLine([GeometryPoint, GeometryPoint, ...GeometryPoint[]]);
```

#### `GeometryPolygon`

A polygon, made up of self-closing lines

**Note**: The lines inside the polygon will automatically be closed if not already, meaning that the last point will be the same as the first.

```php title="Signature"
new GeometryPolygon([GeometryLine, ...GeometryLine[]]);
```

#### `GeometryMultiPoint`

A collection of one or more points

```php title="Signature"
new GeometryMultiPoint([GeometryPoint, ...GeometryPoint[]]);
```

#### `GeometryMultiLine`

A collection of one or more lines

```php title="Signature"
new GeometryMultiLine([GeometryLine, ...GeometryLine[]]);
```

#### `GeometryMultiPolygon`

A collection of one or more polygons

```php title="Signature"
new GeometryMultiPolygon([GeometryPolygon, ...GeometryPolygon[]]);
```

#### `GeometryCollection`

A collection of one or more `Geometry` derivatives

```php title="Signature"
new GeometryCollection([Geometry, ...Geometry[]]);
```

<br />

## `Decimal`

Because PHP does not support Decimals natively,
our SDK represents them in a `Decimal` class as a string.
This means if you want to work with Decimals, you will need to use an external library for this.

```php title="Signature"
new Decimal(string|float|Decimal $decimal);
```

### Converting to string

```php
$decimal = new Decimal("123.456");
decimal->toString(); // "123.456"
```

### Converting to JSON

A `Decimal` will be represented as a string in JSON to perserve accuracy

```php
$decimal = new Decimal("123.456");
$decimal->toJson();                  // "123.456"
json_encode(decimal);                // "123.456"
```

<br />

## `Table`

When you get a table name sent back from SurrealDB, it will be represented as a `Table` class.

```php title="Signature"
new Table(string $table);
```

### Converting to string

```php
$table = new Table("table");
$table->toString();              // "table"
```

### Converting to JSON

A `Table` will be represented as a string in JSON

```php
$table = new Table("table");
$table->jsonSerializable();      // "table"
json_encode($table);             // "table"
```
