dvdoug/BoxPacker

Update jsonSerialize()

AsaXiao opened this issue · 1 comments

First of all, a big thank you to Doug, this is absolutely amazing and it really saved me a lot of time.

I just have a small suggestion, it would be great if you could update the jsonSerialize() function in PackedBox and PackedItem, add jsonSerialize() to the Box and Item Interface, then the users can define this function like what you have in TestBox and TestItem and get the info they want after pack(). Currently because I added new variables to my implementation of Box and Item, I need to modify PackBox and PackItem to get those variables after pack().

PackBox:

    public function jsonSerialize(): array
    {
        return [
            'box' => $this->box->jsonSerialize(),
            'weight' => $this->getWeight(),
            'items' => iterator_to_array($this->items),
        ];
    }

PackItem:

    public function jsonSerialize(): array
    {
        return [
            'x' => $this->x,
            'y' => $this->y,
            'z' => $this->z,
            'width' => $this->width,
            'length' => $this->length,
            'depth' => $this->depth,
            'item' => $this->item->jsonSerialize(),
        ];
    }

Hi @AsaXiao

Thanks for the suggestion - I can't change the interface in a 3.x release because that would cause backwards-compatibility problems, but I'll keep this idea in mind