simresults
simresults copied to clipboard
Implement json export
Easy converting to JSON would be of big value for users who do not know PHP and just want to get a fast universal JSON output.
Hi,
I'm interested in getting JSON output for your classes to store the results in MongoDB. I went ahead and experimented with the code base, knowing not much of PHP:
use JsonSerializable;
class Vehicle implements JsonSerializable {
* Get the json representation of the object
*
* @return string
*/
public function jsonSerialize() {
return [
'name' => $this->getName(),
'type' => $this->getType(),
'class' => $this->getClass(),
'number' => $this->getNumber(),
'ballast' => $this->getBallast(),
'restrictor' => $this->getRestrictor(),
'skin' => $this->getSkin(),
'friendlyName' => $this->getFriendlyName()
];
}
As you're far more knowledgable on PHP than I ever will be, do you think this is a sound approach? If so, I'll continue and push it to github.
Yes I think this would be the approach :) Anyway create it what works for you. Eventually when I have time one day, I might use it. Thanks for wanting to share with others!
Hi, thanks for the encouraging words. I cloned your repo and added JSON serialize functions to most classes: https://github.com/tspindler-cms/simresults I'm not sure on how to proceed on testing these, would you happen to have some ideas for that?
Cool! :+1: Take a look at the specs folder. You could add a function called called it_can_cast_to_json
to a specific class test file. Set some test values and then check the json output.
For example see it_can_cast_to_string
on LapSpec
. Very similar:
https://github.com/mauserrifle/simresults/blob/develop/spec/Simresults/LapSpec.php#L23
You can run the spec tests using ./vendor/bin/phpspec run