Skip to content

Commit 21db6e9

Browse files
committed
Bugfix Token parsing on item context
1 parent 2c15466 commit 21db6e9

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1414

1515
- `InnerList::fromHttpValue` accepts Optional White Spaces at the start of its textual representation.
1616
- `Parameters::fromHttpValue` accepts Optional White Spaces at the start of its textual representation.
17+
- `Item::fromHttpValue` bugfix parsing Token data type.
1718

1819
### Deprecated
1920

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# make install
2+
install:
3+
@docker run --rm -it -v$(PWD):/app composer install
4+
5+
# unit tests
6+
phpunit:
7+
@docker run --rm -it -v$(PWD):/app --workdir=/app php:8.1-cli-alpine vendor/bin/phpunit --coverage-text
8+
9+
# phpstan
10+
phpstan:
11+
@docker run --rm -it -v$(PWD):/app --workdir=/app php:8.1-cli-alpine vendor/bin/phpstan analyse -l max -c phpstan.neon src --ansi
12+
13+
# coding style fix
14+
phpcs:
15+
@docker run --rm -it -v$(PWD):/app --workdir=/app php:8.1-cli-alpine vendor/bin/php-cs-fixer fix -vvv --allow-risky=yes
16+
17+
# test
18+
test:
19+
@docker run --rm -it -v$(PWD):/app --workdir=/app php:8.1-cli-alpine php ./test.php
20+
21+
.PHONY: install phpunit phpstan phpcs test

autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
if (is_readable($file)) {
1414
require $file;
1515
}
16-
});
16+
});

src/Item.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ private static function parseToken(string $string): array
150150
$regexp .= '$';
151151
}
152152

153-
preg_match('/'.$regexp.'/i', $string, $found);
153+
if (1 !== preg_match('/'.$regexp.'/i', $string, $found)) {
154+
throw new SyntaxError("The HTTP textual representation `$string` for a Token contains invalid characters.");
155+
}
154156

155157
return [
156158
Token::fromString($found['token']),
@@ -193,7 +195,12 @@ private static function parseBytesSequence(string $string): array
193195
*/
194196
private static function parseNumber(string $string): array
195197
{
196-
if (1 !== preg_match('/^(?<number>-?\d+(?:\.\d+)?)(?:[^\d.]|$)/', $string, $found)) {
198+
$regexp = '^(?<number>-?\d+(?:\.\d+)?)(?:[^\d.]|$)';
199+
if (!str_contains($string, ';')) {
200+
$regexp = '^(?<number>-?\d+(?:\.\d+)?)$';
201+
}
202+
203+
if (1 !== preg_match('/'.$regexp.'/', $string, $found)) {
197204
throw new SyntaxError("The HTTP textual representation `$string` for a number contains invalid characters.");
198205
}
199206

src/TokenTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
namespace Bakame\Http\StructuredFields;
66

7-
use PHPUnit\Framework\TestCase;
87
use function var_export;
98

109
/**
1110
* @coversDefaultClass \Bakame\Http\StructuredFields\Token
1211
*/
13-
final class TokenTest extends TestCase
12+
final class TokenTest extends StructuredFieldTest
1413
{
1514
/** @var array|string[] */
1615
protected array $paths = [

0 commit comments

Comments
 (0)