Is there a way to use toString template in phpStorm like in InteliJ?

In my classes I need to implenet `jsonSerialize` method. I'm aware that I can't add my own code generator, but I thought that I can change velocity templates for similar method like `toString`. Both methods get class properties and build some custom output. However in phpStorm there is no such method in code templates. I found a nice plugin https://plugins.jetbrains.com/plugin/10098-code-generator but it is dedicated only for InteliJ.

Is there a way to overwrite some template, which has got access to class properties or create own code generator other then writing own plugin?

2 comments

Hi there,

Please clarify what exactly do you mean by "toString template"? How do you generate it?

 

P.S. Maybe you can just use what's already available and then use custom Live Template to quickly insert your own code..? https://confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStorm

0

I was thinking about following entry: https://www.jetbrains.com/help/idea/generating-code.html and "Generate toString()" section. In Java it will generate following method:

public String toString() { 
return "MyClass{" + "name='" + name + '\'' + ", url='" + url + '\'' + ", port=" + port + ", validIps=" + Arrays.toString(validIps) + '}';
}

What I need in php is almost the same implementation as toString for JAVA but for JSON representation when encoded with json_encode -> https://php.net/manual/en/jsonserializable.jsonserialize.php

Live Template and File Template can't do it. I need to read class properties (like generate getters and setters is doing) and then generate one method with all properties. Something like that:

 

public function jsonSerialize() {
return [
'id' => $this->getId(),
'objectId' => $this->getObjectId(),
'sortOrder' => $this->getSortOrder(),
'name' => $this->getName(),
'address' => $this->getAddress(),
'prices' => $this->getPrices(),
'isPermanent' => $this->getIsPermanent(),
'surface' => $this->getSurface(),
'description' => $this->getDescription(),
'isMobile' => $this->getIsMobile(),
'chartColor' => $this->getChartColor(),
'maxDiscount' => $this->getMaxDiscount(),
'showPriceDiscount' => $this->getShowPriceDiscount(),
'isAvailableForMobile' => $this->getIsAvailableForMobile(),
'floor' => $this->getFloor(),
'features' => $this->getFeatures(),
'additionalTimeBeforeReservation' => $this->getAdditionalTimeBeforeReservation(),
'additionalTimeAfterReservation' => $this->getAdditionalTimeAfterReservation(),
'isBookingAvailableForClient' => $this->getIsBookingAvailableForClient(),
'connectedRoomId' => $this->getConnectedRoomId(),
'evaluations' => $this->getEvaluations(),
'isAvailableForDistributor' => $this->getIsAvailableForDistributor(),
'reservationLimits' => $this->getReservationLimits(),
'isModule' => $this->getIsModule(),
'moduleGroupId' => $this->getModuleGroupId(),
'canSubmitRequestIfUnconfirmed' => $this->getCanSubmitRequestIfUnconfirmed(),
'typeId' => $this->getTypeId(),
'images' => $this->getImages(),
];
0

Please sign in to leave a comment.