forked from sbodak/magento2-checkout-custom-form
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from iMi-digital/feature/configurable-input-len…
…gth-validation Add configurable input length validation
- Loading branch information
Showing
6 changed files
with
264 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Bodak\CheckoutCustomForm\Helper; | ||
|
||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Config | ||
{ | ||
const LIMIT_NOT_SET = -1; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* Config constructor. | ||
* | ||
* @param ScopeConfigInterface $config | ||
* @param StoreManagerInterface $storeManager | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
ScopeConfigInterface $config, | ||
StoreManagerInterface $storeManager, | ||
LoggerInterface $logger | ||
) { | ||
$this->config = $config; | ||
$this->storeManager = $storeManager; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getEnabledFields() | ||
{ | ||
return explode(',', | ||
$this->config->getValue('bodak/checkout/enabled_fields', | ||
ScopeInterface::SCOPE_STORE)); | ||
} | ||
|
||
/** | ||
* @param $attribute | ||
* | ||
* @return int | ||
*/ | ||
public function getAllowedLength($attribute) | ||
{ | ||
$configPath = 'checkout/general/' . $attribute . '_limit'; | ||
try { | ||
$allowedLength = $this->config->getValue($configPath, ScopeInterface::SCOPE_STORES, | ||
$this->storeManager->getStore()->getId()); | ||
} catch (NoSuchEntityException $e) { | ||
$this->logger->error('Cannot get allowed length for custom field. Falling back to default scope value.'); | ||
$this->logger->error($e->getMessage(), ['exception' => $e]); | ||
$allowedLength = $this->config->getValue($configPath); | ||
} | ||
|
||
return (int)$allowedLength ?: self::LIMIT_NOT_SET; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Bodak\CheckoutCustomForm\Model\CustomFields; | ||
|
||
use Bodak\CheckoutCustomForm\Api\Data\CustomFieldsInterface; | ||
use Bodak\CheckoutCustomForm\Helper\Config; | ||
use Magento\Framework\Validator\AbstractValidator; | ||
use Zend_Validate_Exception; | ||
use Zend\Filter\Word\UnderscoreToCamelCase; | ||
|
||
|
||
class Validator extends AbstractValidator | ||
{ | ||
|
||
/** | ||
* @var CustomFieldsInterface | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var UnderscoreToCamelCase | ||
*/ | ||
private $underscoreToCamelCase; | ||
|
||
/** | ||
* Validator constructor. | ||
* | ||
* @param Config $config | ||
* @param UnderscoreToCamelCase $underscoreToCamelCase | ||
*/ | ||
public function __construct(Config $config, UnderscoreToCamelCase $underscoreToCamelCase) | ||
{ | ||
$this->config = $config; | ||
$this->underscoreToCamelCase = $underscoreToCamelCase; | ||
} | ||
|
||
/** | ||
* Returns true if and only if $value meets the validation requirements | ||
* | ||
* If $value fails validation, then this method returns false, and | ||
* getMessages() will return an array of messages that explain why the | ||
* validation failed. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return boolean | ||
* @throws Zend_Validate_Exception If validation of $value is impossible | ||
*/ | ||
public function isValid($value) | ||
{ | ||
$valid = true; | ||
if (!($value instanceof CustomFieldsInterface)) { | ||
throw new Zend_Validate_Exception('Expected value to be instance of \Bodak\CheckoutCustomForm\Api\Data\CustomFieldsInterface'); | ||
} | ||
|
||
$this->setValue($value); | ||
|
||
foreach (CustomFieldsInterface::ATTRIBUTES as $attribute) { | ||
if (!$this->lengthIsValid($attribute)) { | ||
$valid = false; | ||
$this->_addMessages([sprintf('Field %s is to long.', $attribute)]); | ||
} | ||
} | ||
|
||
return $valid; | ||
} | ||
|
||
/** | ||
* @param $value | ||
*/ | ||
private function setValue($value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @param $attribute | ||
* | ||
* @return bool | ||
*/ | ||
private function lengthIsValid($attribute) | ||
{ | ||
$allowedLength = $this->config->getAllowedLength($attribute); | ||
|
||
if ($allowedLength === Config::LIMIT_NOT_SET) { | ||
return true; | ||
} | ||
|
||
$function = $this->underscoreToCamelCase->filter('get_' . $attribute); | ||
$value = call_user_func([$this->value, $function]); | ||
|
||
return mb_strlen($value) <= $allowedLength; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.