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:
<?php
namespace Apptus\ESales\Connector;
use Apptus\Util\String\ListCodec;
use Apptus\Util\String\MapCodec;
class Subpanel {
const VALID_PUBLIC_PANEL_PATTERN = "#^/[_a-z](-?([_a-z0-9]+-?)*[_a-z0-9])?$#";
private $name;
private $publicPanel;
private $arguments;
public static function create($name, $publicPanel) {
return new Subpanel($name, $publicPanel, new ArgMap());
}
private function __construct($name, $publicPanel, $args) {
$this->name = $name;
if (!$this::validPublicPanel($publicPanel)) {
throw new \InvalidArgumentException($this::invalidPublicPanelMessage($publicPanel));
}
$this->publicPanel = $publicPanel;
$this->arguments = $args;
}
public function addArgument($name, $value) {
$this->arguments[$name] = $value;
return $this;
}
public function addArguments($args) {
$this->arguments->putAll($args);
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.";
}
}
function encode() {
$subpanelListCodec = new ListCodec(';');
$subpanelArgumentsMapCodec = new MapCodec();
$encodedFields = array();
array_push($encodedFields, $this->name);
array_push($encodedFields, $this->publicPanel);
array_push($encodedFields, $subpanelArgumentsMapCodec->encodeMap(ArgMap::validate($this->arguments)));
return $subpanelListCodec->encodeList($encodedFields);
}
static function decode($encodedSubpanel) {
$subpanelListCodec = new ListCodec(';');
$subpanelArgumentsMapCodec = new MapCodec();
$subpanelFields = $subpanelListCodec->decodeList($encodedSubpanel);
$name = $subpanelFields[0];
$publicPanel = $subpanelFields[1];
$arguments = $subpanelArgumentsMapCodec->decodeMap($subpanelFields[2]);
return new Subpanel($name, $publicPanel, $arguments);
}
function getName() {
return (string)$this->name;
}
}