Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #117 from ivebe/sanitize-class-name
Browse files Browse the repository at this point in the history
Sanitize class name

Closes #116
  • Loading branch information
ReeceM authored Oct 2, 2020
2 parents fb798a5 + c8794e8 commit 9bcf74f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_Store
/vendor
/composer.lock
.phpunit.result.cache
# exclude everything
resources/views/templates/*
resources/views/draft/*
Expand Down
46 changes: 45 additions & 1 deletion src/MailEclipse.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,14 @@ public static function getMailableTemplateData($mailableName)

public static function generateMailable($request = null)
{
$name = ucwords(Str::camel(preg_replace('/\s+/', '_', $request->input('name'))));
$name = self::generateClassName($request->input('name'));

if ($name === false) {
return response()->json([
'status' => 'error',
'message' => 'Wrong name format.',
]);
}

if (! self::getMailable('name', $name)->isEmpty() && ! $request->has('force')) {
// return redirect()->route('createMailable')->with('error', 'mailable already exists! to overide it enable force option.');
Expand Down Expand Up @@ -827,4 +834,41 @@ public static function renderPreview($simpleview, $view, $template = false, $ins
return $error;
}
}

/**
* Class name has to satisfy those rules.
*
* https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class
* https://www.php.net/manual/en/reserved.keywords.php
*
* @param $input
* @return string|false class name or false on failure
*/
public static function generateClassName($input)
{
$suffix = 'Mail';

if (strtolower($input) === strtolower($suffix)) {
return false;
}

// Avoid MailMail as a class name suffix
if (substr_compare(strtolower($input), 'mail', -4) === 0) {
$suffix = '';
}

/**
* - Suffix is needed to avoid usage of reserved word.
* - Str::slug will remove all forbidden characters.
*/
$name = ucwords(Str::camel(Str::slug($input, '_'))).$suffix;

if (! preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $name) ||
substr_compare($name, $suffix, -strlen($suffix), strlen($suffix), true) !== 0
) {
return false;
}

return $name;
}
}
32 changes: 32 additions & 0 deletions tests/Unit/ClassNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use Qoraiche\MailEclipse\MailEclipse;

class ClassNameTest extends TestCase
{
public function testValidClassName()
{
$expectedMap = [
'mail' => false,
'1 Number' => false,
'Number 1' => 'Number1Mail',
'Welcome #1 User' => 'Welcome1UserMail',
'Welcome User' => 'WelcomeUserMail',
'null' => 'NullMail',
'_null' => 'NullMail',
'#null' => 'NullMail',
'CustomerMail' => 'Customermail',
'Customermail' => 'Customermail',
'Customer Mail' => 'CustomerMail',
'customer mail' => 'CustomerMail',
];

foreach ($expectedMap as $input => $expected) {
$className = MailEclipse::generateClassName($input);
$this->assertEquals($expected, $className);
}
}
}

0 comments on commit 9bcf74f

Please sign in to comment.