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 result having the category_tree format. A CategoryNode is part of a category tree and may, in addition to
* attributes, have a parent and/or subcategories. The tree can be traversed using for example CategoryNode.getSubcategories(),
* or utility methods such as CategoryNode.getSelectedSubcategory() and CategoryNode.findDescendant(key).
*
* @package Apptus\ESales\Connector
*/
class CategoryNode extends Category implements Result, \IteratorAggregate {
private $parent;
private $subcategories = array();
private $selected;
/**
* @internal
* @param string $key
* @param string $ticket
* @param string $parentKey
* @param CategoryNode $parent
*/
public function __construct($key, $ticket, $parentKey, CategoryNode $parent=null) {
parent::__construct($key, $ticket, $parentKey);
$this->parent = $parent;
}
/**
* @internal
* @param CategoryNode $subcategory
*/
public function addSubcategory(CategoryNode $subcategory) {
array_push($this->subcategories, $subcategory);
}
/**
* @internal
*/
public function addSpecialAttribute($name, $value) {
parent::addSpecialAttribute($name, $value);
switch ($name) {
case "selected":
$this->selected = filter_var($value, FILTER_VALIDATE_BOOLEAN);
break;
}
}
/**
* Get a list of this category's subcategories.
*
* @return array the subcategories
*/
public function getSubcategories() {
return $this->subcategories;
}
/**
* Get the selected category among this category's subcategories, or null if none is selected.
*
* @return CategoryNode the selected subcategory, or null
*/
public function getSelectedSubcategory() {
/* @var $category CategoryNode */
foreach ($this->subcategories as $category) {
if ($category->isSelected()) {
return $category;
}
}
return null;
}
/**
* Searches for the specified category key among this category and its descendants.
* Returns null if the category key was not found.
*
* @param string $categoryKey
* the key of the category to search for
* @return CategoryNode
* the found CategoryNode with key $categoryKey, or null
*/
public function findDescendant($categoryKey) {
$queue = new \SplQueue();
$queue->enqueue($this);
while (!$queue->isEmpty()) {
/* @var $category CategoryNode */
$category = $queue->dequeue();
if ($category->key() === $categoryKey) {
return $category;
}
foreach ($category->getSubcategories() as $subcategory) {
$queue->enqueue($subcategory);
}
}
return null;
}
/**
* Get the parent of this category.
*
* @return CategoryNode the parent of this category
*/
public function getParent() {
return $this->parent;
}
/**
* Check if this category is a selected category.
* Returns true if the category is selected (or if the category is an ancestor to a selected category)
*
* @return boolean true if this category is selected
*/
public function isSelected() {
return $this->selected;
}
/**
* Get the result type of this result.
*
* @return string The result type
* @see ResultType
*/
public function getType()
{
return ResultType::CATEGORY_TREE;
}
}