From a723c7e7ae0e329121ae3dfb2b66cf1d47f43cb4 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 31 Jan 2019 12:33:24 +0100 Subject: [PATCH] Cleaning by using the arcanedev/php-html --- composer.json | 11 +- src/NoCaptchaV2.php | 160 +++++++++++--------- src/NoCaptchaV3.php | 9 +- src/Utilities/Attributes.php | 279 ----------------------------------- tests/NoCaptchaV2Test.php | 73 ++++----- 5 files changed, 139 insertions(+), 393 deletions(-) delete mode 100644 src/Utilities/Attributes.php diff --git a/composer.json b/composer.json index 60ed79c..9c04965 100644 --- a/composer.json +++ b/composer.json @@ -14,11 +14,12 @@ "type": "library", "license": "MIT", "require": { - "php": ">=7.1.3", - "ext-curl": "*", - "ext-json": "*", - "arcanedev/support": "~4.4.0", - "psr/http-message": "~1.0" + "php": ">=7.1.3", + "ext-curl": "*", + "ext-json": "*", + "arcanedev/php-html": "~1.0.0", + "arcanedev/support": "~4.4.0", + "psr/http-message": "~1.0" }, "require-dev": { "arcanedev/laravel-html": "~5.7.0", diff --git a/src/NoCaptchaV2.php b/src/NoCaptchaV2.php index f66b3cd..93eb8db 100644 --- a/src/NoCaptchaV2.php +++ b/src/NoCaptchaV2.php @@ -1,6 +1,8 @@ setAttributes(new Attributes($attributes)); - } - /* ----------------------------------------------------------------- | Getters & Setters | ----------------------------------------------------------------- @@ -78,20 +53,6 @@ private function getScriptSrc($callbackName = null) return static::CLIENT_URL . (count($queries) ? '?' . http_build_query($queries) : ''); } - /** - * Set noCaptcha Attributes. - * - * @param \Arcanedev\NoCaptcha\Utilities\Attributes $attributes - * - * @return self - */ - public function setAttributes(Attributes $attributes) - { - $this->attributes = $attributes; - - return $this; - } - /* ----------------------------------------------------------------- | Main Methods | ----------------------------------------------------------------- @@ -103,16 +64,14 @@ public function setAttributes(Attributes $attributes) * @param string|null $name * @param array $attributes * - * @return \Illuminate\Support\HtmlString + * @return \Arcanedev\Html\Elements\Div */ public function display($name = null, array $attributes = []) { - $attributes = $this->attributes->build($this->siteKey, array_merge( - $this->attributes->prepareNameAttribute($name), - $attributes + return Div::make()->attributes(array_merge( + static::prepareNameAttribute($name), + $this->prepareAttributes($attributes) )); - - return $this->toHtmlString("
"); } /** @@ -121,13 +80,14 @@ public function display($name = null, array $attributes = []) * @param string|null $name * @param array $attributes * - * @return \Illuminate\Support\HtmlString + * @return \Arcanedev\Html\Elements\Div */ public function image($name = null, array $attributes = []) { - return $this->display( - $name, array_merge($attributes, $this->attributes->getImageAttribute()) - ); + return $this->display($name, array_merge( + $attributes, + ['data-type' => 'image'] + )); } /** @@ -136,31 +96,30 @@ public function image($name = null, array $attributes = []) * @param string|null $name * @param array $attributes * - * @return \Illuminate\Support\HtmlString + * @return \Arcanedev\Html\Elements\Div */ public function audio($name = null, array $attributes = []) { - return $this->display( - $name, array_merge($attributes, $this->attributes->getAudioAttribute()) - ); + return $this->display($name, array_merge( + $attributes, + ['data-type' => 'audio'] + )); } + /** * Display an invisible Captcha (bind the challenge to a button). * * @param string $value * @param array $attributes * - * @return \Illuminate\Support\HtmlString + * @return \Arcanedev\Html\Elements\Button */ public function button($value, array $attributes = []) { - $attributes = $this->attributes->build($this->siteKey, array_merge([ - 'data-callback' => 'onSubmit', - ], $attributes)); - - return $this->toHtmlString( - "" - ); + return Button::make()->text($value)->attributes(array_merge( + ['data-callback' => 'onSubmit'], + $this->prepareAttributes($attributes) + )); } /** @@ -243,14 +202,15 @@ public function scriptWithCallback(array $captchas, $callbackName = 'captchaRend $script = $this->script($callbackName)->toHtml(); if ( ! empty($script) && ! empty($captchas)) { - $script = implode(PHP_EOL, [implode(PHP_EOL, [ + $script = implode(PHP_EOL, [ $this->getApiScript()->toHtml(), '' - ]), $script]); + '', + $script, + ]); } return $this->toHtmlString($script); @@ -303,4 +263,70 @@ protected function parseResponse($json) { return ResponseV2::fromJson($json); } + + /** + * Prepare the attributes. + * + * @param array $attributes + * + * @return array + */ + private function prepareAttributes(array $attributes) + { + $attributes = array_merge( + ['class' => 'g-recaptcha', 'data-sitekey' => $this->siteKey], + array_filter($attributes) + ); + + self::checkDataAttribute($attributes, 'data-type', ['image', 'audio'], 'image'); + self::checkDataAttribute($attributes, 'data-theme', ['light', 'dark'], 'light'); + self::checkDataAttribute($attributes, 'data-size', ['normal', 'compact', 'invisible'], 'normal'); + self::checkDataAttribute($attributes, 'data-badge', ['bottomright', 'bottomleft', 'inline'], 'bottomright'); + + return $attributes; + } + + /** + * Check the `data-*` attribute. + * + * @param array $attributes + * @param string $name + * @param array $supported + * @param string $default + */ + private static function checkDataAttribute(array &$attributes, $name, array $supported, $default) + { + $attribute = $attributes[$name] ?? null; + + if ( ! is_null($attribute)) { + $attribute = (is_string($attribute) && in_array($attribute, $supported)) + ? strtolower(trim($attribute)) + : $default; + + $attributes[$name] = $attribute; + } + } + + /** + * Prepare the name and id attributes. + * + * @param string|null $name + * + * @return array + * + * @throws \Arcanedev\NoCaptcha\Exceptions\InvalidArgumentException + */ + protected static function prepareNameAttribute($name) + { + if (is_null($name)) + return []; + + if ($name === AbstractNoCaptcha::CAPTCHA_NAME) { + throw new InvalidArgumentException( + 'The captcha name must be different from "' . AbstractNoCaptcha::CAPTCHA_NAME . '".' + ); + } + + return array_combine(['id', 'name'], [$name, $name]); + } } diff --git a/src/NoCaptchaV3.php b/src/NoCaptchaV3.php index deb5e9a..72bd170 100644 --- a/src/NoCaptchaV3.php +++ b/src/NoCaptchaV3.php @@ -1,5 +1,6 @@ toHtmlString( - '' - ); + return Input::make()->type('hidden')->id($name)->name($name); } /** @@ -97,8 +96,6 @@ private function hasCallbackName($callbackName) return ! (is_null($callbackName) || trim($callbackName) === ''); } - - /* ----------------------------------------------------------------- | Other Methods | ----------------------------------------------------------------- diff --git a/src/Utilities/Attributes.php b/src/Utilities/Attributes.php deleted file mode 100644 index b47e200..0000000 --- a/src/Utilities/Attributes.php +++ /dev/null @@ -1,279 +0,0 @@ -defaults = array_filter($defaults); - } - - /* ----------------------------------------------------------------- - | Getters & Setters - | ----------------------------------------------------------------- - */ - - /** - * Get all items. - * - * @param string $siteKey - * - * @return array - */ - protected function getItems($siteKey) - { - return array_merge( - $this->getDefaultAttributes($siteKey), - $this->items - ); - } - - /** - * Get Default attributes. - * - * @param string $siteKey - * - * @return array - */ - private function getDefaultAttributes($siteKey) - { - return [ - 'class' => 'g-recaptcha', - 'data-sitekey' => $siteKey, - ]; - } - - /** - * Set items. - * - * @param array $items - * - * @return self - */ - private function setItems(array $items) - { - $this->items = array_merge($this->defaults, $items); - $this->checkAttributes(); - - return $this; - } - - /** - * Get an item value by name. - * - * @param string $name - * - * @return string - */ - private function getItem($name) - { - if ( ! $this->hasItem($name)) - return null; - - return $this->items[$name]; - } - - /** - * Set an item. - * - * @param string $name - * @param string $value - * - * @return self - */ - private function setItem($name, $value) - { - $this->items[$name] = $value; - return $this; - } - - /** - * Get image type attribute. - * - * @return array - */ - public function getImageAttribute() - { - return [self::ATTR_TYPE => 'image']; - } - - /** - * Get audio type attribute. - * - * @return array - */ - public function getAudioAttribute() - { - return [self::ATTR_TYPE => 'audio']; - } - - /* ------------------------------------------------------------------------------------------------ - | Main functions - | ------------------------------------------------------------------------------------------------ - */ - - /** - * Build attributes. - * - * @param string $siteKey - * @param array $items - * - * @return string - */ - public function build($siteKey, array $items = []) - { - $this->setItems($items); - - $output = []; - - foreach ($this->getItems($siteKey) as $key => $value) { - $output[] = trim($key) . '="' . trim($value) . '"'; - } - - return implode(' ', $output); - } - - /** - * Prepare the name and id attributes. - * - * @param string|null $name - * - * @return array - * - * @throws \Arcanedev\NoCaptcha\Exceptions\InvalidArgumentException - */ - public function prepareNameAttribute($name) - { - if (is_null($name)) - return []; - - if ($name === AbstractNoCaptcha::CAPTCHA_NAME) { - throw new InvalidArgumentException( - 'The captcha name must be different from "' . AbstractNoCaptcha::CAPTCHA_NAME . '".' - ); - } - - return array_combine(['id', 'name'], [$name, $name]); - } - - /* ------------------------------------------------------------------------------------------------ - | Check functions - | ------------------------------------------------------------------------------------------------ - */ - - /** - * Check attributes. - */ - private function checkAttributes() - { - $this->checkTypeAttribute(); - $this->checkThemeAttribute(); - $this->checkSizeAttribute(); - $this->checkBadgeAttribute(); - } - - /** - * Check type attribute. - */ - private function checkTypeAttribute() - { - $this->checkDataAttribute(self::ATTR_TYPE, 'image', ['image', 'audio']); - } - - /** - * Check theme attribute. - */ - private function checkThemeAttribute() - { - $this->checkDataAttribute(self::ATTR_THEME, 'light', ['light', 'dark']); - } - - /** - * Check size attribute. - */ - private function checkSizeAttribute() - { - $this->checkDataAttribute(self::ATTR_SIZE, 'normal', ['normal', 'compact', 'invisible']); - } - - /** - * Check badge attribute. - */ - private function checkBadgeAttribute() - { - $this->checkDataAttribute(self::ATTR_BADGE, 'bottomright', ['bottomright', 'bottomleft', 'inline']); - } - - /** - * Check data Attribute. - * - * @param string $name - * @param string $default - * @param array $available - */ - private function checkDataAttribute($name, $default, array $available) - { - $item = $this->getItem($name); - - if ( ! is_null($item)) { - $item = (is_string($item) && in_array($item, $available)) - ? strtolower(trim($item)) - : $default; - - $this->setItem($name, $item); - } - } - - /** - * Check if has an item. - * - * @param string $name - * - * @return bool - */ - private function hasItem($name) - { - return array_key_exists($name, $this->items); - } -} diff --git a/tests/NoCaptchaV2Test.php b/tests/NoCaptchaV2Test.php index 56e7695..cf7a0f8 100644 --- a/tests/NoCaptchaV2Test.php +++ b/tests/NoCaptchaV2Test.php @@ -61,11 +61,12 @@ public function it_can_be_instantiated_with_nullable_attributes() { $this->noCaptcha = $this->createCaptcha(null, [ 'data-theme' => null, - 'data-type' => null, - 'data-size' => null + 'data-type' => null, + 'data-size' => null ]); + static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha')->toHtml() ); } @@ -78,10 +79,10 @@ public function it_can_be_instantiated_with_nullable_attributes() * @param array $attributes * @param string $expected */ - public function it_can_be_instantiated_with_attributes(array $attributes, $expected) + public function it_can_display_with_custom_attributes(array $attributes, $expected) { static::assertSame( - $expected, $this->createCaptcha(null, $attributes)->display('captcha')->toHtml() + $expected, $this->createCaptcha()->display('captcha', $attributes)->toHtml() ); } @@ -93,39 +94,39 @@ public function provideNoCaptchaAttributes() return [ [ ['data-theme' => 'light'], - '
', + '
', ], [ ['data-theme' => 'dark'], - '
', + '
', ], [ ['data-theme' => 'transparent'], // Invalid - '
', + '
', ], [ ['data-type' => 'image'], - '
', + '
', ], [ ['data-type' => 'audio'], - '
', + '
', ], [ ['data-type' => 'video'], // Invalid - '
', + '
', ], [ ['data-size' => 'normal'], - '
', + '
', ], [ ['data-size' => 'compact'], - '
', + '
', ], [ ['data-size' => 'huge'], // Invalid - '
', + '
', ], ]; } @@ -275,17 +276,17 @@ public function it_can_render_script_with_callback() public function it_can_display_captcha() { static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha')->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', ['id' => 'captcha_1'])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', [ 'data-type' => 'image', 'data-theme' => 'light', @@ -293,7 +294,7 @@ public function it_can_display_captcha() ); static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', [ 'data-type' => 'audio', 'data-theme' => 'dark', @@ -305,22 +306,22 @@ public function it_can_display_captcha() public function it_can_display_image_captcha() { static::assertSame( - '
', + '
', $this->noCaptcha->image('captcha')->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->image('captcha', ['data-theme' => 'dark'])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->image('captcha', ['data-theme' => 'light'])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->image('captcha', [ 'data-theme' => 'light', 'data-type' => 'audio', // Intruder @@ -332,22 +333,22 @@ public function it_can_display_image_captcha() public function it_can_display_audio_captcha() { static::assertSame( - '
', + '
', $this->noCaptcha->audio('captcha')->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->audio('captcha', ['data-theme' => 'dark'])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->audio('captcha', ['data-theme' => 'light'])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->audio('captcha', [ 'data-theme' => 'light', 'data-type' => 'image', // Intruder @@ -359,22 +360,22 @@ public function it_can_display_audio_captcha() public function it_can_display_captcha_with_defaults() { static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', ['data-type' => 'video'])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', ['data-type' => true])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', ['data-theme' => 'blue'])->toHtml() ); static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', ['data-theme' => true])->toHtml() ); } @@ -383,7 +384,7 @@ public function it_can_display_captcha_with_defaults() public function it_can_display_captcha_with_style_attribute() { static::assertSame( - '
', + '
', $this->noCaptcha->display('captcha', [ 'style' => 'transform: scale(0.77); transform-origin: 0 0;' ])->toHtml() @@ -394,27 +395,27 @@ public function it_can_display_captcha_with_style_attribute() public function it_can_display_invisible_captcha() { static::assertSame( - '', + '', $this->noCaptcha->button('Send')->toHtml() ); static::assertSame( - '', + '', $this->noCaptcha->button('Post the form', ['data-callback' => 'submitForm'])->toHtml() ); static::assertSame( - '', + '', $this->noCaptcha->button('Send', ['data-size' => 'invisible'])->toHtml() ); static::assertSame( - '', + '', $this->noCaptcha->button('Send', ['data-badge' => 'bottomright'])->toHtml() ); static::assertSame( - '', + '', $this->noCaptcha->button('Send', ['data-badge' => 'topright'])->toHtml() ); }