本文整理匯總了PHP中Magento\Framework\Stdlib\String::strlen方法的典型用法代碼示例。如果您正苦於以下問題:PHP String::strlen方法的具體用法?PHP String::strlen怎麽用?PHP String::strlen使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Magento\Framework\Stdlib\String
的用法示例。
在下文中一共展示了String::strlen方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: validateValue
/**
* Validate data
* Return true or array of errors
*
* @param array|string $value
* @return bool|array
*/
public function validateValue($value)
{
$errors = array();
$attribute = $this->getAttribute();
$label = __($attribute->getStoreLabel());
if ($value === false) {
// try to load original value and validate it
$value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
}
if ($attribute->getIsRequired() && empty($value) && $value !== '0') {
$errors[] = __('"%1" is a required value.', $label);
}
if (!$errors && !$attribute->getIsRequired() && empty($value)) {
return true;
}
// validate length
$length = $this->_string->strlen(trim($value));
$validateRules = $attribute->getValidateRules();
if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
$v = $validateRules['min_text_length'];
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $v);
}
if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
$v = $validateRules['max_text_length'];
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $v);
}
$result = $this->_validateInputRule($value);
if ($result !== true) {
$errors = array_merge($errors, $result);
}
if (count($errors) == 0) {
return true;
}
return $errors;
}
示例2: validateValue
/**
* {@inheritdoc}
*/
public function validateValue($value)
{
$errors = array();
$attribute = $this->getAttribute();
$label = __($attribute->getStoreLabel());
if ($value === false) {
// try to load original value and validate it
$value = $this->_value;
}
if ($attribute->isRequired() && empty($value) && $value !== '0') {
$errors[] = __('"%1" is a required value.', $label);
}
if (!$errors && !$attribute->isRequired() && empty($value)) {
return true;
}
// validate length
$length = $this->_string->strlen(trim($value));
$validateRules = $attribute->getValidationRules();
$minTextLength = ArrayObjectSearch::getArrayElementByName($validateRules, 'min_text_length');
if (!is_null($minTextLength) && $length < $minTextLength) {
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
}
$maxTextLength = ArrayObjectSearch::getArrayElementByName($validateRules, 'max_text_length');
if (!is_null($maxTextLength) && $length > $maxTextLength) {
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
}
$result = $this->_validateInputRule($value);
if ($result !== true) {
$errors = array_merge($errors, $result);
}
if (count($errors) == 0) {
return true;
}
return $errors;
}
示例3: render
/**
* Renders a column
*
* @param \Magento\Framework\Object $row
* @return string
*/
public function render(\Magento\Framework\Object $row)
{
$value = $row->getData($this->getColumn()->getIndex());
if ($this->stringHelper->strlen($value) > 30) {
$value = '<span title="' . $this->escapeHtml($value) . '">' . $this->escapeHtml($this->filterManager->truncate($value, array('length' => 30))) . '</span>';
} else {
$value = $this->escapeHtml($value);
}
return $value;
}
示例4: render
/**
* Renders grid column
*
* @param \Magento\Framework\Object $row
* @return string
*/
public function render(\Magento\Framework\Object $row)
{
$line = parent::_getValue($row);
$wrappedLine = '';
$lineLength = $this->getColumn()->getData('lineLength') ? $this->getColumn()->getData('lineLength') : $this->_defaultMaxLineLength;
for ($i = 0, $n = floor($this->string->strlen($line) / $lineLength); $i <= $n; $i++) {
$wrappedLine .= $this->string->substr($line, $lineLength * $i, $lineLength) . "<br />";
}
return $wrappedLine;
}
示例5: validate
/**
* Validate SKU
*
* @param Product $object
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function validate($object)
{
$attrCode = $this->getAttribute()->getAttributeCode();
$value = $object->getData($attrCode);
if ($this->getAttribute()->getIsRequired() && strlen($value) === 0) {
throw new \Magento\Framework\Exception\LocalizedException(__('The value of attribute "%1" must be set', $attrCode));
}
if ($this->string->strlen($object->getSku()) > self::SKU_MAX_LENGTH) {
throw new \Magento\Framework\Exception\LocalizedException(__('SKU length should be %1 characters maximum.', self::SKU_MAX_LENGTH));
}
return true;
}
示例6: beforeSave
/**
* Special processing before attribute save:
* a) check some rules for password
* b) transform temporary attribute 'password' into real attribute 'password_hash'
*
* @param \Magento\Framework\Object $object
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function beforeSave($object)
{
$password = $object->getPassword();
$length = $this->string->strlen($password);
if ($length > 0) {
if ($length < self::MIN_PASSWORD_LENGTH) {
throw new LocalizedException(__('The password must have at least %1 characters.', self::MIN_PASSWORD_LENGTH));
}
if ($this->string->substr($password, 0, 1) == ' ' || $this->string->substr($password, $length - 1, 1) == ' ') {
throw new LocalizedException(__('The password can not begin or end with a space.'));
}
$object->setPasswordHash($object->hashPassword($password));
}
}
示例7: saveStoreLabels
/**
* Save rule labels for different store views
*
* @param int $ruleId
* @param array $labels
* @throws \Exception
* @return $this
*/
public function saveStoreLabels($ruleId, $labels)
{
$deleteByStoreIds = [];
$table = $this->getTable('salesrule_label');
$adapter = $this->_getWriteAdapter();
$data = [];
foreach ($labels as $storeId => $label) {
if ($this->string->strlen($label)) {
$data[] = ['rule_id' => $ruleId, 'store_id' => $storeId, 'label' => $label];
} else {
$deleteByStoreIds[] = $storeId;
}
}
$adapter->beginTransaction();
try {
if (!empty($data)) {
$adapter->insertOnDuplicate($table, $data, ['label']);
}
if (!empty($deleteByStoreIds)) {
$adapter->delete($table, ['rule_id=?' => $ruleId, 'store_id IN (?)' => $deleteByStoreIds]);
}
} catch (\Exception $e) {
$adapter->rollback();
throw $e;
}
$adapter->commit();
return $this;
}
示例8: getFormattedOptionValue
/**
* Accept option value and return its formatted view
*
* @param string|array $optionValue
* Method works well with these $optionValue format:
* 1. String
* 2. Indexed array e.g. array(val1, val2, ...)
* 3. Associative array, containing additional option info, including option value, e.g.
* array
* (
* [label] => ...,
* [value] => ...,
* [print_value] => ...,
* [option_id] => ...,
* [option_type] => ...,
* [custom_view] =>...,
* )
* @param array $params
* All keys are options. Following supported:
* - 'maxLength': truncate option value if needed, default: do not truncate
* - 'cutReplacer': replacer for cut off value part when option value exceeds maxLength
*
* @return array
*/
public function getFormattedOptionValue($optionValue, $params = null)
{
// Init params
if (!$params) {
$params = array();
}
$maxLength = isset($params['max_length']) ? $params['max_length'] : null;
$cutReplacer = isset($params['cut_replacer']) ? $params['cut_replacer'] : '...';
// Proceed with option
$optionInfo = array();
// Define input data format
if (is_array($optionValue)) {
if (isset($optionValue['option_id'])) {
$optionInfo = $optionValue;
if (isset($optionInfo['value'])) {
$optionValue = $optionInfo['value'];
}
} else {
if (isset($optionValue['value'])) {
$optionValue = $optionValue['value'];
}
}
}
// Render customized option view
if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
$_default = array('value' => $optionValue);
if (isset($optionInfo['option_type'])) {
try {
$group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
return array('value' => $group->getCustomizedView($optionInfo));
} catch (\Exception $e) {
return $_default;
}
}
return $_default;
}
// Truncate standard view
if (is_array($optionValue)) {
$truncatedValue = implode("\n", $optionValue);
$truncatedValue = nl2br($truncatedValue);
return array('value' => $truncatedValue);
} else {
if ($maxLength) {
$truncatedValue = $this->filter->truncate($optionValue, array('length' => $maxLength, 'etc' => ''));
} else {
$truncatedValue = $optionValue;
}
$truncatedValue = nl2br($truncatedValue);
}
$result = array('value' => $truncatedValue);
if ($maxLength && $this->string->strlen($optionValue) > $maxLength) {
$result['value'] = $result['value'] . $cutReplacer;
$optionValue = nl2br($optionValue);
$result['full_view'] = $optionValue;
}
return $result;
}
示例9: validateUserValue
/**
* Validate user input for option
*
* @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...)
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function validateUserValue($values)
{
parent::validateUserValue($values);
$option = $this->getOption();
$value = trim($this->getUserValue());
// Check requires option to have some value
if (strlen($value) == 0 && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) {
$this->setIsValid(false);
throw new LocalizedException(__('Please specify the product\'s required option(s).'));
}
// Check maximal length limit
$maxCharacters = $option->getMaxCharacters();
if ($maxCharacters > 0 && $this->string->strlen($value) > $maxCharacters) {
$this->setIsValid(false);
throw new LocalizedException(__('The text is too long.'));
}
$this->setUserValue($value);
return $this;
}
示例10: filter
/**
* Filter value
*
* @param string $string
* @return string
*/
public function filter($string)
{
$length = $this->length;
$this->remainder = '';
if (0 == $length) {
return '';
}
$originalLength = $this->string->strlen($string);
if ($originalLength > $length) {
$length -= $this->string->strlen($this->etc);
if ($length <= 0) {
return '';
}
$preparedString = $string;
$preparedLength = $length;
if (!$this->breakWords) {
$preparedString = preg_replace('/\\s+?(\\S+)?$/u', '', $this->string->substr($string, 0, $length + 1));
$preparedLength = $this->string->strlen($preparedString);
}
$this->remainder = $this->string->substr($string, $preparedLength, $originalLength);
return $this->string->substr($preparedString, 0, $length) . $this->etc;
}
return $string;
}
示例11: getFormatedOptionValue
/**
* Accept option value and return its formatted view
*
* @param mixed $optionValue
* Method works well with these $optionValue format:
* 1. String
* 2. Indexed array e.g. array(val1, val2, ...)
* 3. Associative array, containing additional option info, including option value, e.g.
* array
* (
* [label] => ...,
* [value] => ...,
* [print_value] => ...,
* [option_id] => ...,
* [option_type] => ...,
* [custom_view] =>...,
* )
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getFormatedOptionValue($optionValue)
{
$optionInfo = [];
// define input data format
if (is_array($optionValue)) {
if (isset($optionValue['option_id'])) {
$optionInfo = $optionValue;
if (isset($optionInfo['value'])) {
$optionValue = $optionInfo['value'];
}
} elseif (isset($optionValue['value'])) {
$optionValue = $optionValue['value'];
}
}
// render customized option view
if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
$_default = ['value' => $optionValue];
if (isset($optionInfo['option_type'])) {
try {
$group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
return ['value' => $group->getCustomizedView($optionInfo)];
} catch (\Exception $e) {
return $_default;
}
}
return $_default;
}
// truncate standard view
$result = [];
if (is_array($optionValue)) {
$truncatedValue = implode("\n", $optionValue);
$truncatedValue = nl2br($truncatedValue);
return ['value' => $truncatedValue];
} else {
$truncatedValue = $this->filterManager->truncate($optionValue, ['length' => 55, 'etc' => '']);
$truncatedValue = nl2br($truncatedValue);
}
$result = ['value' => $truncatedValue];
if ($this->string->strlen($optionValue) > 55) {
$result['value'] = $result['value'] . ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>';
$optionValue = nl2br($optionValue);
$result = array_merge($result, ['full_view' => $optionValue]);
}
return $result;
}
示例12: _getItemsData
/**
* Get data array for building attribute filter items
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return array
*/
protected function _getItemsData()
{
$attribute = $this->getAttributeModel();
$this->_requestVar = $attribute->getAttributeCode();
$options = $attribute->getFrontend()->getSelectOptions();
$optionsCount = $this->_getResource()->getCount($this);
foreach ($options as $option) {
if (is_array($option['value'])) {
continue;
}
if ($this->string->strlen($option['value'])) {
// Check filter type
if ($this->getAttributeIsFilterable($attribute) == self::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS) {
if (!empty($optionsCount[$option['value']])) {
$this->itemDataBuilder->addItemData($this->tagFilter->filter($option['label']), $option['value'], $optionsCount[$option['value']]);
}
} else {
$this->itemDataBuilder->addItemData($this->tagFilter->filter($option['label']), $option['value'], isset($optionsCount[$option['value']]) ? $optionsCount[$option['value']] : 0);
}
}
}
return $this->itemDataBuilder->build();
}
示例13: _getItemsData
/**
* Get data array for building attribute filter items
*
* @return array
*/
protected function _getItemsData()
{
$attribute = $this->getAttributeModel();
$this->_requestVar = $attribute->getAttributeCode();
$options = $attribute->getFrontend()->getSelectOptions();
$optionsCount = $this->_getResource()->getCount($this);
$data = array();
foreach ($options as $option) {
if (is_array($option['value'])) {
continue;
}
if ($this->string->strlen($option['value'])) {
// Check filter type
if ($this->_getIsFilterableAttribute($attribute) == self::OPTIONS_ONLY_WITH_RESULTS) {
if (!empty($optionsCount[$option['value']])) {
$data[] = array('label' => $this->tagFilter->filter($option['label']), 'value' => $option['value'], 'count' => $optionsCount[$option['value']]);
}
} else {
$data[] = array('label' => $this->tagFilter->filter($option['label']), 'value' => $option['value'], 'count' => isset($optionsCount[$option['value']]) ? $optionsCount[$option['value']] : 0);
}
}
}
return $data;
}
示例14: isQueryTooLong
/**
* @param string $queryText
* @param int|string $maxQueryLength
* @return bool
*/
private function isQueryTooLong($queryText, $maxQueryLength)
{
return $maxQueryLength !== '' && $this->string->strlen($queryText) > $maxQueryLength;
}
示例15: isAttributeValid
/**
* Check one attribute can be overridden in child
*
* @param string $attributeCode Attribute code
* @param array $attributeParams Attribute params
* @param array $rowData Row data
* @param int $rowNumber
* @return bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function isAttributeValid($attributeCode, array $attributeParams, array $rowData, $rowNumber)
{
switch ($attributeParams['type']) {
case 'varchar':
$value = $this->string->cleanString($rowData[$attributeCode]);
$valid = $this->string->strlen($value) < self::DB_MAX_VARCHAR_LENGTH;
break;
case 'decimal':
$value = trim($rowData[$attributeCode]);
$valid = (double) $value == $value && is_numeric($value);
break;
case 'select':
case 'multiselect':
$valid = isset($attributeParams['options'][strtolower($rowData[$attributeCode])]);
break;
case 'int':
$value = trim($rowData[$attributeCode]);
$valid = (int) $value == $value && is_numeric($value);
break;
case 'datetime':
$value = trim($rowData[$attributeCode]);
$valid = strtotime($value) !== false;
break;
case 'text':
$value = $this->string->cleanString($rowData[$attributeCode]);
$valid = $this->string->strlen($value) < self::DB_MAX_TEXT_LENGTH;
break;
default:
$valid = true;
break;
}
if (!$valid) {
$this->addRowError(__("Please correct the value for '%s'."), $rowNumber, $attributeCode);
} elseif (!empty($attributeParams['is_unique'])) {
if (isset($this->_uniqueAttributes[$attributeCode][$rowData[$attributeCode]])) {
$this->addRowError(__("Duplicate Unique Attribute for '%s'"), $rowNumber, $attributeCode);
return false;
}
$this->_uniqueAttributes[$attributeCode][$rowData[$attributeCode]] = true;
}
return (bool) $valid;
}