# Getting Started ## Installation If you use [the API Platform distribution](../distribution/index.md), the Schema Generator is already installed as a development dependency of your project and can be invoked through Docker: $ docker-compose exec php vendor/bin/schema The Schema Generator can also [be downloaded independently as a PHAR](https://github.com/api-platform/schema-generator/releases) or installed in an existing project using [Composer](https://getcomposer.org): $ composer require --dev api-platform/schema-generator ## Model Scaffolding Start by browsing [Schema.org](https://schema.org) and pick types applicable to your application. The website provides tons of schemas including (but not limited to) representations of people, organizations, events, postal addresses, creative work and e-commerce structures. Then, write a simple YAML config file similar to the following. Here we will generate a data model for an address book with the following data: - a [`Person`](http://schema.org/Person) which inherits from [`Thing`](http://schema.org/Thing) - a [`PostalAddress`](http://schema.org/PostalAddress) which inherits from [`ContactPoint`](http://schema.org/ContactPoint), which itself inherits from [`StructuredValue`](http://schema.org/StructuredValue), etc. ```yaml # api/config/schema.yaml # The list of types and properties we want to use types: # Parent class of Person Thing: properties: name: ~ Person: properties: familyName: ~ givenName: ~ additionalName: ~ address: ~ PostalAddress: # Disable the generation of the class hierarchy for this type parent: false properties: # Force the type of the addressCountry property to text addressCountry: { range: "Text" } addressLocality: ~ addressRegion: ~ postOfficeBoxNumber: ~ postalCode: ~ streetAddress: ~ ``` Run the generator with this config file as parameter: $ vendor/bin/schema generate-types api/src/ api/config/schema.yaml Using docker: $ docker-compose exec php vendor/bin/schema generate-types src/ config/schema.yaml The following classes will be generated: ```yaml types: Person: properties: name: ~ familyName: ~ givenName: ~ additionalName: ~ address: ~ PostalAddress: properties: # Force the type of the addressCountry property to text addressCountry: { range: "Text" } addressLocality: ~ addressRegion: ~ postOfficeBoxNumber: ~ postalCode: ~ streetAddress: ~ ``` ```php id; } public function setFamilyName(?string $familyName): void { $this->familyName = $familyName; } public function getFamilyName(): ?string { return $this->familyName; } public function setGivenName(?string $givenName): void { $this->givenName = $givenName; } public function getGivenName(): ?string { return $this->givenName; } public function setAdditionalName(?string $additionalName): void { $this->additionalName = $additionalName; } public function getAdditionalName(): ?string { return $this->additionalName; } public function setAddress(?PostalAddress $address): void { $this->address = $address; } public function getAddress(): ?PostalAddress { return $this->address; } } ``` ```php id; } public function setAddressCountry(?string $addressCountry): void { $this->addressCountry = $addressCountry; } public function getAddressCountry(): ?string { return $this->addressCountry; } public function setAddressLocality(?string $addressLocality): void { $this->addressLocality = $addressLocality; } public function getAddressLocality(): ?string { return $this->addressLocality; } public function setAddressRegion(?string $addressRegion): void { $this->addressRegion = $addressRegion; } public function getAddressRegion(): ?string { return $this->addressRegion; } public function setPostOfficeBoxNumber(?string $postOfficeBoxNumber): void { $this->postOfficeBoxNumber = $postOfficeBoxNumber; } public function getPostOfficeBoxNumber(): ?string { return $this->postOfficeBoxNumber; } public function setPostalCode(?string $postalCode): void { $this->postalCode = $postalCode; } public function getPostalCode(): ?string { return $this->postalCode; } public function setStreetAddress(?string $streetAddress): void { $this->streetAddress = $streetAddress; } public function getStreetAddress(): ?string { return $this->streetAddress; } } ``` ```php id; } public function setName(?string $name): void { $this->name = $name; } public function getName(): ?string { return $this->name; } } ``` Note that the generator takes care of creating directories corresponding to the namespace structure. Without configuration file, the tool will build the entire Schema.org vocabulary. If no properties are specified for a given type, all its properties will be generated. The generator also supports enumeration generation. For subclasses of [`Enumeration`](https://schema.org/Enumeration), the generator will automatically create a class extending the Enum type provided by [myclabs/php-enum](https://github.com/myclabs/php-enum). Don't forget to install this library in your project. Refer you to PHP Enum documentation to see how to use it. The Symfony validation annotation generator automatically takes care of enumerations to validate choices values. A config file generating an enum class: ```yaml types: OfferItemCondition: # The generator will automatically guess that OfferItemCondition is subclass of Enum properties: {} # Remove all properties of the parent class ``` The related PHP class: ```php