本文整理汇总了PHP中StringUtils::charCount方法的典型用法代码示例。如果您正苦于以下问题:PHP StringUtils::charCount方法的具体用法?PHP StringUtils::charCount怎么用?PHP StringUtils::charCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringUtils
的用法示例。
在下文中一共展示了StringUtils::charCount方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: isValid
/**
* Returns TRUE if the given value passes validation.
*
* @param string $value A value to test
*
* @return boolean
*/
public function isValid($value)
{
if ($this->skipValidation) {
return true;
}
$datatype = $this->validation['datatype'];
//NULL check, empty strings are considered null
if (in_array($datatype, array('string', 'url', 'email', 'slug', 'slugwithslash', 'html', 'binary', 'json')) && strlen(trim($value)) == 0) {
$value = null;
}
if ($this->validation['nullable'] === false && $value === null && $datatype != 'boolean') {
$this->failureCode = 'nullable';
$this->failureMessage = 'cannot be empty';
return false;
}
//Nothing more to validate if the value is null...
if ($value === null) {
return true;
}
//Check date makes sense
if ($datatype === 'date') {
//todo: not sure how to check validity of date... it's already a DateTime instance.
if (false) {
$this->failureCode = 'invalid';
$this->failureMessage = 'is an invalid date';
return false;
}
}
//Validate MIN
$min = $this->validation['min'];
if ($min != null) {
if ($datatype === 'float') {
if ($value < floatval($min)) {
$this->failureCode = 'min';
$this->failureMessage = 'is less than the minimum value';
return false;
}
} else {
if ($datatype === 'int') {
if ($value < intval($min)) {
$this->failureCode = 'min';
$this->failureMessage = 'is less than the minimum value';
return false;
}
} else {
if (is_string($value) && strlen($value) < intval($min)) {
$this->failureCode = 'minlength';
$this->failureMessage = 'must be at least ' . $min . ' characters';
return false;
}
}
}
}
//Validate MAX
$max = $this->validation['max'];
if ($max != null) {
if ($datatype === 'float') {
if ($value > floatval($max)) {
$this->failureCode = 'max';
$this->failureMessage = 'is more than the maximum value';
return false;
}
} else {
if ($datatype === 'int') {
if ($value > intval($max)) {
$this->failureCode = 'max';
$this->failureMessage = 'is more than the maximum value';
return false;
}
} else {
$maxbytes = intval($max);
if (intval($max) < 255) {
// count characters
if (is_string($value) && StringUtils::charCount($value) > intval($max)) {
$this->failureCode = 'maxlength';
$this->failureMessage = 'must be a maximum of ' . $max . ' characters';
return false;
}
$maxbytes = 255;
}
// count bytes
if (is_string($value) && StringUtils::byteCount($value) > intval($maxbytes)) {
$this->failureCode = 'maxlength';
$this->failureMessage = 'must be a maximum of ' . $maxbytes . ' bytes';
return false;
}
}
}
}
if ($datatype === 'slug') {
if (!SlugUtils::isSlug($value, false)) {
$this->failureCode = 'invalid';
$this->failureMessage = 'is not a valid slug, cannot contain slashes';
//.........这里部分代码省略.........
示例2: truncateToMax
/**
* Returns a string truncated to fit inside a meta.
*
* @param mixed $obj - object that has the schema
* (Node | NodeRef | Element | Aspect)
* @param string $metaName
* @param string $string - string to be truncated
* @param bool $ellipsis - if set, output is appended with three dots
* @return string
*/
public static function truncateToMax($obj, $metaName, $string, $ellipsis = false)
{
switch (true) {
case $obj instanceof Element:
case $obj instanceof Aspect:
break;
case $obj instanceof NodeRef:
case $obj instanceof Node:
$obj = $obj->Element;
break;
default:
throw new Exception(__CLASS__ . '::' . __FUNCTION__ . ": Expected first argument to be instance of Element, Aspect, Node or NodeRef");
}
$metaValidationArray = $obj->Schema->getMetaDef($metaName)->Validation->toArray();
$max = $metaValidationArray['max'];
$maxBytes = max($max, 255);
$truncated = false;
if (strlen($string) > $maxBytes) {
$truncated = true;
$string = StringUtils::utf8SafeTruncate($string, $maxBytes - ($ellipsis ? 3 : 0));
}
if ($max < 255 && StringUtils::charCount($string) > $max) {
$truncated = true;
$string = StringUtils::utf8Substr($string, 0, $max - ($ellipsis ? 1 : 0));
}
return $string . ($truncated && $ellipsis ? "…" : '');
}