smartemailing/types

View on GitHub
src/StringType.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types = 1);

namespace SmartEmailing\Types;

use SmartEmailing\Types\Helpers\ExtractableHelpers;

abstract class StringType implements ExtractableTypeInterface
{

    final public static function from(
        mixed $value
    ): string {
        if (\is_scalar($value)) {
            return (string) $value;
        }

        throw InvalidTypeException::typeError('string', $value);
    }

    final public static function fromOrNull(
        mixed $value,
        bool $nullIfInvalid = false
    ): ?string {
        if ($value === null) {
            return null;
        }

        try {
            return self::from($value);
        } catch (InvalidTypeException $e) {
            if ($nullIfInvalid) {
                return null;
            }

            throw $e;
        }
    }

    /**
     * @param array<mixed>|\ArrayAccess<mixed, mixed> $data
     * @throws \SmartEmailing\Types\InvalidTypeException
     */
    final public static function extract(
        $data,
        string $key
    ): string {
        $value = ExtractableHelpers::extractValue($data, $key);

        try {
            return self::from($value);
        } catch (InvalidTypeException $e) {
            throw $e->wrap($key);
        }
    }

    /**
     * @param \ArrayAccess<mixed,mixed>|array<mixed> $data
     * @throws \SmartEmailing\Types\InvalidTypeException
     */
    final public static function extractOrNull(
        $data,
        string $key,
        bool $nullIfInvalid = false
    ): ?string {
        $value = ExtractableHelpers::extractValueOrNull($data, $key);

        if ($value === null || $value === '') {
            return null;
        }

        try {
            return self::fromOrNull($value, $nullIfInvalid);
        } catch (InvalidTypeException $exception) {
            throw $exception->wrap($key);
        }
    }

}