You are here

final class PreparedAttribute in Commerce Core 8.2

Represents a prepared attribute.

Hierarchy

Expanded class hierarchy of PreparedAttribute

See also

\Drupal\commerce_product\ProductVariationAttributeMapperInterface::prepareAttributes()

File

modules/product/src/PreparedAttribute.php, line 10

Namespace

Drupal\commerce_product
View source
final class PreparedAttribute {

  /**
   * The ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The label.
   *
   * @var string
   */
  protected $label;

  /**
   * The element type.
   *
   * @var string
   */
  protected $elementType;

  /**
   * Whether the attribute is required.
   *
   * @var bool
   */
  protected $required;

  /**
   * The attribute values.
   *
   * @var string[]
   */
  protected $values;

  /**
   * Constructs a new PreparedAttribute instance.
   *
   * @param array $definition
   *   The definition.
   */
  public function __construct(array $definition) {
    foreach ([
      'id',
      'label',
      'element_type',
      'values',
    ] as $required_property) {
      if (empty($definition[$required_property])) {
        throw new \InvalidArgumentException(sprintf('Missing required property "%s".', $required_property));
      }
    }
    if (!is_array($definition['values'])) {
      throw new \InvalidArgumentException(sprintf('The property "values" must be an array.'));
    }
    $this->id = $definition['id'];
    $this->label = $definition['label'];
    $this->elementType = $definition['element_type'];
    $this->required = isset($definition['required']) ? $definition['required'] : TRUE;
    $this->values = $definition['values'];
  }

  /**
   * Gets the ID.
   *
   * @return string
   *   The ID.
   */
  public function getId() : string {
    return $this->id;
  }

  /**
   * Gets the label.
   *
   * @return string
   *   The label.
   */
  public function getLabel() : string {
    return $this->label;
  }

  /**
   * Gets the element type.
   *
   * @return string
   *   The element type.
   */
  public function getElementType() : string {
    return $this->elementType;
  }

  /**
   * Gets whether the attribute is required.
   *
   * @return bool
   *   TRUE if the attribute is required, FALSE otherwise.
   */
  public function isRequired() : bool {
    return $this->required;
  }

  /**
   * Gets the attribute values.
   *
   * @return string[]
   *   The attribute values.
   */
  public function getValues() : array {
    return $this->values;
  }

  /**
   * Gets the array representation of the prepared attribute.
   *
   * @return array
   *   The array representation of the prepared attribute.
   */
  public function toArray() : array {
    return [
      'id' => $this->id,
      'label' => $this->label,
      'element_type' => $this->elementType,
      'required' => $this->required,
      'values' => $this->values,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreparedAttribute::$elementType protected property The element type.
PreparedAttribute::$id protected property The ID.
PreparedAttribute::$label protected property The label.
PreparedAttribute::$required protected property Whether the attribute is required.
PreparedAttribute::$values protected property The attribute values.
PreparedAttribute::getElementType public function Gets the element type.
PreparedAttribute::getId public function Gets the ID.
PreparedAttribute::getLabel public function Gets the label.
PreparedAttribute::getValues public function Gets the attribute values.
PreparedAttribute::isRequired public function Gets whether the attribute is required.
PreparedAttribute::toArray public function Gets the array representation of the prepared attribute.
PreparedAttribute::__construct public function Constructs a new PreparedAttribute instance.