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:
<?php
namespace Apptus\ESales\Connector;
use Apptus\Util\String\ListCodec;
use Apptus\Util\String\MapCodec;
/**
* A subpanel refers to a public panel, and can be provided to a dynamic page in order to
* retrieve content from several panels at once without having to predefine a zone.
*
* Each subpanel must have a unique name within a dynamic page.
*/
class Subpanel
{
/**
* @internal
*/
const VALID_PUBLIC_PANEL_PATTERN = "#^/[_a-z](-?([_a-z0-9]+-?)*[_a-z0-9])?$#";
private $name;
private $publicPanel;
/** @var ArgMap */
private $arguments;
private $attributes;
/**
* Creates a subpanel with a reference to a public panel. The subpanel can be supplied to a dynamic page.
*
* @param string name
* The name of this subpanel, defining the path shown in the result.
* This name must be unique for each dynamic page it is provided to.
* @param string publicPanel
* The path of the public panel.
*
* @return Subpanel
* an object representing a subpanel, which can be supplied to a dynamic page
*/
public static function create($name, $publicPanel)
{
return new Subpanel($name, $publicPanel, new ArgMap(), array());
}
private function __construct($name, $publicPanel, $args, $attributes)
{
$this->name = $name;
if (!$this::validPublicPanel($publicPanel)) {
throw new \InvalidArgumentException($this::invalidPublicPanelMessage($publicPanel));
}
$this->publicPanel = $publicPanel;
$this->arguments = $args;
$this->attributes = $attributes;
}
/**
* Adds an argument to the map of arguments for this subpanel,
* where key is the parameter name, and value is the argument value.
*
* @param string name
* The parameter name.
* @param string value
* The argument value.
* @return Subpanel
* the subpanel with the added argument
*/
public function addArgument($name, $value)
{
$this->arguments[$name] = $value;
return $this;
}
/**
* Appends a map of arguments to the arguments of this subpanel. Any prior arguments with the same name
* will be overriden.
*
* These arguments will have precedence in any name collisions with the arguments provided
* to a dynamic page with this subpanel.
*
* @param array|ArgMap args
* A map of arguments to the subpanel, where key is the parameter name, and value is the argument value.
*
* @return Subpanel
* the subpanel with the added arguments
*/
public function addArguments($args)
{
$this->arguments->putAll($args);
return $this;
}
/**
* Adds a local panel attribute to this subpanel. These attributes can be retrieved from the corresponding {@see PanelContent}
* object in the response.
*
* @param string name
* The attribute name.
* @param string value
* The attribute value.
* @return Subpanel The subpanel 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 subpanel. Any prior attributes with
* the same name will be overridden. These attributes can be retrieved from the corresponding {@see PanelContent} object
* in the response.
*
* @param array attributes
* An associative array of local attributes for the subpanel, where key is the attribute name, and value is the
* attribute value.
* @return Subpanel The subpanel 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;
}
private static function validPublicPanel($publicPanel)
{
return preg_match(Subpanel::VALID_PUBLIC_PANEL_PATTERN, $publicPanel) === 1;
}
private static function invalidPublicPanelMessage($publicPanel)
{
if ($publicPanel[0] != '/') {
return "Public panel path must start with /.";
} else if (Path::length($publicPanel) > 2) {
return "Subpanels in a dynamic page may only refer to public root panels.";
} else {
return "Illegal public panel path.";
}
}
/**
* @return string
* @internal
*/
function encode()
{
$subpanelListCodec = new ListCodec(';');
$mapCodec = new MapCodec();
$encodedFields = array();
// The fields must be inserted in the list in exactly this order
array_push($encodedFields, $this->name);
array_push($encodedFields, $this->publicPanel);
array_push($encodedFields, $mapCodec->encodeMap(ArgMap::validate($this->arguments)));
if (count($this->attributes) > 0) {
array_push($encodedFields, $mapCodec->encodeMap($this->attributes));
}
return $subpanelListCodec->encodeList($encodedFields);
}
/**
* @return string
* @internal
*/
function getName()
{
return (string)$this->name;
}
}