<?php
namespace Misuzu\Profile;

use Index\Data\IDbResult;

class ProfileFieldInfo {
    public function __construct(
        private string $id,
        private int $order,
        private string $name,
        private string $title,
        private string $regex,
    ) {}

    public static function fromResult(IDbResult $result): ProfileFieldInfo {
        return new ProfileFieldInfo(
            id: $result->getString(0),
            order: $result->getInteger(1),
            name: $result->getString(2),
            title: $result->getString(3),
            regex: $result->getString(4),
        );
    }

    public function getId(): string {
        return $this->id;
    }

    public function getOrder(): int {
        return $this->order;
    }

    public function getName(): string {
        return $this->name;
    }

    public function getTitle(): string {
        return $this->title;
    }

    public function getRegEx(): string {
        return $this->regex;
    }

    public function checkValue(string $value): bool {
        return preg_match($this->regex, $value) === 1;
    }

    public function matchValue(string $value): string|false {
        return preg_match($this->regex, $value, $matches) === 1 ? $matches[1] : false;
    }
}
