Syndesi/neo4j-sync-bundle

View on GitHub
src/ValueObject/Property.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace Syndesi\Neo4jSyncBundle\ValueObject;

use Stringable;
use Syndesi\Neo4jSyncBundle\Contract\IsEqualToInterface;
use Syndesi\Neo4jSyncBundle\Exception\UnsupportedPropertyNameException;

class Property implements Stringable, IsEqualToInterface
{
    public const PROPERTY_NAME_REGEX = '/^[a-z_][a-zA-Z0-9_]+$/';

    /**
     * Generic type for properties.
     *
     * @param string     $name  name of the property, always required
     * @param mixed|null $value value of the property, in some contexts optional
     *
     * @throws UnsupportedPropertyNameException
     */
    public function __construct(
        private readonly string $name,
        private readonly mixed $value = null
    ) {
        if (!preg_match(self::PROPERTY_NAME_REGEX, $name)) {
            throw new UnsupportedPropertyNameException(sprintf("Property name '%s' does not match Regex '%s'.", $name, self::PROPERTY_NAME_REGEX));
        }
    }

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

    public function getValue(): mixed
    {
        return $this->value;
    }

    public function __toString()
    {
        return sprintf("%s: %s", $this->getName(), $this->getValue());
    }

    public function isEqualTo(object $element): bool
    {
        if (!($element instanceof Property)) {
            return false;
        }

        return
            ($this->name === $element->name) &&
            ($this->value === $element->value);
    }
}