Browse Source

Style fixes

master
J. King 4 years ago
parent
commit
096cf6ce3f
  1. 18
      lib/Date.php
  2. 1
      lib/Parser/Construct.php
  3. 2
      lib/Parser/HTTP/Message.php
  4. 4
      lib/Parser/Parser.php
  5. 2
      tests/cases/AbstractParserTestCase.php
  6. 17
      tests/cases/Util/Date/DateTest.php

18
lib/Date.php

@ -52,7 +52,7 @@ class Date extends \DateTimeImmutable implements \JsonSerializable {
'PST' => "-0800",
'PDT' => "-0700",
];
/*
Important formats:
@ -73,25 +73,25 @@ class Date extends \DateTimeImmutable implements \JsonSerializable {
}
/** Returns a date parsed from a string in any of the following formats:
*
*
* - RFC 3339
* - RFC 822
* - RFC 850
* - ANSI C asctime()
*
* Subsets of RFC 822 and RFC 850 formats and asctime() format are used
*
* Subsets of RFC 822 and RFC 850 formats and asctime() format are used
* by RFC 7231 (HTTP), and the latter definition was consulted for
* guidance. RFC 3339 and RFC 822 formats are both supported in full, and
* the ambiguous century of RFC 822 and RFC 850 formats is interpreted per
* RFC 7231. Timezones used for RFC 822 are also accepted for RFC 850.
*
*
* All formats except asctime() are also accepted with subsecond precision,
* or with minute precision. Whitespace before the timezone may be omitted
* or used in all formats as well (RFC 3339 does not normally allow
* whitespace, while other formats require it).
*
* or used in all formats as well (RFC 3339 does not normally allow
* whitespace, while other formats require it).
*
* If no timezone is specified, -00:00 is used.
*
*
* @see https://tools.ietf.org/html/rfc3339
* @see https://tools.ietf.org/html/rfc822#section-5
* @see https://tools.ietf.org/html/rfc850#section-2.1.4

1
lib/Parser/Construct.php

@ -7,7 +7,6 @@ declare(strict_types=1);
namespace MensBeam\Lax\Parser;
use MensBeam\Lax\Collection;
use MensBeam\Lax\Date;
trait Construct {
/** Trims plain text and collapses whitespace */

2
lib/Parser/HTTP/Message.php

@ -119,7 +119,7 @@ class Message {
}
public function getMaxAge(): ?\DateInterval {
$out = 0;
$out = 0;
$maxAge = 0;
$sharedMaxAge = 0;
foreach ($this->parseHeader("Cache-Control", self::CCON_PATTERN, true) ?? [] as $t) {

4
lib/Parser/Parser.php

@ -34,7 +34,7 @@ abstract class Parser {
} elseif (preg_match('/^\s*</s', $data)) {
// distinguish between XML feeds and HTML; first skip any comments before the root element
$offset = preg_match('/^\s*(?:<!--(?:[^\-]|-(?!->)*-->\s*)*/s', $data, $match) ? strlen($match[0]) : 0;
$prefix = substr($data, $offset, 100); //
$prefix = substr($data, $offset, 100);
if (preg_match('/^<(?:!DOCTYPE\s+html|html|body|head|table|div|title|p|link|meta)[\s>]/si', $prefix)) {
return "text/html";
} elseif (preg_match('/^<rss[\s>\/]/', $prefix)) {
@ -48,7 +48,7 @@ abstract class Parser {
} else {
// FIIXME: Is there a better fallback that could used here?
"application/xml";
}
}
} else {
return "application/octet-stream";
}

2
tests/cases/AbstractParserTestCase.php

@ -39,7 +39,7 @@ class AbstractParserTestCase extends \PHPUnit\Framework\TestCase {
if (is_object($test->input)) {
assert((isset($test->input->head) && $test->input->head instanceof \stdClass) || (isset($test->input->body) && is_string($test->input->body)), "Input is not in a correct format");
$test->input = new Response($test->input->status ?? 200, (array) ($test->input->head ?? []), $test->input->body ?? null);
}
}
yield "$file: {$description}" => [
$test->input,
$test->type ?? "",

17
tests/cases/Util/Date/DateTest.php

@ -17,26 +17,26 @@ class DateTest extends \PHPUnit\Framework\TestCase {
$this->assertInstanceOf(\DateTimeImmutable::class, $d);
$this->assertSame("2001-05-22T14:55:23.000000Z", $d->setTimezone(new \DateTimeZone("UTC"))->format("Y-m-d\TH:i:s.u\Z"));
}
public function testNormalizeADate(): void {
$d = new Date("2001-05-22T14:55:23-01:00");
$this->assertSame("2001-05-22T15:55:23.000000Z", $d->normalize());
}
public function testCastADateToString(): void {
$d = new Date("2001-05-22T14:55:23-01:00");
$this->assertSame("2001-05-22T14:55:23-01:00", (string) $d);
$d = new Date("2001-05-22T14:55:23.55Z");
$this->assertSame("2001-05-22T14:55:23.550000+00:00", (string) $d);
}
public function testSerializeADateToJson(): void {
$d = new Date("2001-05-22T14:55:23-01:00");
$this->assertSame('"2001-05-22T14:55:23-01:00"', json_encode($d));
$d = new Date("2001-05-22T14:55:23.55Z");
$this->assertSame('"2001-05-22T14:55:23.550000+00:00"', json_encode($d));
}
public function testCreateADateFromADatetimeInterfaceInstance(): void {
$m = new \DateTime("2001-05-22T14:55:23-01:00");
$d = Date::createFromMutable($m);
@ -45,7 +45,7 @@ class DateTest extends \PHPUnit\Framework\TestCase {
$d = Date::createFromImmutable($i);
$this->assertSame("2001-05-22T15:55:23.000000Z", $d->normalize());
}
public function testCreateADateFromAFormatString(): void {
$f = '!Y-m-d\TH:i:sP';
$d = Date::createFromFormat($f, "2001-05-22T14:55:23-01:00");
@ -58,7 +58,7 @@ class DateTest extends \PHPUnit\Framework\TestCase {
/** @dataProvider provideParsableStrings */
public function testCreateADateFromAString(string $input, ?string $exp): void {
$act = Date::createFromString($input);
if (!$exp) {
if (!$exp) {
$this->assertNull($act);
} else {
$this->assertSame($exp, (string) $act);
@ -68,14 +68,13 @@ class DateTest extends \PHPUnit\Framework\TestCase {
public function provideParsableStrings(): iterable {
foreach (new \GlobIterator(__DIR__."/test-*.yaml", \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::KEY_AS_FILENAME) as $file => $path) {
foreach ((new YamlParser)->parseFile($path, Yaml::PARSE_OBJECT_FOR_MAP) as $description => $test) {
if (!is_null($test->output) && substr($test->output, -6) === "-00:00") {
if (!is_null($test->output) && substr($test->output, -6) === "-00:00") {
// PHP does not preserve the -0000 timezone
$test->output[-6] = "+";
}
if (is_array($test->input)) {
foreach($test->input as $input) {
foreach ($test->input as $input) {
yield "$description ($input)" => [$input, $test->output];
}
} else {
yield "$description" => [$test->input, $test->output];

Loading…
Cancel
Save