Skip to content

Commit 59eaadd

Browse files
committed
Updated composer.json. Enhanced doc.
1 parent fe59dc4 commit 59eaadd

11 files changed

+1043
-59
lines changed

README.md

Lines changed: 983 additions & 30 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "dunglas/php-schema.org-model",
2+
"name": "dunglas/php-schema",
33
"type": "library",
44
"description": "Various tools to generate a data model based on Schema.org vocables",
5-
"keywords": ["schema.org", "model", "doctrine", "entity"],
5+
"keywords": ["schema.org", "semantic", "model", "doctrine", "symfony", "entity", "enum"],
66
"homepage": "http://dunglas.fr",
77
"license": "MIT",
88
"authors": [
@@ -29,9 +29,10 @@
2929
"fabpot/php-cs-fixer": "~1.0"
3030
},
3131
"suggest": {
32-
"doctrine/orm": "For Doctrine annotations and collections",
33-
"symfony/validator": "For constraint annotations",
34-
"myclabs/php-enum": "For PHP Enum support"
32+
"myclabs/php-enum": "For enumerations",
33+
"doctrine/collections": "For Doctrine collections",
34+
"doctrine/orm": "For Doctrine annotations",
35+
"symfony/validator": "For constraint annotations"
3536
},
3637
"bin": ["bin/schema"]
3738
}

src/SchemaOrgModel/AnnotationGenerator/ConstraintAnnotationGenerator.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public function generateFieldAnnotations($className, $fieldName)
6565
$asserts[] = '@Assert\NotNull';
6666
}
6767

68+
if ($field['isEnum']) {
69+
$assert = sprintf('@Assert\Choice(callback={"%s", "toArray"}', $field['range']);
70+
71+
if ($field['isArray']) {
72+
$assert .= ', multiple=true';
73+
}
74+
75+
$assert .= ')';
76+
77+
$asserts[] = $assert;
78+
}
79+
6880
return $asserts;
6981
}
7082

@@ -73,6 +85,25 @@ public function generateFieldAnnotations($className, $fieldName)
7385
*/
7486
public function generateUses($className)
7587
{
76-
return $this->classes[$className]['isEnum'] ? [] : ['Symfony\Component\Validator\Constraints as Assert'];
88+
if ($this->classes[$className]['isEnum']) {
89+
return [];
90+
}
91+
92+
$uses = [];
93+
$uses[] = 'Symfony\Component\Validator\Constraints as Assert';
94+
95+
foreach ($this->classes[$className]['fields'] as $field) {
96+
if ($field['isEnum']) {
97+
$enumClass = $this->classes[$field['range']];
98+
$enumNamespace = isset($enumClass['namespaces']['class']) && $enumClass['namespaces']['class'] ? $enumClass['namespaces']['class'] : $this->config['namespaces']['enum'];
99+
$use = sprintf('%s\%s', $enumNamespace, $field['range']);
100+
101+
if (!in_array($use, $uses)) {
102+
$uses[] = $use;
103+
}
104+
}
105+
}
106+
107+
return $uses;
77108
}
78109
}

src/SchemaOrgModel/CardinalitiesExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private function extractForProperty(\EasyRdf_Resource $property)
9494
||
9595
preg_match('/^The /', $comment)
9696
) {
97-
return self::CARDINALITY_1_1;
97+
return self::CARDINALITY_0_1;
9898
}
9999

100100
return self::CARDINALITY_UNKNOWN;

src/SchemaOrgModel/Command/DumpConfigurationCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DumpConfigurationCommand extends Command
2828
protected function configure()
2929
{
3030
$this
31-
->setName('schema:dump-configuration')
31+
->setName('dump-configuration')
3232
->setDescription('Dump configuration')
3333
;
3434
}

src/SchemaOrgModel/Command/ExtractCardinalitiesCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ExtractCardinalitiesCommand extends Command
2929
protected function configure()
3030
{
3131
$this
32-
->setName('schema:extract-cardinalities')
32+
->setName('extract-cardinalities')
3333
->setDescription('Extract properties\' cardinality')
3434
;
3535
}

src/SchemaOrgModel/Command/GenerateTypesCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class GenerateTypesCommand extends Command
3434
protected function configure()
3535
{
3636
$this
37-
->setName('schema:generate-types')
37+
->setName('generate-types')
3838
->setDescription('Generate types')
3939
->addArgument('output', InputArgument::REQUIRED, 'The output directory')
4040
->addArgument('config', InputArgument::OPTIONAL, 'The config file to use')

src/SchemaOrgModel/TypesGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function generate($config)
150150
$class['name'] = $type->localName();
151151
$class['label'] = $type->get('rdfs:comment')->getValue();
152152
$class['resource'] = $type;
153+
$class['config'] = $typeConfig;
153154

154155
$class['isEnum'] = $this->isEnum($type);
155156
if ($class['isEnum']) {

src/SchemaOrgModel/TypesGeneratorConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class TypesGeneratorConfiguration implements ConfigurationInterface
2121
{
22-
const SCHEMA_ORG_RDFA_URL = 'https://raw.githubusercontent.com/rvguha/schemaorg/master/data/schema.rdfa';
22+
const SCHEMA_ORG_RDFA_URL = 'http://schema.org/docs/schema_org_rdfa.html';
2323
const GOOD_RELATIONS_OWL_URL = 'http://purl.org/goodrelations/v1.owl';
2424

2525
/**

tests/config/address-book.yml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1+
# The PHP namespace of generated entities
12
namespaces:
23
entity: "AddressBook\Entity"
4+
# The list of types and properties we want to use
35
types:
6+
# Parent class of Person
47
Thing:
58
properties:
69
name: ~
7-
url: ~
810
Person:
911
properties:
12+
familyName: ~
13+
givenName: ~
1014
additionalName: ~
15+
gender: ~
1116
address: ~
1217
birthDate: ~
13-
email: ~
14-
familyName: ~
15-
faxNumber: ~
16-
gender: ~
17-
givenName: ~
18-
honorificPrefix: ~
19-
honorificSuffix: ~
20-
jobTitle: ~
2118
telephone: ~
19+
email: ~
2220
url: ~
23-
worksFor: ~
21+
jobTitle: ~
2422
PostalAddress:
23+
# Disable the generation of the class hierarchy for this type
2524
parent: false
2625
properties:
27-
addressCountry: { range: Text }
26+
# Force the type of the addressCountry property to text
27+
addressCountry: { range: "Text" }
2828
addressLocality: ~
2929
addressRegion: ~
3030
postOfficeBoxNumber: ~
3131
postalCode: ~
3232
streetAddress: ~
33-
Organization:
34-
properties: {}

0 commit comments

Comments
 (0)