<?php namespace CodeIgniter\Test; /** * CodeIgniter * * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * * Copyright (c) 2014-2019 British Columbia Institute of Technology * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @package CodeIgniter * @author CodeIgniter Dev Team * @copyright 2014-2019 British Columbia Institute of Technology (https://bcit.ca/) * @license https://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 3.0.0 * @filesource */ use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; class ControllerResponse { /** * @var \CodeIgniter\HTTP\IncomingRequest */ protected $request; /** * @var \CodeIgniter\HTTP\Response */ protected $response; /** * @var string */ protected $body; /** * @var DOMParser */ protected $dom; public function __construct() { $this->dom = new DOMParser(); } //-------------------------------------------------------------------- // Getters / Setters //-------------------------------------------------------------------- /** * @param string $body * * @return $this */ public function setBody(string $body) { $this->body = $body; if (! empty($body)) { $this->dom = $this->dom->withString($body); } return $this; } /** * @return string */ public function getBody() { return $this->body; } /** * @param \CodeIgniter\HTTP\RequestInterface $request * * @return $this */ public function setRequest(RequestInterface $request) { $this->request = $request; return $this; } /** * @param \CodeIgniter\HTTP\ResponseInterface $response * * @return $this */ public function setResponse(ResponseInterface $response) { $this->response = $response; $this->setBody($response->getBody() ?? ''); return $this; } /** * @return \CodeIgniter\HTTP\IncomingRequest */ public function request() { return $this->request; } /** * @return \CodeIgniter\HTTP\Response */ public function response() { return $this->response; } //-------------------------------------------------------------------- // Simple Response Checks //-------------------------------------------------------------------- /** * Boils down the possible responses into a bolean valid/not-valid * response type. * * @return boolean */ public function isOK(): bool { // Only 200 and 300 range status codes // are considered valid. if ($this->response->getStatusCode() >= 400 || $this->response->getStatusCode() < 200) { return false; } // Empty bodies are not considered valid. if (empty($this->response->getBody())) { return false; } return true; } /** * Returns whether or not the Response was a redirect response * * @return boolean */ public function isRedirect(): bool { return $this->response instanceof RedirectResponse; } //-------------------------------------------------------------------- // Utility //-------------------------------------------------------------------- public function __call($function, $params) { if (method_exists($this->dom, $function)) { return $this->dom->{$function}(...$params); } } }