diff --git a/src/Firebase/Messaging/WebPushConfig.php b/src/Firebase/Messaging/WebPushConfig.php index 55204433..1e90fc3d 100644 --- a/src/Firebase/Messaging/WebPushConfig.php +++ b/src/Firebase/Messaging/WebPushConfig.php @@ -10,7 +10,7 @@ /** * @see https://tools.ietf.org/html/rfc8030#section-5.3 Web Push Message Urgency * @phpstan-type WebPushHeadersShape array{ - * TTL?: positive-int|numeric-string, + * TTL?: positive-int|numeric-string|null, * Urgency?: self::URGENCY_* * } * @@ -91,6 +91,10 @@ public static function fromArray(array $config): self { if (array_key_exists('headers', $config) && is_array($config['headers'])) { $config['headers'] = self::ensureValidHeaders($config['headers']); + + if ($config['headers'] === []) { + unset($config['headers']); + } } return new self($config); @@ -103,16 +107,18 @@ public static function fromArray(array $config): self */ private static function ensureValidHeaders(array $headers): array { - if (array_key_exists('TTL', $headers) && is_int($headers['TTL'])) { - $headers['TTL'] = (string) $headers['TTL']; - } + if (array_key_exists('TTL', $headers)) { + if (is_int($headers['TTL'])) { + $headers['TTL'] = (string) $headers['TTL']; + } - if ( - array_key_exists('TTL', $headers) - && is_string($headers['TTL']) - && preg_match('/^[\-0]/', $headers['TTL']) === 1 - ) { - throw new InvalidArgument('The TTL in the WebPushConfig must must be a positive int'); + if (is_string($headers['TTL']) && (preg_match('/^[1-9]\d*$/', $headers['TTL']) !== 1)) { + throw new InvalidArgument('The TTL in the WebPushConfig must must be a positive int'); + } + + if ($headers['TTL'] === null) { + unset($headers['TTL']); + } } if (array_key_exists('Urgency', $headers)) { diff --git a/tests/Unit/Messaging/WebPushConfigTest.php b/tests/Unit/Messaging/WebPushConfigTest.php index 160f51f3..2672f4cd 100644 --- a/tests/Unit/Messaging/WebPushConfigTest.php +++ b/tests/Unit/Messaging/WebPushConfigTest.php @@ -42,6 +42,18 @@ public function testItCanHaveAPriority(): void $this->assertSame('high', $config->jsonSerialize()['headers']['Urgency']); } + /** + * @dataProvider validHeaders + * + * @param WebPushHeadersShape $headers + */ + public function testItAcceptsValidHeaders(array $headers): void + { + WebPushConfig::fromArray(['headers' => $headers]); + + $this->addToAssertionCount(1); + } + /** * @dataProvider invalidHeaders * @@ -82,6 +94,7 @@ public function validHeaders(): array return [ 'positive int ttl' => [['TTL' => 1]], 'positive string ttl' => [['TTL' => '1']], + 'null (#719)' => [['TTL' => null]], ]; } @@ -93,8 +106,8 @@ public function invalidHeaders(): array return [ 'negative int ttl' => [['TTL' => -1]], 'negative string ttl' => [['TTL' => '-1']], - 'zero int ttl' => [['TTL' => -1]], - 'zero string ttl' => [['TTL' => '-1']], + 'zero int ttl' => [['TTL' => 0]], + 'zero string ttl' => [['TTL' => '0']], 'unsupported urgency' => [['Urgency' => 'unsupported']], ]; }