Skip to content

Commit

Permalink
bumped to php 8.1
Browse files Browse the repository at this point in the history
added ajax validation reload if wrong
  • Loading branch information
simialbi committed Mar 22, 2024
1 parent 2ad584c commit a0ab55f
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
],
"require": {
"php": ">=7.3",
"php": ">=8.1",
"yiisoft/yii2": "^2.0.15",
"simialbi/yii2-simialbi-base": ">=0.13.1 <1.0 | ^1.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Module extends \simialbi\yii2\base\Module implements BootstrapInterface
* {@inheritdoc}
* @throws \yii\base\InvalidConfigException
*/
public function init()
public function init(): void
{
parent::init();

Expand All @@ -51,7 +51,7 @@ public function getCaptcha(): ?components\Captcha
/**
* {@inheritdoc}
*/
public function bootstrap($app)
public function bootstrap($app): void
{
$app->setAliases([
'@visualcaptcha' => __DIR__
Expand Down
24 changes: 10 additions & 14 deletions src/components/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Captcha extends Component
* @var array All the image options. These can be easily overwritten or extended using
* addImageOptions( <Array> ), or replaceImageOptions( <Array> ).
*/
public $imageOptions = [
public array $imageOptions = [
[
'name' => 'Airplane',
'path' => '@visualcaptcha/assets/images/airplane.png'
Expand Down Expand Up @@ -267,20 +267,20 @@ class Captcha extends Component
/**
* @var string
*/
public $sessionPrefix = 'visualcaptcha_';
public string $sessionPrefix = 'visualcaptcha_';

/**
* @var string the value of the parameter sent to the server for the namespace, if it's not set up,
* no namespace will be sent
*/
private $_namespace;
private string $_namespace;

/**
* Generate a new valid option
* @param integer $numberOfOptions Number of options. This parameter is optional. Defaults to 5.
* @throws \Exception
*/
public function generate(int $numberOfOptions = 5)
public function generate(int $numberOfOptions = 5): void
{
$imageValues = [];

Expand All @@ -292,11 +292,7 @@ public function generate(int $numberOfOptions = 5)
$this->validAudioOption = null;
$this->sessionImageOptions = null;

$numberOfOptions = intval($numberOfOptions);

if ($numberOfOptions < 4) {
$numberOfOptions = 4;
}
$numberOfOptions = max(4, $numberOfOptions);

shuffle($this->imageOptions);

Expand Down Expand Up @@ -394,7 +390,7 @@ public function getFrontendData(): ?array
* Set data to be used by the frontend
* @param array|null $frontendData
*/
public function setFrontendData(?array $frontendData)
public function setFrontendData(?array $frontendData): void
{
Yii::$app->session->set($this->sessionPrefix . 'frontendData', $frontendData);
}
Expand All @@ -415,7 +411,7 @@ public function setNamespace(?string $namespace)
{
$this->_namespace = $namespace;

if ($pos = (strpos($this->sessionPrefix, '/') !== false)) {
if ($pos = (str_contains($this->sessionPrefix, '/'))) {
$this->sessionPrefix = substr($this->sessionPrefix, $pos + 1);
}
if (!empty($namespace)) {
Expand All @@ -436,7 +432,7 @@ public function getValidImageOption(): ?array
* Set the current validImageOption
* @param array|null $validImageOption
*/
public function setValidImageOption(?array $validImageOption)
public function setValidImageOption(?array $validImageOption): void
{
Yii::$app->session->set($this->sessionPrefix . 'validImageOption', $validImageOption);
}
Expand All @@ -454,7 +450,7 @@ public function getValidAudioOption(): ?array
* Get the current validAudioOption
* @param array|null $validAudioOption
*/
public function setValidAudioOption(?array $validAudioOption)
public function setValidAudioOption(?array $validAudioOption): void
{
Yii::$app->session->set($this->sessionPrefix . 'validAudioOption', $validAudioOption);
}
Expand All @@ -472,7 +468,7 @@ public function getSessionImageOptions(): ?array
* Set generated image options
* @param array|null $images
*/
public function setSessionImageOptions(?array $images)
public function setSessionImageOptions(?array $images): void
{
Yii::$app->session->set($this->sessionPrefix . 'images', $images);
}
Expand Down
Binary file modified src/messages/de/messages.mo
Binary file not shown.
10 changes: 10 additions & 0 deletions src/messages/de/messages.po
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"PO-Revision-Date: 2024-03-22 14:20+0100\n"
"Last-Translator: Simon Karlen <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: widgets/VisualCaptcha.php
msgctxt "simialbi/visualcaptcha/input-widget"
msgid "Sound icon"
Expand Down
Binary file modified src/messages/en/messages.mo
Binary file not shown.
10 changes: 10 additions & 0 deletions src/messages/en/messages.po
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"PO-Revision-Date: 2024-03-22 14:20+0100\n"
"Last-Translator: Simon Karlen <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: widgets/VisualCaptcha.php
msgctxt "simialbi/visualcaptcha/input-widget"
msgid "Sound icon"
Expand Down
Binary file modified src/messages/fr/messages.mo
Binary file not shown.
12 changes: 11 additions & 1 deletion src/messages/fr/messages.po
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"PO-Revision-Date: 2024-03-22 14:20+0100\n"
"Last-Translator: Simon Karlen <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: widgets/VisualCaptcha.php
msgctxt "simialbi/visualcaptcha/input-widget"
msgid "Sound icon"
Expand Down Expand Up @@ -29,7 +39,7 @@ msgid "Refresh/reload: get new images and accessibility option!"
msgstr "Mettre à jour"

#: widgets/VisualCaptcha.php
msgctxt "simialbi/visualcaptcha/input-widget"
msgctxt "simialbi/visualcaptcha/names"
msgid "Airplane"
msgstr "Avion"

Expand Down
32 changes: 25 additions & 7 deletions src/validators/VisualCaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class VisualCaptchaValidator extends Validator
* @var string module ID (case-sensitive). To retrieve grand child modules,
* use ID path relative to this module (e.g. `admin/content`).
*/
public $moduleId = 'visualcaptcha';
public string $moduleId = 'visualcaptcha';

/**
* {@inheritdoc}
*/
public function init()
public function init(): void
{
parent::init();
if ($this->message === null) {
Expand All @@ -44,7 +44,7 @@ public function init()
* {@inheritdoc}
* @throws \yii\base\NotSupportedException
*/
public function validateAttribute($model, $attribute)
public function validateAttribute($model, $attribute): void
{
$namespace = Yii::$app->request->getBodyParam('namespace', Html::getInputName($model, $attribute));
$namespaceString = str_replace([Html::getInputName($model, $attribute), '[', ']'], '', $namespace);
Expand All @@ -55,6 +55,21 @@ public function validateAttribute($model, $attribute)
}
}

/**
* {@inheritDoc}
*/
public function clientValidateAttribute($model, $attribute, $view): string
{
return <<<JS
var that = this;
\$form.on('ajaxComplete', function (e, jqXHR) {
if (jqXHR.responseJSON[that.id]) {
jQuery(that.input).data('captcha').refresh();
}
});
JS;
}

/**
* {@inheritdoc}
* @param string|null $namespace string the value of the parameter sent to the server for the namespace,
Expand All @@ -75,11 +90,14 @@ protected function validateValue($value, ?string $namespace = null): ?array
ArrayHelper::getValue($this->captcha->frontendData, 'audioFieldName', '')
), '[]');

if ($imageFieldName && $val = ArrayHelper::getValue($value, $imageFieldName)) {
return $this->captcha->validateImage($val) ? null : [$this->message, []];
} elseif ($audioFieldName && $val = ArrayHelper::getValue($value, $audioFieldName)) {
return $this->captcha->validateAudio($val) ? null : [$this->message, []];
$result = $imageFieldName
? $this->captcha->validateImage(ArrayHelper::getValue($value, $imageFieldName, ''))
: $this->captcha->validateAudio(ArrayHelper::getValue($value, $audioFieldName, ''));

if ($result) {
return null;
}

return [$this->message, []];
}

Expand Down
10 changes: 4 additions & 6 deletions src/widgets/VisualCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class VisualCaptcha extends InputWidget
* @var string module ID (case-sensitive). To retrieve grand child modules,
* use ID path relative to this module (e.g. `admin/content`).
*/
public $moduleId = 'visualcaptcha';
public string $moduleId = 'visualcaptcha';

/**
* @var integer number of generated image options for visualCaptcha
*/
public $numberOfImages = 6;
public int $numberOfImages = 6;

/**
* @var string path to the following interface icons:
Expand All @@ -33,13 +33,13 @@ class VisualCaptcha extends InputWidget
* * refresh.png
* * [email protected]
*/
public $imgPath = '@web/img/';
public string $imgPath = '@web/img/';

/**
* @var string the value of the parameter sent to the server for the namespace, if it's not set up,
* it will be auto generated
*/
public $namespace;
public string $namespace;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -104,8 +104,6 @@ public function init(): void
*/
public function run(): string
{
parent::run();

$options = $this->options;
Html::removeCssClass($options, 'form-control');

Expand Down

0 comments on commit a0ab55f

Please sign in to comment.