Skip to content

Commit

Permalink
Allow properties mapping in reflected names (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jul 13, 2021
1 parent d0126bd commit d99c0ad
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,19 @@ class Order implements ClassStructureContract
$properties->dateTime->format = Format::DATE_TIME;
$properties->price = Schema::number();

$ownerSchema->required[] = self::names()->id;
$ownerSchema->setFromRef('#/definitions/order');

// Define default mapping if any
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);

// Use mapped name references after the default mapping was configured.
$names = self::names($ownerSchema->properties);
$ownerSchema->required = array(
$names->id,
$names->dateTime,
$names->price
);

// Define additional mapping
$ownerSchema->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
$ownerSchema->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
Expand Down
15 changes: 15 additions & 0 deletions src/NameMirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@

class NameMirror
{
/**
* NameMirror constructor.
* @param null|string[] $mapping a map of propertyName to dataName
*/
public function __construct($mapping = null)
{
$this->mapping = $mapping;
}

private $mapping;

public function __get($name)
{
if ($this->mapping !== null && isset($this->mapping[$name])) {
return $this->mapping[$name];
}

return $name;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Structure/ClassStructureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ public function jsonSerialize()
/**
* @return static|NameMirror
*/
public static function names()
public static function names(Properties $properties = null, $mapping = Schema::DEFAULT_MAPPING)
{
if ($properties !== null) {
return new NameMirror($properties->getDataKeyMap($mapping));
}

static $nameflector = null;
if (null === $nameflector) {
$nameflector = new NameMirror();
Expand Down
13 changes: 8 additions & 5 deletions tests/src/Helper/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ public static function setUpProperties($properties, Schema $ownerSchema)
$properties->dateTime->format = Format::DATE_TIME;
$properties->price = Schema::number();

$ownerSchema->required = array(
self::names()->id,
'date_time',
self::names()->price
);
$ownerSchema->setFromRef('#/definitions/order');

// Define default mapping if any
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);

// Use mapped name references after the default mapping was configured.
$names = self::names($ownerSchema->properties);
$ownerSchema->required = array(
$names->id,
$names->dateTime,
$names->price
);

// Define additional mapping
$ownerSchema->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
$ownerSchema->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
Expand Down

0 comments on commit d99c0ad

Please sign in to comment.