1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126:
<?php
namespace Apptus\ESales\Connector;
/**
* Represents a single element in a result having the products format.
*/
class Product implements \IteratorAggregate {
private $ticket;
private $attributes;
private $variants;
private $categoryReferenceAttributes;
/**
* @internal
* @param string|null $ticket
* @param array $attributes
* @param array $variants
* @param array $categoryReferenceAttributes
*/
public function __construct($ticket = null, array $attributes = array (), array $variants = array (),
array $categoryReferenceAttributes = array ()) {
$this->ticket = $ticket;
$this->attributes = $attributes;
$this->variants = $variants;
$this->categoryReferenceAttributes = $categoryReferenceAttributes;
}
/**
* Get the attribute with the specified name or <b>null</b> if no such attribute exists for this product.
*
* @param name
* The name of the attribute to get.
* @return Attribute|null
* The attribute with the specified name or <b>null</b>.
*/
public function getAttribute($name) {
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
/**
* Return the key of this product.
*
* @return string
* The key of this product.
*/
public function key() {
return $this->getValue('product_key');
}
/**
* Get the value of the attribute with the specified $attributeName. The specified $defaultValue
* will be returned if no such attribute exists (defaults to null).
*
* @param string
* The name of the attribute whose value to retrieve.
* @param mixed
* The value to return if this product does not have an attribute with the specified $attributeName, defaults to null.
* @return string|mixed
* The attribute value of attribute with the specified $attributeName, or the supplied $defaultValue
* if this product has no attribute matching the name.
*/
public function getValue($attributeName, $defaultValue = null) {
$a = $this->getAttribute($attributeName);
return ($a === null) ? $defaultValue : $a->getValue();
}
public function getIterator() {
return new \ArrayIterator($this->attributes);
}
/**
* Get the notification ticket of this product.
*
* @return string
* The ticket.
*/
public function getTicket() {
return $this->ticket;
}
/**
* Get the available category reference attributes in the product.
*
* @return array the available category reference attributes in the product as an array
*/
public function getCategoryReferenceAttributes()
{
return array_keys($this->categoryReferenceAttributes);
}
/**
* Get the Category objects of the given category reference attribute.
*
* @param string $categoryReferenceAttribute the category reference attribute to get the categories for
* @return array the Category objects of the categoryReferenceAttribute as an array
*/
public function getCategories($categoryReferenceAttribute)
{
if (isset($this->categoryReferenceAttributes[$categoryReferenceAttribute])){
return $this->categoryReferenceAttributes[$categoryReferenceAttribute];
} else {
return array();
}
}
/**
* Get the an array with variants of this product.
*
* @return Variant[]
* The variants as an indexed array of {@see Variant} objects.
*/
public function getVariants() {
return $this->variants;
}
public function __toString() {
if (isset($this->attributes['product_key'])) {
return (string) $this->attributes['product_key'];
}
return (string) $this->attributes;
}
}