netglue/prismic-php-kit

View on GitHub
src/Prismic/Document/Fragment/Link/WebLink.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php
declare(strict_types=1);

namespace Prismic\Document\Fragment\Link;

use Prismic\Document\Fragment\LinkInterface;
use Prismic\Exception\InvalidArgumentException;
use Prismic\LinkResolver;

class WebLink extends AbstractLink
{

    /** @var string */
    protected $url;


    public function getUrl() : ?string
    {
        return $this->url;
    }

    public static function linkFactory($value, LinkResolver $linkResolver) : LinkInterface
    {
        /** @var WebLink $link */
        $link = new static();
        $value = isset($value->value) ? $value->value : $value;
        $value = isset($value->image) ? $value->image : $value;
        $value = isset($value->file) ? $value->file : $value;

        if (! isset($value->url)) {
            throw new InvalidArgumentException(\sprintf(
                'Expected value to contain a url property, received %s',
                \json_encode($value)
            ));
        }

        $link->url    = $value->url;
        $link->target = isset($value->target) ? $value->target : null;
        return $link;
    }
}