diff --git a/Message/Http/Request.php b/Message/Http/Request.php index e343dff90..88c75e9e7 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -16,12 +16,10 @@ namespace phpOMS\Message\Http; use phpOMS\Localization\Localization; - - use phpOMS\Message\RequestAbstract; -use phpOMS\Message\RequestMethod; use phpOMS\Uri\Http; use phpOMS\Uri\UriFactory; +use phpOMS\Uri\UriInterface; /** * Request class. @@ -80,16 +78,15 @@ class Request extends RequestAbstract /** * Constructor. * - * @param string $rootPath relative installation path + * @param UriInterface $uri Uri * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(string $rootPath) + public function __construct(UriInterface $uri) { - $this->uri = new Http($rootPath); + $this->uri = $uri; $this->l11n = new Localization(); - UriFactory::setQuery('/root', $rootPath); } /** @@ -97,7 +94,7 @@ class Request extends RequestAbstract * * This is used in order to either initialize the current http request or a batch of GET requests * - * @param string $uri URL + * @param mixed $uri URL * * @return void * @@ -106,7 +103,7 @@ class Request extends RequestAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public function init(string $uri = null) + public function init($uri = null) { if ($uri === null) { $this->data = $_GET ?? []; @@ -128,7 +125,7 @@ class Request extends RequestAbstract $this->uri->set(Http::getCurrent()); } else { - $this->setMethod($uri['type']); // TODO: is this correct? + $this->setMethod($uri['type']); $this->uri->set($uri['uri']); } @@ -177,21 +174,6 @@ class Request extends RequestAbstract return false; } - /** - * Set request type. - * - * @param RequestMethod $type Request type - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function setMethod(RequestMethod $type) - { - $this->type = $type; - } - /** * Generate request hash. * @@ -338,11 +320,11 @@ class Request extends RequestAbstract */ public function getMethod() : string { - if (!isset($this->type)) { - $this->type = $_SERVER['REQUEST_METHOD']; + if (!isset($this->method)) { + $this->method = $_SERVER['REQUEST_METHOD']; } - return $this->type; + return $this->method; } /** diff --git a/Message/Http/Rest.php b/Message/Http/Rest.php index bf38f3a64..d99435014 100644 --- a/Message/Http/Rest.php +++ b/Message/Http/Rest.php @@ -15,9 +15,7 @@ */ namespace phpOMS\Message\Http; -use phpOMS\Datatypes\Exception\InvalidEnumValue; use phpOMS\Message\RequestMethod; -use phpOMS\Uri\InvalidUriException; /** * Rest request class. @@ -35,49 +33,21 @@ class Rest /** * Url. * - * @var string + * @var Request * @since 1.0.0 */ - private $url = ''; - - /** - * Method. - * - * @var string - * @since 1.0.0 - */ - private $method = RequestMethod::POST; + private $request = ''; /** * Set url. * - * @param string $url Url + * @param Request $request Request * * @since 1.0.0 * @author Dennis Eichhorn */ - public function setUrl(string $url) { - if (filter_var($url, FILTER_VALIDATE_URL) === false) { - throw new InvalidUriException('$url'); - } - - $this->url = $url; - } - - /** - * Set method. - * - * @param string $method Method - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function setMethod(string $method) { - if(!RequestMethod::isValidValue($method)) { - throw new InvalidEnumValue($method); - } - - $this->method = $method; + public function setRequest(Request $request) { + $this->request = $request; } /** @@ -94,7 +64,7 @@ class Rest { $curl = curl_init(); - switch ($this->method) { + switch ($this->request->getMethod()) { case RequestMethod::POST: curl_setopt($curl, CURLOPT_POST, 1); @@ -105,16 +75,12 @@ class Rest case RequestMethod::PUT: curl_setopt($curl, CURLOPT_PUT, 1); break; - default: - if ($data) { - $this->url = sprintf("%s?%s", $this->url, http_build_query($data)); - } } curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, "username:password"); - curl_setopt($curl, CURLOPT_URL, $this->url); + curl_setopt($curl, CURLOPT_URL, $this->request->getUri()->__toString()); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); diff --git a/Message/RequestAbstract.php b/Message/RequestAbstract.php index a52930c0f..0911edb7d 100644 --- a/Message/RequestAbstract.php +++ b/Message/RequestAbstract.php @@ -44,12 +44,20 @@ abstract class RequestAbstract implements RequestInterface protected $uri = null; /** - * Request type. + * Request method. * - * @var \phpOMS\Message\RequestMethod + * @var string * @since 1.0.0 */ - protected $type = null; + protected $method = null; + + /** + * Root. + * + * @var string + * @since 1.0.0 + */ + protected $rootPath = null; /** * Request data. @@ -125,6 +133,14 @@ abstract class RequestAbstract implements RequestInterface return $this->uri; } + /** + * {@inheritdoc} + */ + public function setUri(UriInterface $uri) + { + return $this->uri = $uri; + } + /** * {@inheritdoc} */ @@ -158,6 +174,13 @@ abstract class RequestAbstract implements RequestInterface */ abstract public function getMethod() : string; + /** + * {@inheritdoc} + */ + public function setMethod(string $method) { + $this->method = $method; + } + /** * {@inheritdoc} */ diff --git a/Message/RequestInterface.php b/Message/RequestInterface.php index 5d421b74c..ab9ceda65 100644 --- a/Message/RequestInterface.php +++ b/Message/RequestInterface.php @@ -51,6 +51,16 @@ interface RequestInterface extends MessageInterface */ public function getMethod() : string; + /** + * Set request method. + * + * @param string $method + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setMethod(string $method); + /** * Get request uri. * @@ -61,10 +71,20 @@ interface RequestInterface extends MessageInterface */ public function getUri() : UriInterface; + /** + * Set request uri. + * + * @param UriInterface $uri + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setUri(UriInterface $uri); + /** * Get request hash. * - * @return string + * @return array * * @since 1.0.0 * @author Dennis Eichhorn diff --git a/Uri/Http.php b/Uri/Http.php index d4b33fd56..b38d6c2e7 100644 --- a/Uri/Http.php +++ b/Uri/Http.php @@ -124,14 +124,22 @@ class Http implements UriInterface /** * Constructor. * - * @param string $rootPath Root path for subdirectory + * @param string $uri Root path for subdirectory * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(string $rootPath) + public function __construct(string $uri) { - $this->rootPath = $rootPath; + $this->set($uri); + } + + /** + * {@inheritdoc} + */ + public function setRootPath(string $root) + { + return $this->rootPath = $root; } /** @@ -310,13 +318,6 @@ class Http implements UriInterface $this->base = $this->scheme . '://' . $this->host . $this->rootPath; } - /** - * {@inheritdoc} - */ - public function parse($uri) - { - } - /** * {@inheritdoc} */ @@ -325,13 +326,6 @@ class Http implements UriInterface return $this->uri; } - /** - * {@inheritdoc} - */ - public function resolve($base) - { - } - /** * {@inheritdoc} */ diff --git a/Uri/UriInterface.php b/Uri/UriInterface.php index f14e944d5..8c21be327 100644 --- a/Uri/UriInterface.php +++ b/Uri/UriInterface.php @@ -41,6 +41,16 @@ interface UriInterface */ public static function isValid(string $uri) : bool; + /** + * Set uri + * + * @param string $uri Uri string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set(string $uri); + /** * Get scheme. *