Skip to content

Commit 3acb165

Browse files
committed
extracted validateName to ErrorTrait
1 parent e224d3e commit 3acb165

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

src/Traits/ErrorTrait.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Traits;
2323

24+
use InvalidArgumentException;
2425
use ReflectionClass;
2526
use TypeError;
2627
use function gettype;
2728
use function is_array;
29+
use function is_numeric;
30+
use function strlen;
31+
use function trim;
2832

2933
/**
3034
* Convenience trait including simple assertions and error reporting functions
@@ -154,4 +158,34 @@ private function isType($types, $userInput): bool
154158

155159
return false;
156160
}
161+
162+
/**
163+
* Validates the name to see if it can be used as a parameter or variable.
164+
*
165+
* @see https://neo4j.com/docs/cypher-manual/current/syntax/naming/#_naming_rules
166+
*
167+
* @param string $name
168+
*
169+
* @return void
170+
*/
171+
private static function validateName(string $name): void
172+
{
173+
$name = trim($name);
174+
175+
if ($name === "") {
176+
throw new InvalidArgumentException("A name cannot be an empty string");
177+
}
178+
179+
if (!ctype_alnum($name)) {
180+
throw new InvalidArgumentException('A name can only contain alphanumeric characters');
181+
}
182+
183+
if (is_numeric($name[0])) {
184+
throw new InvalidArgumentException('A name cannot begin with a numeric character');
185+
}
186+
187+
if (strlen($name) >= 65535) {
188+
throw new InvalidArgumentException('A name cannot be longer than 65534 characters');
189+
}
190+
}
157191
}

src/Traits/HasNameTrait.php

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
trait HasNameTrait
1212
{
13-
public static function automaticVariableLength(): int {
13+
public static function automaticVariableLength(): int
14+
{
1415
return 32;
1516
}
1617

@@ -26,40 +27,10 @@ public static function automaticVariableLength(): int {
2627
*
2728
* @return string
2829
*/
29-
public static function generateName(string $prefix = 'var', int $length = null): string
30+
private static function generateName(string $prefix = 'var', int $length = null): string
3031
{
3132
$length ??= self::automaticVariableLength();
3233

3334
return $prefix . substr(bin2hex(openssl_random_pseudo_bytes(ceil($length / 2))), 0, $length);
3435
}
35-
36-
/**
37-
* Validates the name to see if it can be used as a parameter or variable.
38-
*
39-
* @see https://neo4j.com/docs/cypher-manual/current/syntax/naming/#_naming_rules
40-
*
41-
* @param string $name
42-
*
43-
* @return void
44-
*/
45-
public static function validateName(string $name): void
46-
{
47-
$name = trim($name);
48-
49-
if ($name === "") {
50-
throw new InvalidArgumentException("A name cannot be an empty string");
51-
}
52-
53-
if(!ctype_alnum($name)) {
54-
throw new InvalidArgumentException('A name can only contain alphanumeric characters');
55-
}
56-
57-
if (is_numeric($name[0])) {
58-
throw new InvalidArgumentException('A name cannot begin with a numeric character');
59-
}
60-
61-
if (strlen($name) >= 65535) {
62-
throw new InvalidArgumentException('A name cannot be longer than 65534 characters');
63-
}
64-
}
6536
}

0 commit comments

Comments
 (0)