Code Monkey home page Code Monkey logo

Comments (2)

doc987 avatar doc987 commented on May 26, 2024

Looks like this is still outstanding. I tried sending a serialized Date object from Javascript, and it was deserialized to the object shown below. I was hoping to get a DateTime object instead.

MessagePack\Ext Object
(
    [type] => -1
    [data] => some binary data
)

I wasn't able to get a type transformer to work either. The unpack method in the compressed example below fails.

class DateTimeTransformer implements \MessagePack\TypeTransformer\Unpackable
{
    private $type;

    public function __construct(int $type)
    {
        $this->type = $type;
    }

    public function getType() : int
    {
        return $this->type;
    }

    public function unpack(\MessagePack\BufferUnpacker $unpacker, int $extLength)
    {
        //how does one unpack a timestamp?
        return new \DateTimeImmutable($unpacker->unpack???());
    }
}

$transformer = new DateTimeTransformer(-1);
$unpacker = new \MessagePack\BufferUnpacker();
$unpacker->registerTransformer($transformer);
$data = $unpacker->reset($packed)->unpack();

from msgpack.php.

rybakit avatar rybakit commented on May 26, 2024

@doc987 I took msgpack-lite library as an example from your other issue (msgpack/msgpack-php#127). As you noticed, it has native support for Date objects (ext type 0x0D). So, to be able to deserialize these dates into PHP DateTime(Immutable) objects you can create the following transformer:

namespace App\MessagePack;

use MessagePack\BufferUnpacker;
use MessagePack\TypeTransformer\Unpackable;

final class MsgpackLiteJsDateTransformer implements Unpackable
{
    public function getType() : int
    {
        return 0x0d;
    }

    public function unpack(BufferUnpacker $unpacker, int $extLength) : \DateTimeImmutable
    {
        return \DateTimeImmutable::createFromFormat('U.u', $unpacker->unpackFloat() / 1000);
    }
}

Test:

namespace App\MessagePack;

use MessagePack\BufferUnpacker;

// msgpack.encode(new Date('2018-08-07T22:21:45.580Z')) = c7090dcb42765167b52ec000
$packedJsDate = hex2bin('c7090dcb42765167b52ec000');

$unpacker = new BufferUnpacker($packedJsDate);
$unpacker->registerTransformer(new MsgpackLiteJsDateTransformer());

var_dump($unpacker->unpack());

/*
class DateTimeImmutable#4 (3) {
  public $date =>
  string(26) "2018-08-07 22:21:45.580000"
  public $timezone_type =>
  int(1)
  public $timezone =>
  string(6) "+00:00"
}
*/

from msgpack.php.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.