public function testIsEmpty() { $map = CMap::make(); $this->assertTrue(CMap::isEmpty($map)); $map = CMap::make(); $map["one"] = "a"; $this->assertFalse(CMap::isEmpty($map)); $map = CMap::make(); $map["one"] = null; $this->assertFalse(CMap::isEmpty($map)); }
protected static function requestField($map, $fieldName, CInputFilter $inputFilter, &$success) { $success = true; // Account for the fact that, with PHP, GET and POST (and cookie) fields arrive having "." replaced with "_" // in their names. $fieldName = CString::replace($fieldName, ".", "_"); $value; $hasField = false; if (CMap::hasKey($map, $fieldName)) { $value = $map[$fieldName]; if (isset($value)) { if (!is_cstring($value) && !is_cmap($value)) { // Should not happen in the known versions of PHP. assert('false', vs(isset($this), get_defined_vars())); $success = false; return $inputFilter->defaultValue(); } if (!self::$ms_treatEmptyRequestValuesAsAbsent) { $hasField = true; } else { if (is_cstring($value)) { $hasField = !CString::isEmpty($value); } else { $hasField = !CMap::isEmpty($value) && !(CMap::length($value) == 1 && CMap::hasKey($value, 0) && is_cstring($value[0]) && CString::isEmpty($value[0])); } } } } if (!$hasField) { $success = false; return $inputFilter->defaultValue(); } $inputFilterOrFilterCollection; if ($inputFilter->expectedType() != CInputFilter::CARRAY && $inputFilter->expectedType() != CInputFilter::CMAP) { $inputFilterOrFilterCollection = $inputFilter; } else { $inputFilterOrFilterCollection = $inputFilter->collectionInputFilters(); } // Recursively convert any PHP array that has sequential keys and for which CArray type is expected into a // CArray, while leaving PHP arrays for which CMap type is expected untouched. $value = self::recurseValueBeforeFiltering($value, $inputFilterOrFilterCollection, $success, 0); if (!$success) { return $inputFilter->defaultValue(); } return $inputFilter->filter($value, $success); }
/** * Determines if a locale has any keyword-value pairs. * * @return bool `true` if the locale has any keyword-value pairs, `false` otherwise. */ public function hasKeywords() { $keywords = Locale::getKeywords($this->m_name); return is_cmap($keywords) && !CMap::isEmpty($keywords); }
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--; } } } } } }
/** * Composes a URL query into a query string ready to be used as a part of a URL and returns it. * * Any characters that cannot be represented literally in a valid query string come out percent-encoded. The * resulting query string never starts with "?". * * Because the characters in field named and field values are stored in their literal representations, the * resulting query string is always normalized, with only those characters appearing percent-encoded that really * require it for the query string to be valid and with the hexadecimal letters in percent-encoded characters * appearing uppercased. Also, no duplicate fields are produced in the resulting query string (even if the object * was constructed from a query string with duplicate fields in it) and "=" is added after any field name that goes * without a value and is not followed by "=". * * @param bool $sortFields **OPTIONAL. Default is** `false`. Tells whether the fields in the query string should * appear sorted in the ascending order, case-insensitively, and with natural order comparison used for sorting. * * @return CUStringObject The query string. */ public function queryString($sortFields = false) { assert('is_bool($sortFields)', vs(isset($this), get_defined_vars())); if (!CMap::isEmpty($this->m_query)) { $useQuery = CMap::makeCopy($this->m_query); // Recursively convert any CArray into a CMap for `http_build_query` function to accept the query. $useQuery = self::recurseQueryValueBeforeComposingQs($useQuery, 0); // Compose a preliminary query string. $queryString = http_build_query($useQuery, "", self::$ms_fieldDelimiters[0], PHP_QUERY_RFC1738); if (!is_cstring($queryString)) { return ""; } // Break the string into fields. $fields = CString::split($queryString, self::$ms_fieldDelimiters[0]); // Adjust the result of `http_build_query` function. $len = CArray::length($fields); for ($i = 0; $i < $len; $i++) { if (CString::find($fields[$i], "=")) { // Revert excessive percent-encoding of the square brackets next to the identifiers of // multidimensional data. $fields[$i] = CRegex::replaceWithCallback($fields[$i], "/(?:%5B(?:[^%]++|%(?!5B|5D))*+%5D)+?=/i", function ($matches) { $value = $matches[0]; $value = CString::replace($value, "%5B", "["); $value = CString::replace($value, "%5D", "]"); return $value; }); // Remove redundant indexing next to the identifiers of simple arrays. $fields[$i] = CRegex::replaceWithCallback($fields[$i], "/^.+?=/", function ($matches) { return CRegex::replace($matches[0], "/\\[\\d+\\]/", "[]"); }); } } if ($sortFields) { // Normalize the order of fields. CArray::sortStringsNatCi($fields); } $queryString = CArray::join($fields, self::$ms_fieldDelimiters[0]); return $queryString; } else { return ""; } }