Пример #1
0
 /**
  * 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
0
 /**
  * {@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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * @param string $queryText
  * @param int|string $maxQueryLength
  * @return bool
  */
 private function isQueryTooLong($queryText, $maxQueryLength)
 {
     return $maxQueryLength !== '' && $this->string->strlen($queryText) > $maxQueryLength;
 }
Пример #15
0
 /**
  * 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;
 }
Пример #16
0
 /**
  * Make sure that password complies with minimum security requirements.
  *
  * @param string $password
  * @return void
  * @throws InputException
  */
 protected function checkPasswordStrength($password)
 {
     $length = $this->stringHelper->strlen($password);
     if ($length < self::MIN_PASSWORD_LENGTH) {
         throw new InputException(__('The password must have at least %1 characters.', self::MIN_PASSWORD_LENGTH));
     }
     if ($this->stringHelper->strlen(trim($password)) != $length) {
         throw new InputException(__('The password can not begin or end with a space.'));
     }
 }
Пример #17
0
 /**
  * Get formatted order description
  *
  * @param \Magento\Sales\Model\Order $order
  * @return string
  */
 protected function _getOrderDescription($order)
 {
     $invoiceDesc = '';
     foreach ($order->getAllItems() as $item) {
         if ($item->getParentItem()) {
             continue;
         }
         //COM filed can only handle max 100
         if ($this->string->strlen($invoiceDesc . $item->getName()) > 100) {
             break;
         }
         $invoiceDesc .= $item->getName() . ', ';
     }
     return $this->string->substr($invoiceDesc, 0, -2);
 }