Skip to content

Commit

Permalink
[FEATURE] Set different properties at once for a type
Browse files Browse the repository at this point in the history
A new method setProperties() was introduced to set multiple properties at once.

Relates: #12
  • Loading branch information
brotkrueml committed Jul 3, 2019
1 parent d2ef586 commit 5f73a74
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 41 deletions.
101 changes: 60 additions & 41 deletions Classes/Core/Model/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,76 +46,76 @@ public function setId(string $id): self
/**
* Check, if a property exists
*
* @param string $property The property name
* @param string $propertyName The property name
* @return bool
*/
public function hasProperty(string $property): bool
public function hasProperty(string $propertyName): bool
{
return \property_exists($this, $property);
return \property_exists($this, $propertyName);
}

/**
* Get the value of a property
*
* @param string $property The property name
* @param string $propertyName The property name
* @return string|array|AbstractType
*/
public function getProperty(string $property)
public function getProperty(string $propertyName)
{
if (!\property_exists($this, $property)) {
if (!\property_exists($this, $propertyName)) {
throw new \DomainException(
sprintf('Property "%s" is unknown for type "%s"', $property, $this->getType()),
sprintf('Property "%s" is unknown for type "%s"', $propertyName, $this->getType()),
1561829996
);
}

return $this->$property;
return $this->$propertyName;
}

/**
* Set the value of a property
*
* @param string $property The property name
* @param string|array|AbstractType $value The value of the property
* @param string $propertyName The property name
* @param string|array|AbstractType $propertyValue The value of the property
* @return AbstractType
*/
public function setProperty(string $property, $value): self
public function setProperty(string $propertyName, $propertyValue): self
{
$this->checkProperty($property, $value);
$this->checkProperty($propertyName, $propertyValue);

if (\is_int($value)) {
$value = (string)$value;
if (\is_int($propertyValue)) {
$propertyValue = (string)$propertyValue;
}

$this->$property = $value;
$this->$propertyName = $propertyValue;

return $this;
}

/**
* Check, if property name and value are valid
*
* @param string $property The property name
* @param string|array|AbstractType $value The property value
* @param string $propertyName The property name
* @param string|array|AbstractType $propertyValue The property value
*
* @throws \DomainException
* @throws \InvalidArgumentException
*/
protected function checkProperty(string $property, $value): void
protected function checkProperty(string $propertyName, $propertyValue): void
{
if (!\property_exists($this, $property)) {
if (!\property_exists($this, $propertyName)) {
throw new \DomainException(
sprintf('Property "%s" is unknown for type "%s"', $property, $this->getType()),
sprintf('Property "%s" is unknown for type "%s"', $propertyName, $this->getType()),
1561829996
);
}

if (!(\is_string($value) || \is_int($value) || \is_array($value) || $value instanceof AbstractType)) {
if (!(\is_string($propertyValue) || \is_int($propertyValue) || \is_array($propertyValue) || $propertyValue instanceof AbstractType)) {
throw new \InvalidArgumentException(
\sprintf(
'Value for property "%s" has not a valid data type (given: "%s"). Valid types are: string, int, array, instanceof AbstractType',
$property,
\is_object($value) ? \get_class($value) : \gettype($value)
$propertyName,
\is_object($propertyValue) ? \get_class($propertyValue) : \gettype($propertyValue)
),
1561830012
);
Expand All @@ -125,55 +125,74 @@ protected function checkProperty(string $property, $value): void
/**
* Adds a value to a property
*
* @param string $property The property name
* @param string|array|AbstractType $value The property value
* @param string $propertyName The property name
* @param string|array|AbstractType $propertyValue The property value
* @return AbstractType
*/
public function addProperty(string $property, $value): self
public function addProperty(string $propertyName, $propertyValue): self
{
$this->checkProperty($property, $value);
$this->checkProperty($propertyName, $propertyValue);

if (\is_null($this->$property)) {
$this->$property = $value;
if (\is_null($this->$propertyName)) {
$this->$propertyName = $propertyValue;

return $this;
}

if (\is_array($this->$property)) {
if (\is_string($value) || $value instanceof AbstractType) {
$value = [$value];
if (\is_array($this->$propertyName)) {
if (\is_string($propertyValue) || $propertyValue instanceof AbstractType) {
$propertyValue = [$propertyValue];
}

$this->$property = \array_merge($this->$property, $value);
$this->$propertyName = \array_merge($this->$propertyName, $propertyValue);

return $this;
}

$this->$property = [
$this->$property,
$value,
$this->$propertyName = [
$this->$propertyName,
$propertyValue,
];

return $this;
}

/**
* Set multiple properties at once
*
* The method expects the properties in the following format:
* key = property name
* value = property value
*
* @param array $properties
* @return AbstractType
*/
public function setProperties(array $properties): self
{
foreach ($properties as $propertyName => $propertyValue) {
$this->setProperty($propertyName, $propertyValue);
}

return $this;
}

/**
* Clear a property (set it to null)
*
* @param string $property The property name
* @param string $propertyName The property name
*
* @return AbstractType
*/
public function clearProperty(string $property): self
public function clearProperty(string $propertyName): self
{
if (!\property_exists($this, $property)) {
if (!\property_exists($this, $propertyName)) {
throw new \DomainException(
sprintf('Property "%s" is unknown for type "%s"', $property, $this->getType()),
sprintf('Property "%s" is unknown for type "%s"', $propertyName, $this->getType()),
1562177708
);
}

$this->$property = null;
$this->$propertyName = null;

return $this;
}
Expand Down
34 changes: 34 additions & 0 deletions Tests/Unit/Core/Model/AbstractTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,40 @@ public function addPropertyForPropertyWithArrayAlreadySet(): void
$this->assertSame(['some image value', 'other image value'], $actual);
}

/**
* @test
*/
public function setPropertiesReturnsReferenceToItself(): void
{
$actual = $this->concreteType->setProperties([]);

$this->assertInstanceOf(AbstractType::class, $actual);
}

/**
* @test
*/
public function setPropertiesSetsThePropertiesCorrectly(): void
{
$this->concreteType->setProperties([
'name' => 'some name',
'description' => 'some description',
'image' => ['some image value', 'other image value'],
]);

$actualName = $this->concreteType->getProperty('name');

$this->assertSame('some name', $actualName);

$actualDescription = $this->concreteType->getProperty('description');

$this->assertSame('some description', $actualDescription);

$actualImage = $this->concreteType->getProperty('image');

$this->assertSame(['some image value', 'other image value'], $actualImage);
}

/**
* @test
*/
Expand Down

0 comments on commit 5f73a74

Please sign in to comment.