bkdotcom/PHPDebugConsole

View on GitHub
src/Teams/Actions/OpenUrl.php

Summary

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

namespace bdk\Teams\Actions;

use Psr\Http\Message\UriInterface;
use RuntimeException;

/**
 * OpenUrl action
 *
 * @see https://adaptivecards.io/explorer/Action.OpenUrl.html
 */
class OpenUrl extends AbstractAction
{
    /**
     * Constructor
     *
     * @param string|UriInterface $url The url to open
     */
    public function __construct($url = null)
    {
        if ($url !== null) {
            self::assertUrl($url);
        }
        parent::__construct(array(
            'url' => $url ? (string) $url : null,
        ), 'Action.OpenUrl');
    }

    /**
     * {@inheritDoc}
     */
    public function getContent($version)
    {
        if ($this->fields['url'] === null) {
            throw new RuntimeException('OpenUrl url is required');
        }
        $content = parent::getContent($version);
        /** @var string */
        $content['url'] = $this->fields['url'];
        return $content;
    }

    /**
     * Sets url
     *
     * @param string|UriInterface $url The url to open
     *
     * @return OpenUrl
     */
    public function withUrl($url)
    {
        self::assertUrl($url);
        return $this->with('url', (string) $url);
    }
}