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:
<?php
namespace Apptus\ESales\Connector;
/**
* Class for creating a facets argument, used for check-box navigation. It also has support
* for parsing and toggling facet values, so it can be used to keep track of
* selection changes.
*
* <p>
* Example:
* </p>
*
* <code>
* $f = (new Facets('color', 'yellow', 'blue'))->add('brand', 'nike');
* // ((string) $f) is: brand:nike,color:blue|yellow
* $panelArguments = new ArgMap();
* $panelArguments['facets'] = $f;
* // ... other arguments...
* </code>
*
* If it is convenient for your scenario you can serialize the current selected values by converting it to string
* and store them in the browser. When a check-box is clicked, deserialize it afterwards like this:
*
* <code>
* $f = Facets::parse($facetsString);
* </code>
*
* Then update it with the last click and send a new request:
*
* <code>
* $f = $f->toggle($clickedAttribute, $clickedValue);
* $panelArguments['facets'] = $f;
* // ... add other arguments and send request...
* </code>
*
* Note that Facets are immutable and all methods return new Facets objects.
*/
class Facets {
private $facetCollection;
/**
* Create a new empty Facets, or if provided, with the given attribute and values.
*
* @param attribute string the attribute name
* @param values array|string an indexed array of values, or one or more strings.
* @throws \InvalidArgumentException if attribute or values are null or the empty string.
*/
public function __construct($attribute = null, $values = null) {
$this->facetCollection = new FacetCollection();
if (func_num_args() === 0) {
return;
}
if ($attribute === null || gettype($attribute) !== 'string' || strlen($attribute) === 0) {
throw new \InvalidArgumentException('Attribute name must be at least one character.');
}
if ($values == null) {
throw new \InvalidArgumentException('The $values parameter cannot be null.');
}
if (gettype($values) === 'array') {
$this->facetCollection->add($attribute, $values);
} else {
$this->facetCollection->add($attribute, array_slice(func_get_args(), 1));
}
}
/**
* Return a new Facets with the given attribute and values added.
*
* @param attribute string the attribute name
* @param values array|string an indexed array of values, or one or more strings.
* @return Facets a new Facets object
* @throws \InvalidArgumentException if attribute is null or the empty string.
*/
public function add($attribute, $values) {
if (gettype($values) === 'array') {
$v = $values;
} else {
$v = array_slice(func_get_args(), 1);
}
$f = new Facets();
$f->facetCollection = $this->facetCollection->deepCopy();
$f->facetCollection->add($attribute, $v);
return $f;
}
/**
* Toggle the given attribute and value.
*
* @param attribute string the attribute to toggle
* @param value string the value for the given attribute to toggle
* @return Facets a new Facets object
* @throws \InvalidArgumentException if attribute is null or the empty string.
*/
public function toggle($attribute, $value) {
$f = new Facets();
$f->facetCollection = $this->facetCollection->deepCopy();
$f->facetCollection->toggle($attribute, $value);
return $f;
}
/**
* Return a new Facets with the given attribute range added. If a range of the
* current attribute already existed, it will replace the existing one. Note that
* facet ranges are inclusive.
*
* @param attribute string the attribute
* @param min string the min value of the range
* @param min string the max value of the range
* @return Facets a new Facets object
* @throws \InvalidArgumentException if attribute is null or the empty string.
*/
public function addRange($attribute, $min, $max) {
$f = new Facets();
$f->facetCollection = $this->facetCollection->deepCopy();
$f->facetCollection->addRange($attribute, $min, $max);
return $f;
}
/**
* Return a new Facets with the given attribute removed.
*
* @param attribute string the attribute
* @return Facets a new Facets object
* @throws \InvalidArgumentException if attribute is null or the empty string.
*/
public function remove($attribute) {
$f = new Facets();
$f->facetCollection = $this->facetCollection->deepCopy();
$f->facetCollection->remove($attribute);
return $f;
}
/**
* Create a Facets object from the source string.
* @param source string the string to parse
* @return Facets Facets object from the source string
*/
public static function parse($source){
$f = new Facets();
$f->facetCollection = FacetCollection::parse($source);
return $f;
}
/**
* Builds and returns a string on the facet argument format.
* @return string a string on the facet argument format
*/
public function __toString() {
return (string) $this->facetCollection;
}
}