Home > database >  TYPO3 - Doctrine / DBAL data field type for BINARY(16) encoded IPv6 addresses
TYPO3 - Doctrine / DBAL data field type for BINARY(16) encoded IPv6 addresses

Time:01-26

I am implementing an ORM model for a TYPO3 plugin that is a connector to an existing external database. I got most things done by myself but I don't know how to parse an IPv6 address from the given bytes:

namespace Homeinfo\hwdb\Domain\Model;

use DateTime;

final class System
{
    function __construct(
        public readonly int $id,
        public readonly ?int $group,
        public readonly ?int $deployment,
        public readonly ?int $dataset,
        public readonly ?int $openvpn,
        public readonly WHAT_TYPE_HERE $ipv6address,
        public readonly ?string $pubkey,
        public readonly DateTime $created,
        public readonly ?DateTime $configured,
        public readonly bool $fitted,
        public readonly string $operating_system,
        public readonly ?bool $monitor,
        public readonly ?string $serial_number,
        public readonly ?string $model,
        public readonly ?DateTime $last_sync,
        public readonly bool $updating,
    )
    {
    }

    public static function fromArray(array $array): Self
    {
        return new self(
            $array['id'],
            $array['group'],
            $array['deployment'],
            $array['dataset'],
            $array['openvpn'],
            HOW_TO_PARSE_THIS($array['ipv6address']),
            $array['pubkey'],
            new DateTime($array['created']),
            (($configured = $array['configured']) === null) ? null : new DateTime($configured),
            $array['fitted'],
            $array['operating_system'],
            $array['monitor'],
            $array['serial_number'],
            $array['model'],
            (($last_sync = $array['last_sync']) === null) ? null : new DateTime($last_sync),
            $array['updating'],
        );
    }
}

I need to know what type can be used in TYPO3 to represent IPv6 addresses (I know that I do it with ipaddress.IPv6Address in Python 3) and I don't know either how to properly parse it from the binary data retrieved from the MySQL database where the IPv6 addresses are stored as BINARY(16).

CodePudding user response:

As PHP does not have an "Binary" data type you need to convert the data in some kind of string representation. easies it by casting the columne in the select using hex(clumn) in mysql.

this will give you a hexstring. (maybe doctrine does that already automatically) if you want to display the adress to humans. you can directly convert the hexstring using inet_ntop()

if you need both representations you could store the hex in the model and just do the conversion in the appropriate getters / setters like getHumanReadableIpAdress()

https://www.php.net/manual/en/function.inet-ntop.php

  • Related