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: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199:
<?php
namespace Apptus\ESales\Connector;
use Apptus\Util\String\ListCodec;
use Apptus\Util\String\MapCodec;
use Apptus\Util\String\Slugifier;
/**
* A dynamic page consists of a page name and a number of sub panels. Each sub panel must point to a public panel,
* already defined in the eSales panel hierarchy.
*
* Use {@see retrieveContent()} to get the content of this page and all of its sub panels, recursively.
*
* The request will be equivalent to requesting a predefined zone with the same name, and the same sub panels,
* as the dynamic page.
*/
class DynamicPage
{
/**
* @internal
*/
const DYNAMIC_PAGE_ROOT = "/dynamic-pages";
private $cluster;
private $session;
private $displayName;
private $path;
private $subpanels = array();
private $subpanelNames = array();
private $attributes = array();
/**
* @param Cluster
* @param Session
* @param string
* @internal
*/
public function __construct(Cluster $cluster, Session $session, $name)
{
$this->displayName = $name;
$this->path = Slugifier::slugify($name);
$this->session = $session;
$this->cluster = $cluster;
}
/**
* Adds a subpanel to this dynamic page. Each subpanel must have a unique name.
*
* @param Subpanel
* The subpanel to add
* @return DynamicPage
* The dynamic page with the added subpanel
* @throws DuplicateSubpanelException
* if the dynamic page already has a subpanel with the same name as the provided
*
*/
public function addSubpanel(Subpanel $subpanel)
{
if (in_array($subpanel->getName(), $this->subpanelNames)) {
throw new DuplicateSubpanelException("Each subpanel must have a unique name.");
}
array_push($this->subpanels, $subpanel);
array_push($this->subpanelNames, $subpanel->getName());
return $this;
}
/**
* Adds a local panel attribute to this dynamic page. These attributes can be retrieved from the {@see PanelContent} response.
*
* @param string name
* The attribute name.
* @param string value
* The attribute value.
* @return DynamicPage The dynamic page with the added local attribute
*/
public function addAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
/**
* Appends an associative array of attributes to the local panel attributes of this dynamic page. Any prior attributes with
* the same name will be overridden. These attributes can be retrieved from the {@see PanelContent} response.
*
* @param array attributes
* An associative array of local attributes for the dynamic page, where key is the attribute name, and value is the
* attribute value.
* @return DynamicPage The dynamic page with the added local attributes
*/
public function addAttributes($attributes)
{
if (!is_array($attributes)) {
throw new \InvalidArgumentException('attributes must be an array');
}
foreach ($attributes as $key => $value) {
$this->attributes[$key] = $value;
}
return $this;
}
/**
* Retrieves the content of this dynamic page from the eSales service.
*
* The arguments supplied to this method may be overriden by the arguments sent to the subpanels.
*
* You may use an {@see ArgMap} or an associative array to pass the arguments.
*
* @param ArgMap|array|null
* A map of arguments, where key is the parameter name, and value is the argument value.
* Arguments having null values are ignored.
* @return PanelContent
* A PanelContent object holding the content of the dynamic page
* @throws IOException
* if there is an error communicating with the eSales service
* @throws \InvalidArgumentException
* if arguments contains null, empty or reserved keys
* @throws MissingSubpanelException
* if the dynamic page does not have any subpanels
*/
public function retrieveContent($dynamicPageArguments = null)
{
$xml = $this->retrieveContentAsXml($dynamicPageArguments);
$parser = new PanelParser(Path::childPath(self::DYNAMIC_PAGE_ROOT, $this->path));
$content = $parser->parse($xml);
return $content;
}
/**
* Retrieves the content of this dynamic page from the eSales service as a string.
*
* The arguments supplied to this method may be overriden by the arguments sent to the subpanels,
* or by the arguments already defined in the public panels referred to by the subpanels.
*
* You may use an {@see ArgMap} or an associative array to pass the arguments.
*
* @param array|ArgMap dynamicPageArguments
* A map of arguments, where key is the parameter name, and value is the argument value.
* Arguments having null values are ignored.
* @return string
* A string containing the xml content of the dynamic page.
* @throws IOException
* if there is an error communicating with the eSales service
* @throws \InvalidArgumentException
* if arguments contains null, empty or reserved keys
* @throws MissingSubpanelException
* if the dynamic page does not have any subpanels
*/
public function retrieveContentAsXml($dynamicPageArguments = null)
{
if (empty($this->subpanels)) {
throw new MissingSubpanelException("The dynamic page does not have any subpanels.");
}
if ($dynamicPageArguments !== null && isset($dynamicPageArguments['session_key'])) {
throw new \InvalidArgumentException('Argument session_key is reserved and must not be specified.');
}
$args = new ArgMap();
$args->put('session_key', $this->session->key());
$args->putAll($dynamicPageArguments);
$encodedBody = $this->encodeBody($this->subpanels, $this->displayName, $this->attributes);
$headers = new ArgMap();
if ($this->session->market() !== null) {
$headers->put(session::MARKET_HEADER_NAME, $this->session->market());
}
if ($this->session->customerKey() !== null) {
$headers->put(session::CUSTOMER_KEY_HEADER_NAME, $this->session->customerKey());
}
return $this->cluster->queryDynamicPage($this->path, $args, $headers, $encodedBody);
}
/**
* @param $subpanels
* @param $dynamicPageName
* @param array $attributes
* @return string
* @internal
*/
static function encodeBody($subpanels, $dynamicPageName, array $attributes)
{
$encodedBody = array();
array_push($encodedBody, $dynamicPageName);
array_push($encodedBody, count($subpanels));
foreach ($subpanels as $subpanel) {
/** @var $subpanel Subpanel */
array_push($encodedBody, $subpanel->encode());
}
if (count($attributes) > 0) {
$mapCodec = new MapCodec();
array_push($encodedBody, $mapCodec->encodeMap($attributes));
}
$bodyListCodec = new ListCodec("\n");
return $bodyListCodec->encodeList($encodedBody);
}
}