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: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243:
<?php
namespace Apptus\ESales\Connector;
/**
* Convenience class for working with panel paths.
*/
class Path {
const SEPARATOR = '/';
/**
* Returns a path denoting the root, '/'.
*
* @return string
*/
public static function root() {
return self::SEPARATOR;
}
private final function __construct($path) {
// Prevent instantiation
}
/**
* Returns a new path denoting a specified child element of the element denoted by this path.
*
* For instance, Path::childPath('/an/example/', 'child') is equal to '/an/example/child'.
*
* @param string
* A path.
* @param string
* The name of the child element.
* @throws \InvalidArgumentException if name contains illegal characters.
* @return string
* A path denoting the child element.
*/
public static function childPath($path, $name) {
if (strstr($name, self::SEPARATOR) !== false) {
throw new \InvalidArgumentException('Child name must not contain ' . self::SEPARATOR);
}
if (self::isRoot($path)) {
return $path . $name;
} else {
return $path . self::SEPARATOR . $name;
}
}
/**
* Returns a new path denoting the parent element of the element denoted by this path.
*
* For instance, Path::parentPath('/an/example/') is equal to '/an'.
*
* @param string
* A path.
* @throws \InvalidArgumentException
* @return string
* A new path denoting the parent element.
*/
public static function parentPath($path) {
if (self::isRoot($path)) {
throw new \InvalidArgumentException('root has no parent');
}
$ix = strrpos($path, self::SEPARATOR);
if ($ix === false) {
throw new \InvalidArgumentException('relative path has no parent');
}
$parentPath = substr($path, 0, $ix);
if ($parentPath === '') {
return self::root();
}
return $parentPath;
}
/**
* Returns all the ancestors to a path, starting with the closest.
*
* For instance, Path::ancestors('/an/example/path') will return an indexed array containing
* the paths: '/an/example', '/an' and '/' in that order.
*
* @param string
* A path.
* @throws \InvalidArgumentException
* @return array
* A list of ancestor path strings.
*/
public static function ancestors($path) {
if (self::isRoot($path)) {
throw new \InvalidArgumentException('root has no parent');
}
$ancestors = array ();
$path = self::parentPath($path);
while (self::hasParent($path)) {
$ancestors[] = $path;
$path = self::parentPath($path);
}
$ancestors[] = self::root();
return $ancestors;
}
/**
* Returns the simple name of the element denoted by this path.
*
* For instance, Path::simpleName('/an/example') becomes 'example'.
*
* @param string
* A path.
* @return string
*/
public static function simpleName($path) {
if (self::isRoot($path)) {
return $path;
}
$ix = strrpos($path, self::SEPARATOR);
if ($ix === false) {
return $path;
}
return substr($path, $ix + 1);
}
/**
* True if this path denotes the root '/', false otherwise.
*
* @param string
* A path.
* @return boolean
*/
public static function isRoot($path) {
return ($path === self::SEPARATOR);
}
/**
* True if this path has a parent path.
*
* @param string
* A path.
* @return boolean
*/
public static function hasParent($path) {
return (!self::isRoot($path) && (strrpos($path, self::SEPARATOR) !== false));
}
/**
* Returns the number of components in a path.
*
* The root (/) has length 1.
*
* @param string
* A path.
* @return int
*/
public static function length($path) {
$c = 1;
while (self::hasParent($path)) {
++$c;
$path = self::parentPath($path);
}
return $c;
}
/**
* Returns the ancestor of length $length of a path.
*
* For instance, Path::ancestor('/a/b/c/d', 3) returns /a/b,
* since /a/b is the ancestor of /a/b/c/d with length 3.
*
* @param string
* A path.
* @param int
* @return string
*/
public static function ancestor($path, $length) {
while (self::length($path) > $length && self::hasParent($path)) {
$path = self::parentPath($path);
}
return $path;
}
/**
* Returns an end path of a path.
*
* For instance, Path::endpath('/a/b/c/d', 2) returns the relative
* path b/c/d, since the prefix /a of length 2 is cut.
*
* @param string
* A path.
* @param int
* @throws \InvalidArgumentException
* @return string
*/
public static function endpath($path, $start) {
if ($start == 0) {
return $path;
}
if ($start > self::length($path)) {
throw new \InvalidArgumentException('Endpath would be longer than this path.');
}
$startpath = self::ancestor($path, $start);
$end = substr($path, strlen($startpath));
if ($end !== '' && ($end[0] === self::SEPARATOR)) {
$end = substr($end, 1);
}
return $end;
}
/**
* Returns a descendant of a path.
*
* For instance, Path::descendant('/a/b', 'c/d') returns the path '/a/b/c/d'.
*
* @param string
* @param string
* @return string
*/
public static function descendant($path, $endpath) {
if (self::isRoot($endpath)) {
return $path;
}
if (self::isRoot(self::ancestor($endpath, 1))) {
$endpath = self::endpath($endpath, 1);
}
if (self::isRoot($path)) {
return $path . $endpath;
} else {
return $path . self::SEPARATOR . $endpath;
}
}
/**
* Returns true if this path has an ancestor as specified by the $ancestor argument. Otherwise, returns false.
*
* For instance, Path::hasAncestor('/a/b/c', '/a') returns true,
* while Path::hasAncestor('/a/b', '/c') return false.
*
* @param string
* @param string
* @return boolean
*/
public static function hasAncestor($path, $ancestor) {
$aLength = self::length($ancestor);
return ($aLength <= self::length($path) && self::ancestor($path, $aLength) === $ancestor);
}
}