|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * It's free open-source software released under the MIT License. |
| 5 | + * |
| 6 | + * @author Anatoly Fenric <anatoly@fenric.ru> |
| 7 | + * @copyright Copyright (c) 2018, Anatoly Fenric |
| 8 | + * @license https://github.com/sunrise-php/http-header-kit/blob/master/LICENSE |
| 9 | + * @link https://github.com/sunrise-php/http-header-kit |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Sunrise\Http\Header; |
| 13 | + |
| 14 | +/** |
| 15 | + * Import classes |
| 16 | + */ |
| 17 | +use InvalidArgumentException; |
| 18 | + |
| 19 | +/** |
| 20 | + * HeaderCustom |
| 21 | + */ |
| 22 | +class HeaderCustom extends AbstractHeader implements HeaderInterface |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * The header name |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $fieldName; |
| 31 | + |
| 32 | + /** |
| 33 | + * The header value |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + protected $fieldValue; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor of the class |
| 41 | + * |
| 42 | + * @param string $fieldName |
| 43 | + * @param string $fieldValue |
| 44 | + * |
| 45 | + * @throws InvalidArgumentException |
| 46 | + * If the name or value isn't valid. |
| 47 | + */ |
| 48 | + public function __construct(string $fieldName, string $fieldValue) |
| 49 | + { |
| 50 | + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $fieldName)) { |
| 51 | + throw new InvalidArgumentException(sprintf('Name of the header "%s" is not valid', $fieldName)); |
| 52 | + } |
| 53 | + |
| 54 | + if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $fieldValue)) { |
| 55 | + throw new InvalidArgumentException(sprintf('Value of the header "%s" is not valid', $fieldName)); |
| 56 | + } |
| 57 | + |
| 58 | + $this->fieldName = $fieldName; |
| 59 | + $this->fieldValue = $fieldValue; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritdoc} |
| 64 | + */ |
| 65 | + public function getFieldName() : string |
| 66 | + { |
| 67 | + return $this->fieldName; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + public function getFieldValue() : string |
| 74 | + { |
| 75 | + return $this->fieldValue; |
| 76 | + } |
| 77 | +} |
0 commit comments