/** * Creates an Internet session ready to be populated with requests. */ public function __construct() { $this->m_requestRecordsQueue = CArray::make(); $this->m_multiCurl = curl_multi_init(); if (!is_resource($this->m_multiCurl)) { $this->m_hasError = true; $this->m_errorMessage = "Multi cURL initialization failed."; $this->finalize(); return; } }
/** * Creates a URL path from a path string or as empty. * * If a source path string is provided, it should start with "/". Just like in any valid URL, the path string is * not expected to contain characters that cannot be represented literally and percent-encoding is expected to be * used for any such characters. No trailing "/" are stripped off the path string, so if the path is e.g. * "/comp0/comp1/", it produces an empty string as the last component. * * @param string $path **OPTIONAL. Default is** *create an empty URL path*. A string with the source path. */ public function __construct($path = null) { assert('!isset($path) || is_cstring($path)', vs(isset($this), get_defined_vars())); assert('!isset($path) || CString::startsWith($path, "/")', vs(isset($this), get_defined_vars())); if (isset($path)) { $path = CString::stripStart($path, "/"); $this->m_components = CString::split($path, "/"); $len = CArray::length($this->m_components); for ($i = 0; $i < $len; $i++) { $this->m_components[$i] = CUrl::leaveTdNew($this->m_components[$i]); } } else { $this->m_components = CArray::make(); } }
/** * Normalizes a path by removing any trailing slashes, any redundant slashes, any references to the current * directory, and any references to the parent directory where possible, and returns the new path. * * For example, "/path//./dir-a/.././to//../dir-b/" is normalized to "/path/dir-b". * * @param string $path The path to be normalized (can be absolute or relative). * @param bool $targetIsExecutable **OPTIONAL. Default is** `false`. Tells whether the path's target should be * treated as an executable so that, if the path starts with ".", the resulting path will start with "." too and * the "." will not be removed as a reference to the current directory. * * @return CUStringObject The normalized path. */ public static function normalize($path, $targetIsExecutable = false) { assert('is_cstring($path) && is_bool($targetIsExecutable)', vs(isset($this), get_defined_vars())); assert('!CString::isEmpty($path)', vs(isset($this), get_defined_vars())); $path = CRegex::replace($path, "/\\/{2,}/", "/"); // normalize consecutive slashes $path = CString::stripEnd($path, "/"); // remove the trailing slash, if any if (CString::isEmpty($path)) { return "/"; } $path = CRegex::remove($path, "/\\/\\.(?=\\/|\\z)/"); // remove any "/." followed by a slash or at the end if (CString::isEmpty($path)) { return "/"; } if (!$targetIsExecutable) { $path = CString::stripStart($path, "./"); } $pathIsAbsolute; if (!CString::startsWith($path, "/")) { $pathIsAbsolute = false; } else { $pathIsAbsolute = true; $path = CString::substr($path, 1); } if (!CString::find($path, "/")) { if ($pathIsAbsolute) { if (!CString::equals($path, "..")) { $path = "/{$path}"; } else { $path = "/"; } } return $path; } // Recompose the path. $components = CString::split($path, "/"); $newComponents = CArray::make(); $len = CArray::length($components); for ($i = 0; $i < $len; $i++) { $comp = $components[$i]; $lastAddedComp = ""; $noCompsAddedYet = CArray::isEmpty($newComponents); if (!$noCompsAddedYet) { $lastAddedComp = CArray::last($newComponents); } if (CString::equals($comp, "..")) { if ($noCompsAddedYet || CString::equals($lastAddedComp, "..") || CString::equals($lastAddedComp, ".")) { if (!($noCompsAddedYet && $pathIsAbsolute)) { CArray::push($newComponents, $comp); } } else { CArray::pop($newComponents); } } else { CArray::push($newComponents, $comp); } } $path = CArray::join($newComponents, "/"); if ($pathIsAbsolute) { $path = "/{$path}"; } else { if (CString::isEmpty($path)) { $path = "."; } } return $path; }
/** * Parses a locale into components. * * @param reference $language The two-letter language code of type `CUStringObject` (always lowercased). * @param reference $region **OPTIONAL. OUTPUT.** The two-letter country/region code of type `CUStringObject` * (always uppercased). * @param reference $script **OPTIONAL. OUTPUT.** The four-letter script code of type `CUStringObject` (always * titlecased). * @param reference $variants **OPTIONAL. OUTPUT.** The variants of type `CArrayObject` with elements of type * `CUStringObject` (always uppercased). * @param reference $keywords **OPTIONAL. OUTPUT.** The keyword-value pairs of type `CMapObject` with values of * type `CUStringObject`. * * @return void */ public function components(&$language, &$region = null, &$script = null, &$variants = null, &$keywords = null) { $subtags = Locale::parseLocale($this->m_name); if (CMap::hasKey($subtags, Locale::LANG_TAG)) { $language = $subtags[Locale::LANG_TAG]; } if (CMap::hasKey($subtags, Locale::REGION_TAG)) { $region = $subtags[Locale::REGION_TAG]; } if (CMap::hasKey($subtags, Locale::SCRIPT_TAG)) { $script = $subtags[Locale::SCRIPT_TAG]; } $variants = CArray::make(); for ($i = 0; $i < 15; $i++) { $key = Locale::VARIANT_TAG . $i; if (CMap::hasKey($subtags, $key)) { CArray::push($variants, $subtags[$key]); } } $variants = oop_a($variants); $keywords = oop_m($this->keywords($this->m_name)); }
/** * Splits a string into substrings using a specified substring or substrings as the delimiter(s) and returns the * resulting strings as an array. * * If no delimiter substrings were found, the resulting array contains just one element, which is the original * string. If a delimiter is located at the very start or at the very end of the string or next to another * delimiter, it will accordingly cause some string(s) in the resulting array to be empty. * * As a special case, the delimiter substring can be empty, which will split the string into its constituting * characters. * * @param string $string The string to be split. * @param string|array|map $delimiterOrDelimiters The substring or array of substrings to be recognized as the * delimiter(s). * * @return CArray The resulting strings. */ public static function split($string, $delimiterOrDelimiters) { assert('is_cstring($string) && ' . '(is_cstring($delimiterOrDelimiters) || is_collection($delimiterOrDelimiters))', vs(isset($this), get_defined_vars())); if (is_cstring($delimiterOrDelimiters)) { if (self::isEmpty($delimiterOrDelimiters)) { // Special case. if (self::isEmpty($string)) { $resStrings = CArray::fromElements(""); return $resStrings; } else { $resStrings = CArray::make(strlen($string)); for ($i = 0; $i < strlen($string); $i++) { $resStrings[$i] = $string[$i]; } return $resStrings; } } $resStrings = CArray::make(self::numSubstrings($string, $delimiterOrDelimiters) + 1); $startPos = 0; $i = 0; while (true) { $endPos = self::indexOf($string, $delimiterOrDelimiters, $startPos); if ($endPos != -1) { $resStrings[$i++] = self::substring($string, $startPos, $endPos); $startPos = $endPos + strlen($delimiterOrDelimiters); } else { $resStrings[$i] = self::substr($string, $startPos); break; } } return $resStrings; } else { $resStrings = CArray::fromElements($string); foreach ($delimiterOrDelimiters as $delimiter) { assert('is_cstring($delimiter)', vs(isset($this), get_defined_vars())); $resStringsNew = CArray::make(); $len = CArray::length($resStrings); for ($i = 0; $i < $len; $i++) { CArray::pushArray($resStringsNew, self::split($resStrings[$i], $delimiter)); } $resStrings = $resStringsNew; } return $resStrings; } }
public function leaveNode(PhpParser\Node $node) { if ($node instanceof PhpParser\Node\Stmt\Class_ || $node instanceof PhpParser\Node\Stmt\Trait_) { $this->m_numEnteredClassesOrTraits--; } else { if ($node instanceof PhpParser\Node\Stmt\Interface_) { $this->m_numEnteredInterfaces--; } else { if ($node instanceof PhpParser\Node\Stmt\ClassMethod) { $numEnteredMethods = $this->m_numEnteredMethods; $this->m_numEnteredMethods--; if (!($this->m_numEnteredClassesOrTraits == 1 && $this->m_numEnteredInterfaces == 0 && $numEnteredMethods == 1 && $this->m_numEnteredClosures == 0 && $this->m_numEnteredFunctions == 0)) { return; } $method = $node; if ($method->isProtected() && !$this->m_wrapProtectedMethods && !$this->m_isTrait || $method->isPrivate() && !$this->m_wrapPrivateMethods && !$this->m_isTrait || $method->isAbstract()) { return; } if (!isset($method->stmts) || CMap::isEmpty($method->stmts)) { return; } $hasParams = false; $params = CArray::make(); $hasParamsByRef = false; $paramsByRef = CArray::make(); if (isset($method->params) && !CMap::isEmpty($method->params)) { $hasParams = true; $params = CArray::fromPArray($method->params); $paramsByRef = CArray::filter($params, function ($param) { return $param->byRef; }); $hasParamsByRef = !CArray::isEmpty($paramsByRef); } $method->stmts[0]->setAttribute("_imFirstStmtInMethodOrFunction", true); $method->stmts[CMap::length($method->stmts) - 1]->setAttribute("_imLastStmtInMethodOrFunction", true); $statements = [$method]; $methodFirstLastStmtVisitor = new CMethodOrFunctionFirstLastStmtVisitor($hasParams, $params, $hasParamsByRef, $paramsByRef); $traverser = new PhpParser\NodeTraverser(); $traverser->addVisitor($methodFirstLastStmtVisitor); $statements = $traverser->traverse($statements); $method = $statements[0]; $returnVisitor = new CReturnVisitor($hasParams, $params, $hasParamsByRef, $paramsByRef, $method->byRef, CReturnVisitor::SUBJ_METHOD); $traverser = new PhpParser\NodeTraverser(); $traverser->addVisitor($returnVisitor); $statements = $traverser->traverse($statements); return $statements; } else { if ($node instanceof PhpParser\Node\Expr\Closure) { $this->m_numEnteredClosures--; } else { if ($node instanceof PhpParser\Node\Stmt\Function_) { $this->m_numEnteredFunctions--; } } } } } }
public function testIsSubsetOf() { // Using the default comparator. $array = CArray::fromElements("a", "b", "c", "a", "b", "c", "a", "b", "c"); $this->assertTrue(CArray::isSubsetOf($array, CArray::fromElements("a", "b", "c"))); $array = CArray::fromElements("a", "b", "c", "a", "b", "c", "a", "d", "b", "c"); $this->assertFalse(CArray::isSubsetOf($array, CArray::fromElements("a", "b", "c"))); // Using a custom comparator. $array = CArray::fromElements("a", "b", "c", "a", "b", "c", "a", "b", "c"); $comparator = function ($string0, $string1) { return CString::toLowerCase($string0) === CString::toLowerCase($string1); }; $this->assertTrue(CArray::isSubsetOf($array, CArray::fromElements("A", "B", "C"), $comparator)); // Special case. $array = CArray::make(); $this->assertFalse(CArray::isSubsetOf($array, CArray::fromElements("a", "b", "c"))); }
/** * Sets the alternative body of a message to be used if the primary body cannot be displayed. * * @param string $body The alternative body of the message. * @param string $type **OPTIONAL. Default is** `CMimeType::PLAIN_TEXT`. The MIME type of the alternative body. * * @return void */ public function addAltBody($body, $type = CMimeType::PLAIN_TEXT) { assert('is_cstring($body) && is_cstring($type)', vs(isset($this), get_defined_vars())); if (!isset($this->m_altBodiesAndTypes)) { $this->m_altBodiesAndTypes = CArray::make(); } $bodyAndType = CArray::fromElements($body, $type); CArray::push($this->m_altBodiesAndTypes, $bodyAndType); }
/** * Sets the byte range(s) of interest in the requested entity. * * @param array $byteRangeOrRanges The byte range(s) of interest. This is either an array with two integer values * specifying the byte range to be retrieved or, for HTTP requests only, an array of such arrays specifying * multiple byte ranges. The second value in a pair is allowed to be `null`, in which case the range is considered * to be open-ended and the server is expected to respond with data starting from the byte indicated by the first * value in the pair and up to the very last byte available. * * @return void */ public function setRequestedByteRange($byteRangeOrRanges) { assert('is_carray($byteRangeOrRanges) && !CArray::isEmpty($byteRangeOrRanges)', vs(isset($this), get_defined_vars())); if (!is_carray(CArray::first($byteRangeOrRanges))) { assert('CArray::length($byteRangeOrRanges) == 2', vs(isset($this), get_defined_vars())); $rangeBPosLow = $byteRangeOrRanges[0]; $rangeBPosHigh = $byteRangeOrRanges[1]; assert('is_int($rangeBPosLow) && (is_null($rangeBPosHigh) || is_int($rangeBPosHigh))', vs(isset($this), get_defined_vars())); assert('$rangeBPosLow >= 0 && (is_null($rangeBPosHigh) || $rangeBPosHigh >= 0)', vs(isset($this), get_defined_vars())); assert('is_null($rangeBPosHigh) || $rangeBPosHigh >= $rangeBPosLow', vs(isset($this), get_defined_vars())); $this->m_requestedByteRange = !is_null($rangeBPosHigh) ? "{$rangeBPosLow}-{$rangeBPosHigh}" : "{$rangeBPosLow}-"; } else { assert('$this->isHttp()', vs(isset($this), get_defined_vars())); $preOutString = CArray::make(); $len = CArray::length($byteRangeOrRanges); $lenMinusOne = $len - 1; for ($i = 0; $i < $len; $i++) { $range = $byteRangeOrRanges[$i]; assert('is_carray($range)', vs(isset($this), get_defined_vars())); assert('CArray::length($range) == 2', vs(isset($this), get_defined_vars())); $rangeBPosLow = $range[0]; $rangeBPosHigh = $range[1]; if (CDebug::isDebugModeOn()) { // The high byte position can be specified as `null` for the last range only. if ($i != $lenMinusOne) { assert('is_int($rangeBPosLow) && is_int($rangeBPosHigh)', vs(isset($this), get_defined_vars())); assert('$rangeBPosLow >= 0 && $rangeBPosHigh >= 0', vs(isset($this), get_defined_vars())); assert('$rangeBPosHigh >= $rangeBPosLow', vs(isset($this), get_defined_vars())); } else { assert('is_int($rangeBPosLow) && (is_null($rangeBPosHigh) || is_int($rangeBPosHigh))', vs(isset($this), get_defined_vars())); assert('$rangeBPosLow >= 0 && (is_null($rangeBPosHigh) || $rangeBPosHigh >= 0)', vs(isset($this), get_defined_vars())); assert('is_null($rangeBPosHigh) || $rangeBPosHigh >= $rangeBPosLow', vs(isset($this), get_defined_vars())); } } $rbr = !is_null($rangeBPosHigh) ? "{$rangeBPosLow}-{$rangeBPosHigh}" : "{$rangeBPosLow}-"; CArray::push($preOutString, $rbr); } $this->m_requestedByteRange = CArray::join($preOutString, ","); } }
/** * Splits a string into substrings using a specified pattern or patterns as the delimiter(s) and returns the * resulting strings as an array. * * If no delimiter patterns were found, the resulting array contains just one element, which is the original * string. If a delimiter is located at the very start or at the very end of the string or next to another * delimiter, it will accordingly cause some string(s) in the resulting array to be empty. * * As a special case, the delimiter pattern can be empty, which will split the string into its constituting * characters. * * @param string $string The string to be split. * @param string|array|map $delimiterPatternOrPatterns The pattern or array of patterns to be recognized as the * delimiter(s). * * @return CArray The resulting strings. */ public static function split($string, $delimiterPatternOrPatterns) { assert('is_cstring($string) && (is_cstring($delimiterPatternOrPatterns) || ' . 'is_collection($delimiterPatternOrPatterns))', vs(isset($this), get_defined_vars())); if (is_cstring($delimiterPatternOrPatterns)) { $numIdt = self::findGroups($delimiterPatternOrPatterns, "/^([^0-9A-Za-z\\s\\\\])(.*)\\1/", $foundGroups); assert('$numIdt == 2', vs(isset($this), get_defined_vars())); $idt = $foundGroups[1]; if (CString::isEmpty($idt)) { // Special case. if (CString::isEmpty($string)) { $resStrings = CArray::fromElements(""); return $resStrings; } else { if (preg_match("/^([^0-9A-Za-z\\s\\\\])\\1[A-Za-z]*u[A-Za-z]*\\z/", $delimiterPatternOrPatterns) !== 1) { $resStrings = CArray::make(strlen($string)); for ($i = 0; $i < strlen($string); $i++) { $resStrings[$i] = $string[$i]; } return $resStrings; } else { return CUString::splitIntoChars($string); } } } $paResStrings = preg_split($delimiterPatternOrPatterns, $string); $qty = count($paResStrings); $resStrings = CArray::make($qty); for ($i = 0; $i < $qty; $i++) { $resStrings[$i] = $paResStrings[$i]; } return $resStrings; } else { $resStrings = CArray::fromElements($string); foreach ($delimiterPatternOrPatterns as $delimiterPattern) { assert('is_cstring($delimiterPattern)', vs(isset($this), get_defined_vars())); $resStringsNew = CArray::make(); $len = CArray::length($resStrings); for ($i = 0; $i < $len; $i++) { CArray::pushArray($resStringsNew, self::split($resStrings[$i], $delimiterPattern)); } $resStrings = $resStringsNew; } return $resStrings; } }
/** * Creates an OOP array as either empty or having a specified length. * * @param int $length **OPTIONAL. Default is** `0`. The length of the array. Don't forget to give values to all * of the allocated elements when using this parameter. */ public function __construct($length = 0, $_noinit = false) { if ($_noinit) { return; } $this->m_splArray = CArray::make($length); }
/** * @ignore */ public static function initialize() { $configDp = CFilePath::add($GLOBALS["PHRED_PATH_TO_APP"], "Configuration"); $configEnvsDp = CFilePath::add($configDp, "Environments"); $configs = CArray::make(); $currEnv; if ($GLOBALS["PHRED_TESTS"]) { $currEnv = "tst"; } // Main configuration files. $configFps = CFile::findFiles(CFilePath::add($configDp, "*.json")); $numConfigs = CArray::length($configFps); for ($i = 0; $i < $numConfigs; $i++) { $configFp = $configFps[$i]; $configName = CFilePath::nameOnly($configFp); self::readAndAddConfig($configFp, $configName, $configs); if (!isset($currEnv) && CString::equals($configName, "Application")) { $config = CArray::last($configs); $currEnv = $config[self::$ms_configAliases["Application"]]["environment"]; } } assert('is_cstring($currEnv)', vs(isset($this), get_defined_vars())); // The configuration files from the current environment's directory. $currEnvDp = CFilePath::add($configEnvsDp, $currEnv); assert('CFile::exists($currEnvDp)', vs(isset($this), get_defined_vars())); $configFps = CFile::findFiles(CFilePath::add($currEnvDp, "*.json")); $numConfigs = CArray::length($configFps); for ($i = 0; $i < $numConfigs; $i++) { $configFp = $configFps[$i]; $configName = CFilePath::nameOnly($configFp); self::readAndAddConfig($configFp, $configName, $configs); } self::$ms_config = call_user_func_array("CMap::merge", CArray::toPArray($configs)); self::$ms_isInitialized = true; }
/** * Creates a filter suited for a specified data type to be outputted. * * @param enum $expectedType The output data type that is expected (see [Summary](#summary)). * @param mixed $collectionInputFilters **OPTIONAL.** *Required only when the expected type is `CARRAY` or * `CMAP`*. The array or map containing the filters to be applied to the elements in the input array if the * expected type is `CARRAY` or to the values in the input map if the expected type is `CMAP`. If the input value * is going to be an array, the number of filters in this parameter should match the length of the input array, and * if the input value is going to be a map, the keys in this parameter should match the keys in the input map. Same * as input arrays and maps, this parameter can be multidimensional in order to correspond with the input value. */ public function __construct($expectedType, $collectionInputFilters = null) { assert('is_enum($expectedType) && !($expectedType != self::CARRAY && $expectedType != self::CMAP && ' . 'isset($collectionInputFilters)) && !($expectedType == self::CARRAY && ' . '!is_carray($collectionInputFilters)) && !($expectedType == self::CMAP && ' . '!is_cmap($collectionInputFilters))', vs(isset($this), get_defined_vars())); $this->m_expectedType = $expectedType; $this->m_collectionInputFilters = $collectionInputFilters; switch ($expectedType) { case self::BOOL: $this->m_defaultValue = false; break; case self::INT: $this->m_defaultValue = 0; break; case self::FLOAT: $this->m_defaultValue = 0.0; break; case self::CSTRING: case self::CUSTRING: $this->m_defaultValue = ""; break; case self::CARRAY: $this->m_defaultValue = CArray::make(); break; case self::CMAP: $this->m_defaultValue = CMap::make(); break; case self::EMAIL: case self::URL: case self::IP: $this->m_defaultValue = ""; break; default: assert('false', vs(isset($this), get_defined_vars())); break; } }