final class PreparedAttribute in Commerce Core 8.2
Represents a prepared attribute.
Hierarchy
- class \Drupal\commerce_product\PreparedAttribute
Expanded class hierarchy of PreparedAttribute
See also
\Drupal\commerce_product\ProductVariationAttributeMapperInterface::prepareAttributes()
File
- modules/
product/ src/ PreparedAttribute.php, line 10
Namespace
Drupal\commerce_productView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PreparedAttribute:: |
protected | property | The element type. | |
PreparedAttribute:: |
protected | property | The ID. | |
PreparedAttribute:: |
protected | property | The label. | |
PreparedAttribute:: |
protected | property | Whether the attribute is required. | |
PreparedAttribute:: |
protected | property | The attribute values. | |
PreparedAttribute:: |
public | function | Gets the element type. | |
PreparedAttribute:: |
public | function | Gets the ID. | |
PreparedAttribute:: |
public | function | Gets the label. | |
PreparedAttribute:: |
public | function | Gets the attribute values. | |
PreparedAttribute:: |
public | function | Gets whether the attribute is required. | |
PreparedAttribute:: |
public | function | Gets the array representation of the prepared attribute. | |
PreparedAttribute:: |
public | function | Constructs a new PreparedAttribute instance. |