-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Pushover package. | ||
* | ||
* (c) Serhiy Lunak <https://github.com/slunak> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Serhiy\Pushover\Api\Message; | ||
|
||
use Serhiy\Pushover\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Serhiy Lunak | ||
*/ | ||
class CustomSound | ||
{ | ||
/** | ||
* Custom Sound Notification | ||
* Admins can now upload custom sounds through Pushover website and have them played on devices | ||
* without having to copy sound files to each device. Can only contain letters, numbers, underscores, and dashes, | ||
* and is limited to 20 characters, such as "warning", "door_open", or "long_siren2". It cannot match the name | ||
* of any built-in sound and will be specified as the sound parameter through our API when requesting this sound. | ||
* | ||
* @var string | ||
*/ | ||
private $customSound; | ||
|
||
public function __construct(string $customSound) | ||
{ | ||
$this->setCustomSound($customSound); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getCustomSound(): string | ||
{ | ||
return $this->customSound; | ||
} | ||
|
||
/** | ||
* @param string $customSound | ||
*/ | ||
public function setCustomSound(string $customSound): void | ||
{ | ||
if (in_array($customSound, Sound::getAvailableSounds())) { | ||
throw new InvalidArgumentException(sprintf('Sound "%s" is not a valid custom sound because it matches the name of a built-in sound.', $customSound)); | ||
} | ||
|
||
if (1 !== preg_match("/^[a-zA-Z0-9_-]{1,20}$/", $customSound)) { | ||
throw new InvalidArgumentException(sprintf('Sound "%s" is not a valid custom sound. Custom sound name can only contain letters, numbers, underscores, and dashes, and is limited to 20 characters, such as "warning", "door_open", or "long_siren2".', $customSound)); | ||
} | ||
|
||
$this->customSound = $customSound; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Pushover package. | ||
* | ||
* (c) Serhiy Lunak <https://github.com/slunak> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Api\Message; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Serhiy\Pushover\Api\Message\CustomSound; | ||
use Serhiy\Pushover\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Serhiy Lunak | ||
*/ | ||
class CustomSoundTest extends TestCase | ||
{ | ||
public function testCanBeCreated() | ||
{ | ||
$this->assertInstanceOf(CustomSound::class, $customSound = new CustomSound("door_open")); | ||
|
||
return $customSound; | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param CustomSound $customSound | ||
*/ | ||
public function testGetCustomSound(CustomSound $customSound) | ||
{ | ||
$this->assertEquals('door_open', $customSound->getCustomSound()); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param CustomSound $customSound | ||
*/ | ||
public function testSetCustomSound(CustomSound $customSound) | ||
{ | ||
$customSound->setCustomSound("warning"); | ||
|
||
$this->assertEquals('warning', $customSound->getCustomSound()); | ||
|
||
$customSound->setCustomSound("door_open"); | ||
|
||
$this->assertEquals('door_open', $customSound->getCustomSound()); | ||
|
||
$customSound->setCustomSound("bell-sound"); | ||
|
||
$this->assertEquals('bell-sound', $customSound->getCustomSound()); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param CustomSound $customSound | ||
*/ | ||
public function testSetExistingCustomSound(CustomSound $customSound) | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$customSound->setCustomSound("echo"); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param CustomSound $customSound | ||
*/ | ||
public function testSetInvalidCustomSound(CustomSound $customSound) | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$customSound->setCustomSound("warning+door_open"); | ||
} | ||
|
||
/** | ||
* @depends testCanBeCreated | ||
* @param CustomSound $customSound | ||
*/ | ||
public function testSetLongCustomSound(CustomSound $customSound) | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$customSound->setCustomSound("warning_door_open_warning_door_open"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters