smartemailing/types

View on GitHub
src/IntArray.php

Summary

Maintainability
C
1 day
Test Coverage
<?php

declare(strict_types = 1);

namespace SmartEmailing\Types;

abstract class IntArray implements ExtractableTypeInterface
{

    /**
     * @return array<int>
     */
    final public static function from(
        mixed $value
    ): array {
        $array = Arrays::from($value);

        $return = [];

        foreach ($array as $index => $item) {
            $return[$index] = IntType::from($item);
        }

        return $return;
    }

    /**
     * @return array<int>|null
     */
    final public static function fromOrNull(
        mixed $value,
        bool $nullIfInvalid = false
    ): ?array {
        $array = Arrays::fromOrNull($value, $nullIfInvalid);

        if ($array === null) {
            return null;
        }

        $return = [];

        try {
            foreach ($array as $index => $item) {
                $return[$index] = IntType::from($item);
            }
        } catch (InvalidTypeException $e) {
            if ($nullIfInvalid) {
                return null;
            }

            throw $e;
        }

        return $return;
    }

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

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

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

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

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

}