Skip to content

Commit 790034a

Browse files
committed
Move honeypot to a helper function
1 parent 4d79b1b commit 790034a

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/Torann/LaravelRepository/ServiceProvider.php

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public function register() {}
1818
*/
1919
public function boot()
2020
{
21+
// Load helpers
22+
include __DIR__.'/../helpers.php';
23+
2124
$this->publishes([
2225
__DIR__.'/../../config/repositories.php' => config_path('repositories.php'),
2326
]);
@@ -71,38 +74,6 @@ public function setHoneypotValidator()
7174
'Torann\LaravelRepository\Extenders\HoneypotValidator@validate',
7275
$this->app->translator->get('validation.honeypot')
7376
);
74-
75-
// Add a custom honeypot macro to Laravel's forms
76-
$this->app->form->macro('honeypot', function($honey_name)
77-
{
78-
// Create element ID
79-
$honey_id = $this->slugify($honey_name);
80-
81-
return "<div id=\"{$honey_id}_wrap\" style=\"display:none;\">\n<input id=\"{$honey_id}\" name=\"{$honey_name}\" type=\"text\" value=\"\">\n</div>";
82-
});
8377
}
8478

85-
/**
86-
* Create an ID for the honeypot HTML element
87-
*
88-
* @param string $text
89-
* @return string
90-
*/
91-
public function slugify($text)
92-
{
93-
// replace non letter or digits by _
94-
$text = preg_replace('~[^\\pL\d]+~u', '_', $text);
95-
96-
// trim
97-
$text = trim($text, '_');
98-
99-
// lowercase
100-
$text = strtolower($text);
101-
102-
// remove unwanted characters
103-
$text = preg_replace('~[^-\w]+~', '', $text);
104-
105-
// return lowercase
106-
return $text;
107-
}
10879
}

src/Torann/helpers.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
if ( ! function_exists('honeypot'))
4+
{
5+
/**
6+
* Generate honeypot form input.
7+
*
8+
* @param string $honey_name
9+
* @return string
10+
*/
11+
function honeypot($honey_name)
12+
{
13+
// Create element ID
14+
$honey_id = preg_replace('~[^-\w]+~', '', trim(preg_replace('~[^\\pL\d]+~u', '_', $honey_name), '_'));
15+
16+
return "<div id=\"{$honey_id}_wrap\" style=\"display:none;\">\n<input id=\"{$honey_id}\" name=\"{$honey_name}\" type=\"text\" value=\"\">\n</div>";
17+
}
18+
}

0 commit comments

Comments
 (0)