You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.6 KiB
PHP

<?php
namespace Artmark\Forms\Attributes;
/**
* Description of MethodAttribute
*
* @author Andrey Pokidov <pokidov@e-traffic.ru>
*/
trait MethodAttribute
{
/**
*
* @var string
*/
private $method = 'POST';
/**
* HTML method attribute of the tag
* @return string
*/
public function method()
{
return $this->method;
}
/**
*
* @return boolean
*/
public function isPost()
{
return $this->method === 'POST';
}
/**
*
* @return boolean
*/
public function isGet()
{
return $this->method === 'GET';
}
/**
*
* @param string $newMethod
* @return $this
*/
public function setMethod($newMethod)
{
self::checkMethod($newMethod);
$this->method = self::normalizeMethod($newMethod);
return $this;
}
protected function appendMethodAttribute(array & $attributes)
{
$attributes['method'] = $this->method;
}
public static function checkMethod($method)
{
if (!self::isCorrectMethod($method)) {
throw new AttributeException('Incorrect method value: `' . strval($method) . '`');
}
}
public static function isCorrectMethod($method)
{
if (!is_string($method)) {
return false;
}
$corrected = self::normalizeMethod($method);
return $corrected === 'POST' || $corrected === 'GET';
}
private static function normalizeMethod($method)
{
return strtoupper(trim($method));
}
}