Skip to content

Commit 23c1232

Browse files
committed
Integrate alias into $metadata
- added missing type PropertyPath - added Identifier to Record - added missing Type attribute to Record - added alias to Identifier – including dirty hack to derive it from given namespace - added addReference to Lodata Facade to allow adding additional vocabularies to the model - added alias for all auto registered References
1 parent 5ac8c55 commit 23c1232

File tree

9 files changed

+85
-1
lines changed

9 files changed

+85
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ vendor/
33
.phpunit.result.cache
44
.phpunit.cache/test-results
55
.phpdoc/
6+
.idea
67
.idea/dataSources.xml
78
.idea/**/tasks.xml
89
.idea/**/usage.statistics.xml

src/Annotation.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Flat3\Lodata\Type\Byte;
1414
use Flat3\Lodata\Type\Collection;
1515
use Flat3\Lodata\Type\Enum;
16+
use Flat3\Lodata\Type\PropertyPath;
1617
use Flat3\Lodata\Type\String_;
1718
use SimpleXMLElement;
1819

@@ -58,6 +59,10 @@ public function appendJsonValue($value)
5859
case $value instanceof Record:
5960
$record = (object) [];
6061

62+
if (method_exists($value, 'getTypeName') && $value->getTypeName()) {
63+
$record->{'@type'} = $value->getTypeName();
64+
}
65+
6166
/** @var PropertyValue $propertyValue */
6267
foreach ($value as $propertyValue) {
6368
$record->{$propertyValue->getProperty()->getName()} = $this->appendJsonValue($propertyValue->getPrimitive());
@@ -109,6 +114,10 @@ protected function appendXmlValue(SimpleXMLElement $element, $value)
109114
$element->addAttribute('Int', $value->toUrl());
110115
break;
111116

117+
case $value instanceof PropertyPath:
118+
$element->addAttribute('PropertyPath', $value->get());
119+
break;
120+
112121
case $value instanceof String_:
113122
$element->addAttribute('String', $value->get());
114123
break;
@@ -147,6 +156,10 @@ protected function appendXmlValue(SimpleXMLElement $element, $value)
147156
protected function appendXmlRecord(SimpleXMLElement $element, Record $record)
148157
{
149158
$recordElement = $element->addChild('Record');
159+
$identifier = $record->getIdentifier();
160+
if (!is_null($identifier)) {
161+
$recordElement->addAttribute('Type', $identifier->getQualifiedName());
162+
}
150163

151164
/** @var PropertyValue $propertyValue */
152165
foreach ($record as $propertyValue) {

src/Annotation/Capabilities/V1/Reference.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class Reference extends \Flat3\Lodata\Annotation\Reference
1212
{
1313
protected $uri = 'https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Capabilities.V1';
1414
protected $namespace = 'Org.OData.Capabilities.V1';
15+
protected $alias = 'Capabilities';
1516
}

src/Annotation/Core/V1/Reference.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class Reference extends \Flat3\Lodata\Annotation\Reference
1212
{
1313
protected $uri = 'https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1';
1414
protected $namespace = 'Org.OData.Core.V1';
15+
protected $alias = 'Core';
1516
}

src/Annotation/Record.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Flat3\Lodata\Annotation;
66

7+
use Flat3\Lodata\Helper\Identifier;
78
use Flat3\Lodata\Helper\ObjectArray;
89
use Flat3\Lodata\Interfaces\TypeInterface;
910
use Flat3\Lodata\Traits\HasComplexType;
@@ -16,4 +17,31 @@
1617
class Record extends ObjectArray implements TypeInterface
1718
{
1819
use HasComplexType;
20+
21+
/**
22+
* Resource identifier
23+
* @var Identifier $identifier
24+
*/
25+
protected $identifier;
26+
27+
/**
28+
* Get the identifier
29+
* @return Identifier Identifier
30+
*/
31+
public function getIdentifier(): ?Identifier
32+
{
33+
return $this->identifier;
34+
}
35+
36+
/**
37+
* Set the identifier
38+
* @param string|Identifier $identifier Identifier
39+
* @return $this
40+
*/
41+
public function setIdentifier($identifier): Record
42+
{
43+
$this->identifier = $identifier instanceof Identifier ? $identifier : new Identifier($identifier);
44+
45+
return $this;
46+
}
1947
}

src/Annotation/Reference.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ class Reference
3333
*/
3434
protected $alias;
3535

36+
public function getUri(): string
37+
{
38+
return $this->uri;
39+
}
40+
41+
public function getNamespace(): string
42+
{
43+
return $this->namespace;
44+
}
45+
46+
public function getAlias(): string
47+
{
48+
return is_null($this->alias) ? $this->namespace : $this->alias;
49+
}
50+
3651
/**
3752
* Append this reference to the provided XML element
3853
* @param SimpleXMLElement $schema Schema

src/Facades/Lodata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @method static ComplexType getComplexType(Identifier|string $name) Get a complex type from the model
4747
* @method static Singleton getSingleton(Identifier|string $name) Get a singleton from the model
4848
* @method static IdentifierInterface add(IdentifierInterface $item) Add a named resource or type to the model
49+
* @method static Model addReference(Reference $reference) Add a reference to an external CSDL document
4950
* @method static Model drop(Identifier|string $key) Drop a named resource or type from the model
5051
* @method static EntityContainer getEntityContainer() Get the entity container
5152
* @method static string getNamespace() Get the namespace of this model

src/Helper/Identifier.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ final class Identifier
2626
*/
2727
private $namespace;
2828

29+
/**
30+
* @var string $alias
31+
*/
32+
private $alias;
33+
2934
public function __construct(string $identifier)
3035
{
3136
if (!Str::contains($identifier, '.')) {
@@ -38,6 +43,14 @@ public function __construct(string $identifier)
3843

3944
$this->name = Str::afterLast($identifier, '.');
4045
$this->namespace = Str::beforeLast($identifier, '.');
46+
47+
// NB dirty hack to derive alias from namespace
48+
if (preg_match('/\.([^.]+)\.V1$/', $this->namespace, $matches)) {
49+
$this->alias = $matches[1];
50+
}
51+
else {
52+
$this->alias = $this->namespace;
53+
}
4154
}
4255

4356
/**
@@ -98,7 +111,7 @@ public function setNamespace(string $namespace): self
98111
*/
99112
public function getQualifiedName(): string
100113
{
101-
return $this->namespace.'.'.$this->name;
114+
return $this->alias.'.'.$this->name;
102115
}
103116

104117
/**

src/Type/PropertyPath.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Flat3\Lodata\Type;
4+
5+
use Flat3\Lodata\Type\String_;
6+
7+
class PropertyPath extends String_
8+
{
9+
// @PropertyPath in EDXML
10+
const identifier = 'Edm.PropertyPath';
11+
}

0 commit comments

Comments
 (0)