Skip to content

Commit

Permalink
Added common interface for definitions which hold a list of properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
joachim-n committed Sep 17, 2024
1 parent f39b3ce commit a066eb1
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 22 deletions.
38 changes: 18 additions & 20 deletions Definition/DataDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Defines a data property.
*/
class DataDefinition {
class DataDefinition implements PropertyListInterface {

/**
* The type of data the property holds.
Expand Down Expand Up @@ -263,6 +263,9 @@ public function getVariantProperties(string $type) {
return $this->variants[$type]->getProperties();
}

/**
* {@inheritdoc}
*/
public function setProperties(array $properties): self {
// TODO! this won't catch child classes of SimpleData!!!
// TODO we don't have access to the factory here. So we can't ask the
Expand Down Expand Up @@ -297,25 +300,22 @@ public function setProperties(array $properties): self {
return $this;
}

/**
* {@inheritdoc}
*/
public function getProperties() {
return $this->properties;
}

/**
* {@inheritdoc}
*/
public function getPropertyNames() {
return array_keys($this->properties);
}

/**
* Gets a child property definition.
*
* @param string $name
* The property name.
*
* @return static
* The definition.
*
* @throws \Exception
* Throws an exception if the property doesn't exit.
* {@inheritdoc}
*/
public function getProperty(string $name): self {
if (!isset($this->properties[$name])) {
Expand Down Expand Up @@ -378,6 +378,9 @@ public function getNestedProperty($address): self {
return $property_definition;
}

/**
* {@inheritdoc}
*/
public function addProperty(self $property): self {
// TODO! this won't catch child classes of SimpleData!!!
if ($this->type == 'string' || $this->type == 'boolean') {
Expand All @@ -403,15 +406,7 @@ public function addProperty(self $property): self {
}

/**
* Adds properties to this definition.
*
* @param array $properties
* An array of data definitions. These are appended to existing properties.
* If any keys in this array correspond to existing properties, the existing
* definition is overwritten. The replacement property will be in the order
* given in the $properties array, not in its original position.
*
* @return static
* {@inheritdoc}
*/
public function addProperties(array $properties): self {
if ($this->type == 'string') {
Expand Down Expand Up @@ -441,6 +436,9 @@ public function addProperties(array $properties): self {
return $this;
}

/**
* {@inheritdoc}
*/
public function removeProperty(string $name) {
unset($this->properties[$name]);

Expand Down
91 changes: 91 additions & 0 deletions Definition/PropertyListInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace MutableTypedData\Definition;

/**
* Interface for definitions which hold lists of properties.
*/
interface PropertyListInterface {

/**
* Sets the properties for this definition.
*
* Previously-defined properties are removed.
*
* @param array $properties
* An array of data definitions, keyed by the property name.
*
* @return self
*/
public function setProperties(array $properties): self;

/**
* Adds a property to this definition.
*
* @param DataDefinition $property
* The property to add. It must have its name set already.
*
* @return self
*/
public function addProperty(DataDefinition $property): self;

/**
* Adds properties to this definition.
*
* @param array $properties
* An array of data definitions. These are appended to existing properties.
* If any keys in this array correspond to existing properties, the existing
* definition is overwritten. The replacement property will be in the order
* given in the $properties array, not in its original position.
*
* @return self
*/
public function addProperties(array $properties): self;

/**
* Removes a property.
*
* @param string $name
* The name of the property to remove.
*
* @return self
*
* TODO: Add return type in 2.0.0
*/
public function removeProperty(string $name);

/**
* Gets a child property definition.
*
* @param string $name
* The property name.
*
* @return \MutableTypedData\Definition\DataDefinition
* The definition.
*
* @throws \Exception
* Throws an exception if the property doesn't exist.
*/
public function getProperty(string $name): DataDefinition;

/**
* Gets this definition's properties.
*
* @return \MutableTypedData\Definition\DataDefinition[]
* An array of property definitions, keyed by the property name.
*
* TODO: Add return type in 2.0.0
*/
public function getProperties();

/**
* Gets the names of this definition's properties.
*
* @return string[]
* An array of property names.
*
* TODO: Add return type in 2.0.0
*/
public function getPropertyNames();

}
50 changes: 48 additions & 2 deletions Definition/VariantDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* When a mutable data item's variant controlling property is set, the variant's
* properties are added to the mutable data.
*/
class VariantDefinition {
class VariantDefinition implements PropertyListInterface {

protected $label;

Expand All @@ -35,7 +35,10 @@ public function getLabel() {
return $this->label;
}

public function setProperties(array $properties) {
/**
* {@inheritdoc}
*/
public function setProperties(array $properties): self {
$this->properties = $properties;

// Fill in machine names as a convenience.
Expand All @@ -46,6 +49,16 @@ public function setProperties(array $properties) {
return $this;
}

/**
* {@inheritdoc}
*/
public function addProperty(DataDefinition $property): self {
$this->properties[$property->getName()] = $property;
}

/**
* {@inheritdoc}
*/
public function addProperties(array $properties): self {
foreach ($properties as $name => $property) {
// Fill in machine names as a convenience.
Expand All @@ -57,8 +70,41 @@ public function addProperties(array $properties): self {
return $this;
}

/**
* {@inheritdoc}
*/
public function getProperties() {
return $this->properties;
}

/**
* {@inheritdoc}
*/
public function getProperty(string $name): DataDefinition {
if (!isset($this->properties[$name])) {
throw new \Exception(sprintf("Property definition '%s' has no child property '$name' defined.",
$this->name,
$name
));
}

return $this->properties[$name];
}

/**
* {@inheritdoc}
*/
public function getPropertyNames() {
return array_keys($this->properties);
}

/**
* {@inheritdoc}
*/
public function removeProperty(string $name) {
unset($this->properties[$name]);

return $this;
}

}

0 comments on commit a066eb1

Please sign in to comment.