From b1a6a4d047dc5245f01b3f744243fa7a59d28c0b Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 28 Dec 2020 01:09:41 +0000 Subject: [PATCH] almost there, timespan needs to stop after days --- lib/FWIF/FWIF.php | 7 +++++-- public/index.php | 28 +--------------------------- src/Dummy/DummyPackage.php | 24 ++++++++---------------- src/Version.php | 7 +------ 4 files changed, 15 insertions(+), 51 deletions(-) diff --git a/lib/FWIF/FWIF.php b/lib/FWIF/FWIF.php index 490ed3a..af39473 100644 --- a/lib/FWIF/FWIF.php +++ b/lib/FWIF/FWIF.php @@ -9,7 +9,7 @@ use DateTimeZone; use InvalidArgumentException; class FWIF { - public const CONTENT_TYPE = 'text/plain; charset=us-ascii'; // TODO: come up with a mime type + public const CONTENT_TYPE = 'application/x.fwif'; public const DEFAULT = 0; // Default behaviour public const DISCARD_MILLISECONDS = 0x01; // Always exclude the millisecond component from DateTime @@ -148,7 +148,7 @@ class FWIF { return $packed; } private static function decodeInteger($data, int $flags): int { - $number = 0; $shift = 0; $o = 0; $size = PHP_INT_SIZE * 8; + $number = 0; $shift = 0; $size = PHP_INT_SIZE * 8; do { $byte = ord(fgetc($data)); $number |= ($byte & 0x7F) << $shift; @@ -475,6 +475,9 @@ class FWIF { if($wsmh & self::TIMESPAN_FLAG_DMY) { $dmy = unpack('n', fread($data, 2))[1]; $di->d = ($dmy >> self::TIMESPAN_DAYS_SHIFT) & self::DATETIME_DAY_MASK; + + // OOPS! THESE WILL BE HORRIBLY INACCURATE! + // Perhaps go back to the drawing board and tack months and years onto days? $di->m = ($dmy >> self::TIMESPAN_MONTH_SHIFT) & self::DATETIME_MONTH_MASK; if($wsmh & self::TIMESPAN_FLAG_YEAR) $di->y = ord(fgetc($data)) diff --git a/public/index.php b/public/index.php index b4e15f2..19c8169 100644 --- a/public/index.php +++ b/public/index.php @@ -12,33 +12,7 @@ header('Content-Type: ' . FWIF::CONTENT_TYPE); if($request->match('GET', '/packages')) { $tags = explode(';', (string)$request->getQueryParam('tags', FILTER_SANITIZE_STRING)); $packages = empty($tags) ? Patchouli::getPackages() : Patchouli::getPackagesWithTags($tags); - - $encoded = FWIF::encode($packages); - echo 'FWIF ' . strlen($encoded) . ' bytes ' . $encoded; - - echo "\r\n\r\n--------------------\r\n\r\n"; - - $jsonEncoded = json_encode($packages, JSON_INVALID_UTF8_SUBSTITUTE); - echo 'JSON ' . strlen($jsonEncoded) . ' bytes ' . $jsonEncoded; - echo "\r\n\r\n--------------------\r\n\r\n"; - - $hexdump = bin2hex($encoded); $hexdumpSect = 8; $hexdumpSize = 32; - for($i = 0; $i < strlen($hexdump) / $hexdumpSize; ++$i) { - $line = substr($hexdump, $i * $hexdumpSize, $hexdumpSize); - echo str_pad(dechex($i * $hexdumpSize), 4, '0', STR_PAD_LEFT) . ' '; - for($j = 0; $j < strlen($line) / $hexdumpSect; ++$j) - echo substr($line, $j * $hexdumpSect, $hexdumpSect) . ' '; - echo "\r\n"; - } - - echo "\r\n--------------------\r\n\r\n"; - - var_dump([(object)$packages[0]->fwifSerialize()]); - - echo "\r\n--------------------\r\n\r\n"; - - $decoded = FWIF::decode($encoded); - var_dump($decoded); + echo FWIF::encode($packages); return; } diff --git a/src/Dummy/DummyPackage.php b/src/Dummy/DummyPackage.php index 3005935..c35ba64 100644 --- a/src/Dummy/DummyPackage.php +++ b/src/Dummy/DummyPackage.php @@ -4,7 +4,7 @@ namespace Patchouli\Dummy; use Patchouli\IPackage; use Patchouli\Version; -class DummyPackage implements IPackage, \JsonSerializable { +class DummyPackage implements IPackage { public function getId(): string { return 'package-id'; } @@ -23,6 +23,10 @@ class DummyPackage implements IPackage, \JsonSerializable { public function fwifSerialize(): array { $data = [ + 'id' => $this->getId(), + 'name' => $this->getName(), + 'version' => $this->getVersion(), + 'deps' => [], 'null' => null, 'zero' => 0, 'u8' => 0x42, @@ -55,9 +59,9 @@ class DummyPackage implements IPackage, \JsonSerializable { 'floatSqrt12' => M_SQRT1_2, 'floatLnPi' => M_LNPI, 'floatEuler' => M_EULER, - //'floatNaN' => NAN, - //'floatInf' => INF, - //'floatNegInf' => -INF, + 'floatNaN' => NAN, + 'floatInf' => INF, + 'floatNegInf' => -INF, 'floatZero' => 0.0, 'floatNegZero' => -0.0, 'invalid' => "\xFF\x25\x25\x02\xFF蠕。蝮F鄒守清\xFF\xFF\xFF", @@ -70,21 +74,9 @@ class DummyPackage implements IPackage, \JsonSerializable { 'array' => ['e', 'a', 0x55], 'object' => new \stdClass, 'misaka' => '御坂 美琴', - 'id' => $this->getId(), - 'name' => $this->getName(), - 'version' => $this->getVersion(), - 'deps' => [], ]; foreach($this->getDependencies() as $dependency) $data['deps'][] = $dependency->getName(); return $data; } - - public function jsonSerialize() { - $serial = $this->fwifSerialize(); - $serial['datetime'] = $serial['datetime']->format(\DateTimeInterface::ATOM); - $serial['datetimeNegative'] = $serial['datetimeNegative']->format(\DateTimeInterface::ATOM); - $serial['datetimeNow'] = $serial['datetimeNow']->format(\DateTimeInterface::ATOM); - return $serial; - } } diff --git a/src/Version.php b/src/Version.php index bf0caed..7f7436b 100644 --- a/src/Version.php +++ b/src/Version.php @@ -2,17 +2,12 @@ namespace Patchouli; use FWIF\FWIFSerializable; -use JsonSerializable; -class Version implements FWIFSerializable, JsonSerializable { +class Version implements FWIFSerializable { public function fwifSerialize(): string { return (string)$this; } - public function jsonSerialize(): string { - return (string)$this; - } - public function __toString(): string { return '1.0.0'; }