martin-helmich/phpunit-psr7-assert

View on GitHub
src/Constraint/IsAbsoluteUriConstraint.php

Summary

Maintainability
A
0 mins
Test Coverage
C
72%
<?php
declare(strict_types = 1);
namespace Helmich\Psr7Assert\Constraint;

use PHPUnit\Framework\Constraint\Constraint;

class IsAbsoluteUriConstraint extends Constraint
{
    public function toString(): string
    {
        return "is valid URI";
    }

    protected function matches($other): bool
    {
        $parts = parse_url($other);
        if ($parts === false) {
            return false;
        }

        if (!isset($parts["host"]) || !$parts["host"]) {
            return false;
        }

        if (!isset($parts["scheme"]) || !$parts["scheme"]) {
            return false;
        }

        return $parts !== false;
    }
}