Skip to content

Commit

Permalink
Cleaning by using the arcanedev/php-html
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Jan 31, 2019
1 parent 0b3f53c commit a723c7e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 393 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
160 changes: 93 additions & 67 deletions src/NoCaptchaV2.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace Arcanedev\NoCaptcha;

use Arcanedev\NoCaptcha\Utilities\Attributes;
use Arcanedev\Html\Elements\Button;
use Arcanedev\Html\Elements\Div;
use Arcanedev\NoCaptcha\Exceptions\InvalidArgumentException;
use Arcanedev\NoCaptcha\Utilities\ResponseV2;
use Illuminate\Support\Arr;

Expand All @@ -24,33 +26,6 @@ class NoCaptchaV2 extends AbstractNoCaptcha
*/
protected $scriptLoaded = false;

/**
* noCaptcha Attributes
*
* @var \Arcanedev\NoCaptcha\Utilities\Attributes
*/
protected $attributes;

/* -----------------------------------------------------------------
| Constructor
| -----------------------------------------------------------------
*/

/**
* NoCaptcha constructor.
*
* @param string $secret
* @param string $siteKey
* @param string|null $lang
* @param array $attributes
*/
public function __construct($secret, $siteKey, $lang = null, array $attributes = [])
{
parent::__construct($secret, $siteKey, $lang);

$this->setAttributes(new Attributes($attributes));
}

/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
Expand Down Expand Up @@ -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
| -----------------------------------------------------------------
Expand All @@ -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("<div {$attributes}></div>");
}

/**
Expand All @@ -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']
));
}

/**
Expand All @@ -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(
"<button {$attributes}>{$value}</button>"
);
return Button::make()->text($value)->attributes(array_merge(
['data-callback' => 'onSubmit'],
$this->prepareAttributes($attributes)
));
}

/**
Expand Down Expand Up @@ -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>',
"var $callbackName = function() {",
$this->renderCaptchas($captchas),
'};',
'</script>'
]), $script]);
'</script>',
$script,
]);
}

return $this->toHtmlString($script);
Expand Down Expand Up @@ -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]);
}
}
9 changes: 3 additions & 6 deletions src/NoCaptchaV3.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Arcanedev\NoCaptcha;

use Arcanedev\Html\Elements\Input;
use Arcanedev\NoCaptcha\Utilities\ResponseV3;
use Illuminate\Support\Arr;

Expand Down Expand Up @@ -31,13 +32,11 @@ class NoCaptchaV3 extends AbstractNoCaptcha
/**
* @param string $name
*
* @return \Illuminate\Support\HtmlString
* @return \Arcanedev\Html\Elements\Input
*/
public function input($name = 'g-recaptcha-response')
{
return $this->toHtmlString(
'<input type="hidden" id="'.$name.'" name="'.$name.'">'
);
return Input::make()->type('hidden')->id($name)->name($name);
}

/**
Expand Down Expand Up @@ -97,8 +96,6 @@ private function hasCallbackName($callbackName)
return ! (is_null($callbackName) || trim($callbackName) === '');
}



/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
Expand Down
Loading

0 comments on commit a723c7e

Please sign in to comment.