Coder Social home page Coder Social logo

Better PHP Support about quicktype HOT 10 OPEN

HighLiuk avatar HighLiuk commented on June 19, 2024 1
Better PHP Support

from quicktype.

Comments (10)

comod avatar comod commented on June 19, 2024 1

Please explain what youve customized. I am currently analyzing quicktype especially for php as well

from quicktype.

comod avatar comod commented on June 19, 2024 1

At first glance, this looks exactly like what I need as well. I have already started building my own version as well. Things I've noticed or changed: enums (BackedEnum), validations which are currently completely pointless. From/To-Array instead of stdClass, Multiple-Files with namespaces and imports instead of a god-class. Let's go into the details in the new year and then we simply create a pull request.

from quicktype.

HighLiuk avatar HighLiuk commented on June 19, 2024

@comod here you go if you want to test it yourself > https://github.com/HighLiuk/quicktype

I've added support for a lot of different features ranging through PHP 7.3 (or older) to PHP 8.2 which has far way less boilerplate than previous versions of PHP especially for OOP.

I've added a dozen options as shown in this local version of the web app I've scaffolded to play with it. As you can see from the screenshot, choosing which PHP version to target is an option. This internally guarantees that you cannot opt in for features that are not available for that PHP version (e.g. readonly attributes are introduced in PHP 8.1. These may be turned off with a flag (defaults to true for simplicity) but are auto-disabled if the PHP version is lower than 8.1)

It is not 100% accurate (I recently noticed a few inconsistencies and so) and I've not run unit tests against it, but if a PR is accepted I would add the required tests.

Also, I've revised how the validation works in order to keep it simpler.

Let me know what do you think about.
Would be nice if a maintainer read this too (perhaps liking the thread would help?)

from quicktype.

HighLiuk avatar HighLiuk commented on June 19, 2024

I saw that other languages (like Go) have support for output multiple files but I didn't figure out how to implement it.
In PHP this is necessary when you leverage autoloads and if you have to do that manually over several generated classes, it easily becomes messy.

from quicktype.

dvdsgl avatar dvdsgl commented on June 19, 2024

Hello! You can make a PR and we can review it there.

from quicktype.

comod avatar comod commented on June 19, 2024

i figured out that it needs to remove this line to get it work @HighLiuk:
image
The generated code looks good so far!

I was able to generate multiple files with some few changes like this:
image

but i dont know how to add namespaces and imports. should this be inferred by additional properties in the json schema or should this be inferred by the original structure of the json-schema source. i only get fileName as parameter and not the whole filePath. so this is kind of tricky - any ideas?

from quicktype.

HighLiuk avatar HighLiuk commented on June 19, 2024

Hey there @comod thanks in advance

but i dont know how to add namespaces and imports. should this be inferred by additional properties in the json schema or should this be inferred by the original structure of the json-schema source. i only get fileName as parameter and not the whole filePath. so this is kind of tricky - any ideas?

probably you should look for the property types to get which classes / enums need to be imported at the top of the file.

But a clever option would be to add a "namespace" option to add a namespace on the top of the file. Like --namespace=Glideapps\Quicktype so for instance class Foo would have namespace Glideapps\Quicktype; on top of the file. This way according to PSR-4 you don't even need to add use Glideapps\Quicktype\Foo; if you add them to the same directory with the same namespace.

from quicktype.

comod avatar comod commented on June 19, 2024

I wasnt clear enough. What i try to achive is multiple files in different folders either according to the source structure (which is already organized in different folders) or to any additional information from the json schema (like a property named "package" or "namespace") if this makes any sense. Maybe this is out of scope and should be done as extra step

from quicktype.

comod avatar comod commented on June 19, 2024

I have now delved even deeper into the subject matter and have a few basic questions for the authors:

  • Is there already a concept for namespaces that spread across multiple levels, or are there only "global namespaces", i.e., ultimately a single namespace that is simply appended everywhere?

In the code, for example, I found this:

    private _globalForbiddenNamespace: Namespace | undefined;
    private _otherForbiddenNamespaces: Map<string, Namespace> | undefined;
    private _globalNamespace: Namespace | undefined;

However, I could not understand what this is used for. I believe this has nothing to do with the namespace I mean.

  • Is it possible to add and read a custom top-level property to the JSON schema? What would be the places that would need to be adjusted for this?
  • I have already tried using a custom Namer, but file and class names are inseparably constructed in the code. As a workaround, it would initially suffice to build namespaces based only on the "title" property (e.g., "title": "myNameSpace/myClassName"). File and class name, however, refer to the same Name/Namer, and I am not sure whether I should use these or whether there is not a better way.

Finding a solution outside is no less challenging, as not only namespaces have to be added, but all references also have to be adjusted. Additionally, the sources would have to be read and mapped, etc. It would simply be more elegant to get a grip on this with this one tool on the spot.

If it is to remain with just the one global namespace, then what @HighLiuk suggested is indeed sufficient.

from quicktype.

HighLiuk avatar HighLiuk commented on June 19, 2024

Not sure if I understand everything @comod, but I agree that it should be simpler to use QuickType's internal API to easily print on different files.

from quicktype.

Related Issues (20)

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.