/** * 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; }
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 (CString::equalsCi($method->name, "__get")) { $this->m_hasGetMMethod = true; } else { if (CString::equalsCi($method->name, "__set")) { $this->m_hasSetMMethod = true; } } } else { if ($node instanceof PhpParser\Node\Expr\Closure) { $this->m_numEnteredClosures--; } else { if ($node instanceof PhpParser\Node\Stmt\Function_) { $this->m_numEnteredFunctions--; } else { if ($node instanceof PhpParser\Node\Stmt\Property) { if (!($this->m_numEnteredClassesOrTraits == 1 && $this->m_numEnteredInterfaces == 0 && $this->m_numEnteredMethods == 0 && $this->m_numEnteredClosures == 0 && $this->m_numEnteredFunctions == 0)) { return; } $property = $node; if ($property->isProtected() || $property->isPrivate() || $property->isStatic()) { return; } if (!isset($property->props) || CMap::isEmpty($property->props)) { return; } foreach ($property->props as $prop) { CArray::push($this->m_propsToWrap, $prop->name); } $property->type = PhpParser\Node\Stmt\Class_::MODIFIER_PROTECTED; return $property; } } } } } } }
/** * 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)); }
/** * 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); }
public function testPush() { $array = CArray::fromElements("a", "b", "c"); $newLength = CArray::push($array, "d"); $this->assertTrue(CArray::equals($array, CArray::fromElements("a", "b", "c", "d"))); $this->assertTrue($newLength == 4); }
/** * Adds a component to a URL path. * * @param string $component The component to be added. All the characters in the component's string should be * represented literally and none of the characters should be percent-encoded. * * @return void */ public function addComponent($component) { assert('is_cstring($component)', vs(isset($this), get_defined_vars())); CArray::push($this->m_components, $component); }
protected function addHeaderWithoutOverriding($headers, $headerName, $value) { $headerLine; $headerName = CString::trim($headerName); $value = CString::trim($value); $foundHeaderPos; $alreadyExists = CArray::find($headers, $headerName, function ($element0, $element1) { return CRegex::find($element0, "/^\\h*" . CRegex::enterTd($element1) . "\\h*:/i"); }, $foundHeaderPos); if (!$alreadyExists) { $headerLine = "{$headerName}: {$value}"; } else { // The header already exists. Combine the header values, removing duplicates based on case-insensitive // equality. $currValue = CRegex::remove($headers[$foundHeaderPos], "/^.*?:\\h*/"); CArray::remove($headers, $foundHeaderPos); $values = CString::split("{$currValue}, {$value}", ","); $len = CArray::length($values); for ($i = 0; $i < $len; $i++) { $values[$i] = CString::trim($values[$i]); } $values = CArray::filter($values, function ($element) { return !CString::isEmpty($element); }); $values = CArray::unique($values, function ($element0, $element1) { return CString::equalsCi($element0, $element1); }); $combinedValue = CArray::join($values, ", "); $headerLine = "{$headerName}: {$combinedValue}"; } CArray::push($headers, $headerLine); }
/** * Adds a request to a session. * * @param CInetRequest $request The request to be added. * @param callable $onCompleteCallback **OPTIONAL.** The callback function or method to be called by the session * when the request completes. The function is expected to take four parameters: a flag of type `bool` that tells * whether the request was successful, the response of type `CUStringObject` that was received for the request, an * object of type `CInetRequest` that represents the request, and an object of type `CInetSession` that represents * the session to which the request belongs, in this order. You can use the session's object to add more requests * to the request queue of the session depending on the response or any other factors. * @param bool $newCookieSession **OPTIONAL. Default is** `false`. Tells whether to delete all previously stored * cookies that were set by the remote server(s) to expire when the "browsing session" during which those cookies * were received comes to an end (you can have as much of such "browsing sessions" as you like). If this parameter * is `true`, the "browsing session" cookies are deleted before the request is sent. * * @return void */ public function addRequest(CInetRequest $request, $onCompleteCallback = null, $newCookieSession = false) { assert('(!isset($onCompleteCallback) || is_callable($onCompleteCallback)) && is_bool($newCookieSession)', vs(isset($this), get_defined_vars())); $requestRecord = CArray::fromElements($request, $onCompleteCallback, $newCookieSession); CArray::push($this->m_requestRecordsQueue, $requestRecord); }
/** * Adds an element to the end of an array. * * @param mixed $element The element to be added. * * @return int The array's new length. */ public function push($element) { return CArray::push($this->m_splArray, $element); }
protected static function readAndAddConfig($configFp, $configName, $configs) { if (CMap::hasKey(self::$ms_configAliases, $configName)) { $configName = self::$ms_configAliases[$configName]; } $configName = CString::toLowerCase($configName); $configJson = CFile::read($configFp); $configJson = CRegex::remove($configJson, "/^\\h*\\/\\/.*/m"); // remove comments $configJson = "{\"{$configName}\": {$configJson}}"; $json = new CJson($configJson); $success; $config = $json->decode($success); assert('$success', vs(isset($this), get_defined_vars())); CArray::push($configs, $config); }