-
-
Notifications
You must be signed in to change notification settings - Fork 108
Add MongoDB annotation generator #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
src/AnnotationGenerator/DoctrineMongoDBAnnotationGenerator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace ApiPlatform\SchemaGenerator\AnnotationGenerator; | ||
|
||
use ApiPlatform\SchemaGenerator\CardinalitiesExtractor; | ||
use ApiPlatform\SchemaGenerator\TypesGenerator; | ||
|
||
/** | ||
* Doctrine MongoDB annotation generator. | ||
* | ||
* @author Andrew Meshchanchuk <andrew.meshchanchuk@gmail.com>> | ||
*/ | ||
class DoctrineMongoDBAnnotationGenerator extends AbstractAnnotationGenerator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generateClassAnnotations($className) | ||
{ | ||
$class = $this->classes[$className]; | ||
|
||
if ($class['isEnum']) { | ||
return []; | ||
} | ||
|
||
if (isset($this->config['types'][$class['resource']->localName()]['doctrine']['inheritanceMapping'])) { | ||
$inheritanceMapping = $this->config['types'][$class['resource']->localName()]['doctrine']['inheritanceMapping']; | ||
} else { | ||
$inheritanceMapping = $class['abstract'] ? '@MongoDB\MappedSuperclass' : '@MongoDB\Document'; | ||
} | ||
|
||
return [ | ||
'', | ||
$inheritanceMapping, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generateFieldAnnotations($className, $fieldName) | ||
{ | ||
$this->classes[$className]; | ||
$field = $this->classes[$className]['fields'][$fieldName]; | ||
|
||
$annotations = []; | ||
|
||
if ($field['isEnum']) { | ||
if ($field['isArray']) { | ||
$type = 'simple_array'; | ||
} else { | ||
$type = 'string'; | ||
} | ||
} else { | ||
switch ($field['range']) { | ||
case 'Boolean': | ||
$type = 'boolean'; | ||
break; | ||
case 'Date': | ||
// No break | ||
case 'DateTime': | ||
$type = 'date'; | ||
break; | ||
case 'Time': | ||
$type = 'time'; | ||
break; | ||
case 'Number': | ||
// No break | ||
case 'Float': | ||
$type = 'float'; | ||
break; | ||
case 'Integer': | ||
$type = 'integer'; | ||
break; | ||
case 'Text': | ||
// No break | ||
case 'URL': | ||
$type = 'string'; | ||
break; | ||
} | ||
} | ||
|
||
if (isset($type)) { | ||
if (!$field['isId']) { | ||
$annotation = '@MongoDB\Field'; | ||
|
||
if ($field['isArray']) { | ||
$type = 'collection'; | ||
} | ||
|
||
$annotation .= sprintf('(type="%s")', $type); | ||
|
||
$annotations[] = $annotation; | ||
} | ||
} else { | ||
if ($field['cardinality'] === CardinalitiesExtractor::CARDINALITY_0_1 | ||
|| $field['cardinality'] === CardinalitiesExtractor::CARDINALITY_1_1 | ||
|| $field['cardinality'] === CardinalitiesExtractor::CARDINALITY_N_0 | ||
|| $field['cardinality'] === CardinalitiesExtractor::CARDINALITY_N_1) { | ||
$annotations[] = sprintf('@MongoDB\ReferenceOne(targetDocument="%s", simple=true))', $this->getRelationName($field['range'])); | ||
} elseif ($field['cardinality'] === CardinalitiesExtractor::CARDINALITY_0_N | ||
|| $field['cardinality'] === CardinalitiesExtractor::CARDINALITY_1_N | ||
|| $field['cardinality'] === CardinalitiesExtractor::CARDINALITY_N_N) { | ||
$annotations[] = sprintf('@MongoDB\ReferenceMany(targetDocument="%s", simple=true)', $this->getRelationName($field['range'])); | ||
} | ||
} | ||
|
||
if ($field['isId']) { | ||
$annotations[] = '@MongoDB\Id'; | ||
} | ||
|
||
return $annotations; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generateUses($className) | ||
{ | ||
$resource = $this->classes[$className]['resource']; | ||
|
||
$subClassOf = $resource->get('rdfs:subClassOf'); | ||
$typeIsEnum = $subClassOf && $subClassOf->getUri() === TypesGenerator::SCHEMA_ORG_ENUMERATION; | ||
|
||
return $typeIsEnum ? [] : ['Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB']; | ||
} | ||
|
||
/** | ||
* Gets class or interface name to use in relations. | ||
* | ||
* @param string $range | ||
* | ||
* @return string | ||
*/ | ||
private function getRelationName($range) | ||
{ | ||
$class = $this->classes[$range]; | ||
|
||
if (isset($class['interfaceName'])) { | ||
return $class['interfaceName']; | ||
} | ||
|
||
return $class['name']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# The PHP namespace of generated entities | ||
namespaces: | ||
entity: 'AddressBook\Document' | ||
# Enable DunglasApiAnnotationGenerator | ||
annotationGenerators: | ||
- ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator | ||
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineMongoDBAnnotationGenerator | ||
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator | ||
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DunglasApiAnnotationGenerator | ||
# The list of types and properties we want to use | ||
types: | ||
Person: | ||
parent: false | ||
properties: | ||
name: ~ | ||
familyName: ~ | ||
givenName: ~ | ||
additionalName: ~ | ||
gender: ~ | ||
address: { range: PostalAddress } | ||
birthDate: ~ | ||
telephone: ~ | ||
email: ~ | ||
jobTitle: ~ | ||
# Default relation table name would be "person_organization" for all following fields, but we customize them | ||
affiliation: ~ | ||
brand: { relationTableName: "person_brand"} | ||
memberOf: { range: "Organization", cardinality: (1..*), relationTableName: "person_memberof"} | ||
worksFor: { range: "Organization", cardinality: (0..*), relationTableName: "person_worksfor"} | ||
# url field is a custom one without definition, it should render error | ||
url: ~ | ||
friends: { range: "Person", cardinality: (0..*) } | ||
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: ~ | ||
Organization: | ||
parent: false | ||
properties: | ||
name: ~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
header: | | ||
/* | ||
* This file is part of the Ecommerce package. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
rdfa: | ||
- | ||
uri: tests/data/schema.rdfa | ||
format: rdfa | ||
relations: | ||
- tests/data/v1.owl | ||
namespaces: | ||
entity: 'Dunglas\EcommerceBundle\Document' | ||
enum: 'Dunglas\EcommerceBundle\Enum' | ||
interface: 'Dunglas\EcommerceBundle\Model' | ||
annotationGenerators: | ||
- ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator | ||
- ApiPlatform\SchemaGenerator\AnnotationGenerator\ConstraintAnnotationGenerator | ||
- ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineMongoDBAnnotationGenerator | ||
author: "Kévin Dunglas <dunglas@gmail.com>" | ||
debug: true | ||
useInterface: true | ||
checkIsGoodRelations: true | ||
types: | ||
Thing: | ||
properties: | ||
name: ~ | ||
description: ~ | ||
image: { range: ImageObject } | ||
additionalType: ~ | ||
Product: | ||
properties: | ||
sku: | ||
cardinality: "(0..1)" | ||
url: ~ | ||
brand: ~ | ||
productID: ~ | ||
releaseDate: ~ | ||
offers: ~ | ||
itemCondition: ~ | ||
gtin13: ~ | ||
gtin14: ~ | ||
gtin8: ~ | ||
mpn: ~ | ||
color: ~ | ||
depth: { range: "Text" } | ||
height: { range: "Text" } | ||
weight: { range: "Text" } | ||
width: { range: "Text" } | ||
seller: { range: "Seller" } | ||
Brand: | ||
parent: "Thing" | ||
properties: | ||
logo: { range: "ImageObject" } | ||
Seller: | ||
parent: false | ||
guessFrom: Person | ||
properties: | ||
name: ~ | ||
birthDate: ~ | ||
ImageObject: | ||
parent: Thing | ||
properties: | ||
caption: ~ | ||
ProductModel: | ||
properties: | ||
isVariantOf: ~ | ||
Offer: | ||
parent: Thing | ||
properties: | ||
acceptedPaymentMethod: ~ | ||
availability: ~ | ||
availabilityStarts: ~ | ||
availabilityEnds: ~ | ||
availableDeliveryMethod: ~ | ||
category: { range: "Text" } | ||
deliveryLeadTime: ~ | ||
inventoryLevel: ~ | ||
itemCondition: ~ | ||
price: { range: Number } | ||
priceCurrency: ~ | ||
validFrom: ~ | ||
validThrough: ~ | ||
DeliveryChargeSpecification: { parent: false } | ||
PaymentChargeSpecification: { parent: false } | ||
OfferItemCondition: ~ | ||
PaymentMethod: ~ | ||
ItemAvailability: ~ | ||
DeliveryMethod: ~ | ||
QuantitativeValue: { parent: false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably extract some parts of these method to other ones to make the code a bit more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have idea, how do it. Can you help me?