Files
shlink/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php

33 lines
1005 B
PHP
Raw Normal View History

<?php
2019-10-05 17:26:10 +02:00
2017-10-12 10:13:20 +02:00
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
class CreateShortUrlAction extends AbstractCreateShortUrlAction
{
protected const ROUTE_PATH = '/short-urls';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_POST];
/**
* @throws ValidationException
*/
protected function buildShortUrlData(Request $request): CreateShortUrlData
{
2017-12-27 16:23:54 +01:00
$postData = (array) $request->getParsedBody();
if (! isset($postData['longUrl'])) {
throw ValidationException::fromArray([
'longUrl' => 'A URL was not provided',
]);
}
$meta = ShortUrlMeta::fromRawData($postData);
return new CreateShortUrlData($postData['longUrl'], (array) ($postData['tags'] ?? []), $meta);
}
}