<?php
namespace texdc\cicerone;
/**
* Provides encapsulation and validation of HTTP method values
*
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
*/
final class HttpMethod
{
/**#@+
* method constants
* @var string
*/
const ANY = '*';
const GET = 'GET';
const HEAD = 'HEAD';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
const OPTIONS = 'OPTIONS';
const TRACE = 'TRACE';
/**#@- */
/** @var string[] */
private static $allValues = [
self::ANY,
self::GET,
self::HEAD,
self::POST,
self::PUT,
self::DELETE,
self::OPTIONS,
self::TRACE,
];
/** @var self[] */
private static $instances = [];
/** @var string */
private $value;
/**
* @param string $aValue
* @throws InvalidArgumentException
*/
private function HttpMethod($aValue)
{
if (!self::validate($aValue)) {
throw new InvalidArgumentException("Invalid method [$aValue]");
}
$this->value = self::sanitize($aValue);
}
/**
* @param string $aMethod
* @param array $arguments
* @return self
*/
public static function __callStatic($aMethod, $arguments = [])
{
if (!isset(self::$instances[$aMethod]) && $instance = new static($aMethod)) {
self::$instances[$aMethod] = $instance;
} var_dump($aMethod, $instance);
return self::$instances[$aMethod];
}
/**
* @param string $aValue
* @return string
*/
private static function sanitize($aValue)
{
return strtoupper(trim($aValue));
}
/**
* @param string $aValue
* @return bool
*/
public static function validate($aValue)
{
return in_array(self::sanitize($aValue), self::$allValues);
}
/**
* @param string $aValue
* @return bool
*/
public function hasValue($aValue)
{
return $this->value === (string) $aValue;
}
/** @return bool */
public function isAny()
{
return $this->hasValue(self::ANY);
}
/** @return bool */
public function isGet()
{
return $this->hasValue(self::GET) || $this->isAny();
}
/** @return bool */
public function isHead()
{
return $this->hasValue(self::HEAD) || $this->isAny();
}
/** @return bool */
public function isPost()
{
return $this->hasValue(self::POST) || $this->isAny();
}
/** @return bool */
public function isPut()
{
return $this->hasValue(self::PUT) || $this->isAny();
}
/** @return bool */
public function isDelete()
{
return $this->hasValue(self::DELETE) || $this->isAny();
}
/** @return bool */
public function isOptions()
{
return $this->hasValue(self::OPTIONS) || $this->isAny();
}
/** @return bool */
public function isTrace()
{
return $this->hasValue(self::TRACE) || $this->isAny();
}
/** @return string */
public function __toString()
{
return $this->value;
}
}
var_dump(HttpMethod::PUT());
- Output for git.master, git.master_jit, rfc.property-hooks
- string(3) "PUT"
object(texdc\cicerone\HttpMethod)#1 (1) {
["value":"texdc\cicerone\HttpMethod":private]=>
NULL
}
object(texdc\cicerone\HttpMethod)#1 (1) {
["value":"texdc\cicerone\HttpMethod":private]=>
NULL
}
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
83.15 ms | 402 KiB | 8 Q