You are here

public function FieldableEdgeEntityBase::setPropertyValue in Apigee Edge 8

Updates the property value on an entity by field name.

Parameters

string $field_name: Name of the field (which usually the name of the property.)

mixed $value: Value of the field.

Overrides FieldableEdgeEntityInterface::setPropertyValue

2 calls to FieldableEdgeEntityBase::setPropertyValue()
AttributesAwareFieldableEdgeEntityBase::setPropertyValue in src/Entity/AttributesAwareFieldableEdgeEntityBase.php
Updates the property value on an entity by field name.
FieldableEdgeEntityBase::onChange in src/Entity/FieldableEdgeEntityBase.php
Reacts to changes to a field.
1 method overrides FieldableEdgeEntityBase::setPropertyValue()
AttributesAwareFieldableEdgeEntityBase::setPropertyValue in src/Entity/AttributesAwareFieldableEdgeEntityBase.php
Updates the property value on an entity by field name.

File

src/Entity/FieldableEdgeEntityBase.php, line 466

Class

FieldableEdgeEntityBase
Base field support for Apigee Entities without making them content entities.

Namespace

Drupal\apigee_edge\Entity

Code

public function setPropertyValue(string $field_name, $value) : void {

  // Ignore NULL values, because those are not supported by setters of
  // the SDK entities.
  if ($value === NULL) {
    return;
  }

  // We try to call the setter on the current object first,
  // because it can take care of extra things not just updating the values
  // on the decorated SDK entity.
  $setter = 'set' . ucfirst($field_name);
  $destination = NULL;
  if (method_exists($this, $setter)) {
    $destination = $this;
  }
  elseif (method_exists($this->decorated, $setter)) {
    $destination = $this->decorated;
  }
  if ($destination) {
    try {
      $destination
        ->{$setter}($value);
    } catch (\TypeError $error) {

      // Auto-retry, pass the value as variable-length arguments.
      // Ignore empty variable list.
      if (is_array($value)) {

        // Clear the value of the property.
        if (empty($value)) {
          $destination
            ->{$setter}();
        }
        else {
          $destination
            ->{$setter}(...$value);
        }
      }
      else {
        throw $error;
      }
    }
  }
  else {
    throw new InvalidArgumentException("Property with %s name not found.");
  }
}