Skip to content

Commit 9bfa6c7

Browse files
author
Alex Westergaard
committed
Throw invalid name on starting or ending with "-"
1 parent eed5a5f commit 9bfa6c7

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Helper/EventHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function toArray(): array
7373
throw Ga4EventException::throwNameMissing();
7474
} elseif (strlen($name) > 40) {
7575
throw Ga4EventException::throwNameTooLong();
76-
} elseif (preg_match('/[^\w\d\-]/', $name)) {
76+
} elseif (preg_match('/[^\w\d\-]|^\-|\-$/', $name)) {
7777
throw Ga4EventException::throwNameInvalid();
7878
} elseif (in_array($name, EventType::RESERVED_NAMES) && !($this instanceof GtmEventType)) {
7979
throw Ga4EventException::throwNameReserved($name);

test/Unit/EventTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,42 @@ public function getName(): string
679679
$class->toArray();
680680
}
681681

682+
public function test_throw_name_invalid_starting_line()
683+
{
684+
$mock = new class extends Event\Refund
685+
{
686+
public function getName(): string
687+
{
688+
return '-almost-valid-name';
689+
}
690+
};
691+
692+
$class = $mock::new()->setTransactionId(1);
693+
694+
$this->expectException(Ga4EventException::class);
695+
$this->expectExceptionCode(Ga4Exception::EVENT_NAME_INVALID);
696+
697+
$class->toArray();
698+
}
699+
700+
public function test_throw_name_invalid_ending_line()
701+
{
702+
$mock = new class extends Event\Refund
703+
{
704+
public function getName(): string
705+
{
706+
return 'almost-valid-name-';
707+
}
708+
};
709+
710+
$class = $mock::new()->setTransactionId(1);
711+
712+
$this->expectException(Ga4EventException::class);
713+
$this->expectExceptionCode(Ga4Exception::EVENT_NAME_INVALID);
714+
715+
$class->toArray();
716+
}
717+
682718
public function test_throw_name_reserved()
683719
{
684720
$mock = new class extends Event\Refund

0 commit comments

Comments
 (0)