| @@ 19-81 (lines=63) @@ | ||
| 16 | /** |
|
| 17 | * "Allow" directive element. |
|
| 18 | */ |
|
| 19 | class Allow implements GroupMemberInterface, PathDirectiveInterface |
|
| 20 | { |
|
| 21 | /** |
|
| 22 | * Directive value. |
|
| 23 | * |
|
| 24 | * @var string |
|
| 25 | */ |
|
| 26 | private $value = ''; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * {@inheritdoc} |
|
| 30 | */ |
|
| 31 | public function __construct(string $raw) |
|
| 32 | { |
|
| 33 | if (!preg_match('/^allow:\s*([^# ]+).*/i', $raw, $matches)) { |
|
| 34 | throw InvalidDirectiveException::create($raw); |
|
| 35 | } |
|
| 36 | ||
| 37 | $this->value = trim($matches[1]); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * {@inheritdoc} |
|
| 42 | */ |
|
| 43 | public static function getField(): string |
|
| 44 | { |
|
| 45 | return 'allow'; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * {@inheritdoc} |
|
| 50 | */ |
|
| 51 | public function getValue(): string |
|
| 52 | { |
|
| 53 | return $this->value; |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * {@inheritdoc} |
|
| 58 | */ |
|
| 59 | public function __toString(): string |
|
| 60 | { |
|
| 61 | return sprintf('Allow: %s', $this->value); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * {@inheritdoc} |
|
| 66 | */ |
|
| 67 | public function getRegex(): string |
|
| 68 | { |
|
| 69 | // "*" matches any sequence of characters (zero or more) |
|
| 70 | $value = str_replace('*', '.*', $this->value); |
|
| 71 | ||
| 72 | // "?" matches any character |
|
| 73 | $value = str_replace('?', '*', $value); |
|
| 74 | ||
| 75 | // "\" supresses syntactic significance of a special character |
|
| 76 | $value = str_replace('\*', '\?', $value); |
|
| 77 | $value = str_replace('\.*', '\*', $value); |
|
| 78 | ||
| 79 | return $value; |
|
| 80 | } |
|
| 81 | } |
|
| 82 | ||
| @@ 19-81 (lines=63) @@ | ||
| 16 | /** |
|
| 17 | * "Disallow" directive element. |
|
| 18 | */ |
|
| 19 | class Disallow implements GroupMemberInterface, PathDirectiveInterface |
|
| 20 | { |
|
| 21 | /** |
|
| 22 | * Directive value. |
|
| 23 | * |
|
| 24 | * @var string |
|
| 25 | */ |
|
| 26 | private $value = ''; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * {@inheritdoc} |
|
| 30 | */ |
|
| 31 | public function __construct(string $raw) |
|
| 32 | { |
|
| 33 | if (!preg_match('/^disallow:\s*([^# ]+).*/i', $raw, $matches)) { |
|
| 34 | throw InvalidDirectiveException::create($raw); |
|
| 35 | } |
|
| 36 | ||
| 37 | $this->value = trim($matches[1]); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * {@inheritdoc} |
|
| 42 | */ |
|
| 43 | public static function getField(): string |
|
| 44 | { |
|
| 45 | return 'disallow'; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * {@inheritdoc} |
|
| 50 | */ |
|
| 51 | public function getValue(): string |
|
| 52 | { |
|
| 53 | return $this->value; |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * {@inheritdoc} |
|
| 58 | */ |
|
| 59 | public function __toString(): string |
|
| 60 | { |
|
| 61 | return sprintf('Disallow: %s', $this->value); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * {@inheritdoc} |
|
| 66 | */ |
|
| 67 | public function getRegex(): string |
|
| 68 | { |
|
| 69 | // "*" matches any sequence of characters (zero or more) |
|
| 70 | $value = str_replace('*', '.*', $this->value); |
|
| 71 | ||
| 72 | // "?" matches any character |
|
| 73 | $value = str_replace('?', '*', $value); |
|
| 74 | ||
| 75 | // "\" supresses syntactic significance of a special character |
|
| 76 | $value = str_replace('\*', '\?', $value); |
|
| 77 | $value = str_replace('\.*', '\*', $value); |
|
| 78 | ||
| 79 | return $value; |
|
| 80 | } |
|
| 81 | } |
|
| 82 | ||