almost there, timespan needs to stop after days

This commit is contained in:
flash 2020-12-28 01:09:41 +00:00
parent 111b74b20b
commit b1a6a4d047
4 changed files with 15 additions and 51 deletions

View file

@ -9,7 +9,7 @@ use DateTimeZone;
use InvalidArgumentException; use InvalidArgumentException;
class FWIF { 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 DEFAULT = 0; // Default behaviour
public const DISCARD_MILLISECONDS = 0x01; // Always exclude the millisecond component from DateTime public const DISCARD_MILLISECONDS = 0x01; // Always exclude the millisecond component from DateTime
@ -148,7 +148,7 @@ class FWIF {
return $packed; return $packed;
} }
private static function decodeInteger($data, int $flags): int { 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 { do {
$byte = ord(fgetc($data)); $byte = ord(fgetc($data));
$number |= ($byte & 0x7F) << $shift; $number |= ($byte & 0x7F) << $shift;
@ -475,6 +475,9 @@ class FWIF {
if($wsmh & self::TIMESPAN_FLAG_DMY) { if($wsmh & self::TIMESPAN_FLAG_DMY) {
$dmy = unpack('n', fread($data, 2))[1]; $dmy = unpack('n', fread($data, 2))[1];
$di->d = ($dmy >> self::TIMESPAN_DAYS_SHIFT) & self::DATETIME_DAY_MASK; $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; $di->m = ($dmy >> self::TIMESPAN_MONTH_SHIFT) & self::DATETIME_MONTH_MASK;
if($wsmh & self::TIMESPAN_FLAG_YEAR) if($wsmh & self::TIMESPAN_FLAG_YEAR)
$di->y = ord(fgetc($data)) $di->y = ord(fgetc($data))

View file

@ -12,33 +12,7 @@ header('Content-Type: ' . FWIF::CONTENT_TYPE);
if($request->match('GET', '/packages')) { if($request->match('GET', '/packages')) {
$tags = explode(';', (string)$request->getQueryParam('tags', FILTER_SANITIZE_STRING)); $tags = explode(';', (string)$request->getQueryParam('tags', FILTER_SANITIZE_STRING));
$packages = empty($tags) ? Patchouli::getPackages() : Patchouli::getPackagesWithTags($tags); $packages = empty($tags) ? Patchouli::getPackages() : Patchouli::getPackagesWithTags($tags);
echo FWIF::encode($packages);
$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);
return; return;
} }

View file

@ -4,7 +4,7 @@ namespace Patchouli\Dummy;
use Patchouli\IPackage; use Patchouli\IPackage;
use Patchouli\Version; use Patchouli\Version;
class DummyPackage implements IPackage, \JsonSerializable { class DummyPackage implements IPackage {
public function getId(): string { public function getId(): string {
return 'package-id'; return 'package-id';
} }
@ -23,6 +23,10 @@ class DummyPackage implements IPackage, \JsonSerializable {
public function fwifSerialize(): array { public function fwifSerialize(): array {
$data = [ $data = [
'id' => $this->getId(),
'name' => $this->getName(),
'version' => $this->getVersion(),
'deps' => [],
'null' => null, 'null' => null,
'zero' => 0, 'zero' => 0,
'u8' => 0x42, 'u8' => 0x42,
@ -55,9 +59,9 @@ class DummyPackage implements IPackage, \JsonSerializable {
'floatSqrt12' => M_SQRT1_2, 'floatSqrt12' => M_SQRT1_2,
'floatLnPi' => M_LNPI, 'floatLnPi' => M_LNPI,
'floatEuler' => M_EULER, 'floatEuler' => M_EULER,
//'floatNaN' => NAN, 'floatNaN' => NAN,
//'floatInf' => INF, 'floatInf' => INF,
//'floatNegInf' => -INF, 'floatNegInf' => -INF,
'floatZero' => 0.0, 'floatZero' => 0.0,
'floatNegZero' => -0.0, 'floatNegZero' => -0.0,
'invalid' => "\xFF\x25\x25\x02\xFF蠕。蝮F鄒守清\xFF\xFF\xFF", 'invalid' => "\xFF\x25\x25\x02\xFF蠕。蝮F鄒守清\xFF\xFF\xFF",
@ -70,21 +74,9 @@ class DummyPackage implements IPackage, \JsonSerializable {
'array' => ['e', 'a', 0x55], 'array' => ['e', 'a', 0x55],
'object' => new \stdClass, 'object' => new \stdClass,
'misaka' => '御坂 美琴', 'misaka' => '御坂 美琴',
'id' => $this->getId(),
'name' => $this->getName(),
'version' => $this->getVersion(),
'deps' => [],
]; ];
foreach($this->getDependencies() as $dependency) foreach($this->getDependencies() as $dependency)
$data['deps'][] = $dependency->getName(); $data['deps'][] = $dependency->getName();
return $data; 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;
}
} }

View file

@ -2,17 +2,12 @@
namespace Patchouli; namespace Patchouli;
use FWIF\FWIFSerializable; use FWIF\FWIFSerializable;
use JsonSerializable;
class Version implements FWIFSerializable, JsonSerializable { class Version implements FWIFSerializable {
public function fwifSerialize(): string { public function fwifSerialize(): string {
return (string)$this; return (string)$this;
} }
public function jsonSerialize(): string {
return (string)$this;
}
public function __toString(): string { public function __toString(): string {
return '1.0.0'; return '1.0.0';
} }