1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\HTTP\Headers;
use ICanBoogie\OffsetNotDefined;
use ICanBoogie\PropertyNotDefined;
/**
* Base class for header fields.
*
* Classes that extend the class and support attributes must defined them during construct:
*
* <pre>
* <?php
*
* namespace ICanBoogie\HTTP\Headers;
*
* class ContentDisposition extends Header
* {
* public function __construct($value=null, array $attributes=[])
* {
* $this->parameters['filename'] = new HeaderParameter('filename');
*
* parent::__construct($value, $attributes);
* }
* }
* </pre>
*
* Magic properties are automatically mapped to parameters. The value of a parameter is accessed
* through its corresponding property:
*
* <pre>
* <?php
*
* $cd = new ContentDisposition;
* $cd->filename = "Statistics.csv";
* echo $cd->filename;
* // "Statistics.csv"
* </pre>
*
* The instance of the parameter itself is accessed using the header as an array:
*
* <pre>
* <?php
*
* $cd = new ContentDisposition;
* $cd['filename']->value = "Statistics.csv";
* $cd['filename']->language = "en";
* </pre>
*
* An alias to the {@link $value} property can be defined by using the `VALUE_ALIAS` constant. The
* following code defines `type` as an alias:
*
* <pre>
* <?php
*
* class ContentDisposition extends Header
* {
* const VALUE_ALIAS = 'type';
* }
* </pre>
*/
abstract class Header implements \ArrayAccess
{
const VALUE_ALIAS = null;
/**
* The value of the header.
*
* @var string
*/
public $value;
/**
* The parameters supported by the header.
*
* @var HeaderParameter[]
*/
protected $parameters = [];
/**
* Creates a {@link Header} instance from the provided source.
*
* @param string|Header $source The source to create the instance from. If the source is
* an instance of {@link Header} it is returned as is.
*
* @return Header
*/
static public function from($source)
{
if ($source instanceof self)
{
return $source;
}
if ($source === null)
{
return new static;
}
list($value, $parameters) = static::parse($source);
return new static($value, $parameters);
}
/**
* Parse the provided source and extract its value and parameters.
*
* @param string $source The source to create the instance from.
*
* @throws \InvalidArgumentException if `$source` is not a string nor an object implementing
* `__toString()`.
*
* @return array
*/
static protected function parse($source)
{
if (is_object($source) && method_exists($source, '__toString'))
{
$source = (string) $source;
}
if (!is_string($source))
{
throw new \InvalidArgumentException(\ICanBoogie\format
(
"%var must be a string or an object implementing __toString(). Given: !data", [
'var' => 'source',
'data' => $source
]
));
}
$value_end = strpos($source, ';');
$parameters = [];
if ($value_end !== false)
{
$value = substr($source, 0, $value_end);
$attributes = trim(substr($source, $value_end + 1));
if ($attributes)
{
$a = explode(';', $attributes);
$a = array_map('trim', $a);
foreach ($a as $attribute)
{
$parameter = HeaderParameter::from($attribute);
$parameters[$parameter->attribute] = $parameter;
}
}
}
else
{
$value = $source;
}
return [ $value, $parameters ];
}
/**
* Checks if a parameter exists.
*
* @param string $attribute
*
* @return bool
*/
public function offsetExists($attribute)
{
return isset($this->parameters[$attribute]);
}
/**
* Sets the value of a parameter to `null`.
*
* @param string $attribute
*/
public function offsetUnset($attribute)
{
$this->parameters[$attribute]->value = null;
}
/**
* Sets the value of a parameter.
*
* If the value is an instance of {@link HeaderParameter} then the parameter is replaced,
* otherwise the value of the current parameter is updated and its language is set to `null`.
*
* @param string $attribute
* @param mixed $value
*
* @throws OffsetNotDefined in attempt to access a parameter that is not defined.
*/
public function offsetSet($attribute, $value)
{
if (!$this->offsetExists($attribute))
{
throw new OffsetNotDefined([ $attribute, $this ]);
}
if ($value instanceof HeaderParameter)
{
$this->parameters[$attribute] = $value;
}
else
{
$this->parameters[$attribute]->value = $value;
$this->parameters[$attribute]->language = null;
}
}
/**
* Returns a {@link HeaderParameter} instance.
*
* @param string $attribute
*
* @return HeaderParameter
*
* @throws OffsetNotDefined in attempt to access a parameter that is not defined.
*/
public function offsetGet($attribute)
{
if (!$this->offsetExists($attribute))
{
throw new OffsetNotDefined([ $attribute, $this ]);
}
return $this->parameters[$attribute];
}
/**
* Initializes the {@link $name}, {@link $value} and {@link $parameters} properties.
*
* To enable future extensions, unrecognized parameters are ignored. Supported parameters must
* be defined by a child class before it calls its parent.
*
* @param string $value
* @param array $parameters
*/
public function __construct($value=null, array $parameters=[])
{
$this->value = $value;
$parameters = array_intersect_key($parameters, $this->parameters);
foreach ($parameters as $attribute => $value)
{
$this[$attribute] = $value;
}
}
/**
* Returns the value of a defined parameter.
*
* The method also handles the alias of the {@link $value} property.
*
* @param string $property
*
* @throws PropertyNotDefined in attempt to access a parameter that is not defined.
*
* @return mixed
*/
public function __get($property)
{
if ($property === static::VALUE_ALIAS)
{
return $this->value;
}
if ($this->offsetExists($property))
{
return $this[$property]->value;
}
throw new PropertyNotDefined([ $property, $this ]);
}
/**
* Sets the value of a supported parameter.
*
* The method also handles the alias of the {@link $value} property.
*
* @param string $property
* @param mixed $value
*
* @throws PropertyNotDefined in attempt to access a parameter that is not defined.
*/
public function __set($property, $value)
{
if ($property === static::VALUE_ALIAS)
{
$this->value = $value;
return;
}
if ($this->offsetExists($property))
{
$this[$property]->value = $value;
return;
}
throw new PropertyNotDefined([ $property, $this ]);
}
/**
* Unsets the matching parameter.
*
* @param string $property
*
* @throws PropertyNotDefined in attempt to access a parameter that is not defined.
*/
public function __unset($property)
{
if (!isset($this->parameters[$property]))
{
return;
}
unset($this[$property]);
}
/**
* Renders the instance's value and parameters into a string.
*
* @return string
*/
public function __toString()
{
$value = $this->value;
if (!$value && $value !== 0)
{
return '';
}
foreach ($this->parameters as $attribute)
{
$rendered_attribute = $attribute->render();
if (!$rendered_attribute)
{
continue;
}
$value .= '; ' . $rendered_attribute;
}
return $value;
}
}