-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from lucadegasperi/master
Add Support For Critical Notification Sound
- Loading branch information
Showing
4 changed files
with
227 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Pushok package. | ||
* | ||
* (c) Arthur Edamov <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Pushok\Payload; | ||
|
||
/** | ||
* Class Sound | ||
* | ||
* @package Pushok\Payload | ||
* | ||
* @see http://bit.ly/payload-key-reference | ||
*/ | ||
class Sound implements \JsonSerializable | ||
{ | ||
const SOUND_CRITICAL_KEY = 'critical'; | ||
const SOUND_NAME_KEY = 'name'; | ||
const SOUND_VOLUME_KEY = 'volume'; | ||
|
||
/** | ||
* Whether the sound should be played as a critical notification or not | ||
* | ||
* @var integer | ||
*/ | ||
private $critical; | ||
|
||
/** | ||
* The sound file name. | ||
* | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* The sound volume. | ||
* | ||
* @var float | ||
*/ | ||
private $volume; | ||
|
||
protected function __construct() | ||
{ | ||
} | ||
|
||
public static function create() | ||
{ | ||
return new self(); | ||
} | ||
|
||
/** | ||
* Set Sound critical. | ||
* | ||
* @param int $value | ||
* @return Sound | ||
*/ | ||
public function setCritical(int $value): Sound | ||
{ | ||
$this->critical = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get Sound critical. | ||
* | ||
* @return string | ||
*/ | ||
public function getCritical() | ||
{ | ||
return $this->critical; | ||
} | ||
|
||
/** | ||
* Set Sound name. | ||
* | ||
* @param string $value | ||
* @return Sound | ||
*/ | ||
public function setName(string $value): Sound | ||
{ | ||
$this->name = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get Sound name. | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Set Sound volume. | ||
* | ||
* @param float $value | ||
* @return Sound | ||
*/ | ||
public function setVolume(float $value): Sound | ||
{ | ||
$this->volume = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get Sound volume. | ||
* | ||
* @return float | ||
*/ | ||
public function getVolume() | ||
{ | ||
return $this->volume; | ||
} | ||
|
||
/** | ||
* Convert Sound to JSON. | ||
* | ||
* @return string | ||
*/ | ||
public function toJson(): string | ||
{ | ||
return json_encode($this, JSON_UNESCAPED_UNICODE); | ||
} | ||
|
||
/** | ||
* Specify data which should be serialized to JSON. | ||
* | ||
* @return array | ||
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php | ||
*/ | ||
public function jsonSerialize() | ||
{ | ||
$sound = []; | ||
|
||
if (is_integer($this->critical)) { | ||
$sound[self::SOUND_CRITICAL_KEY] = $this->critical; | ||
} | ||
|
||
if (is_string($this->name)) { | ||
$sound[self::SOUND_NAME_KEY] = $this->name; | ||
} | ||
|
||
if (is_float($this->volume)) { | ||
$sound[self::SOUND_VOLUME_KEY] = $this->volume; | ||
} | ||
|
||
return $sound; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Pushok package. | ||
* | ||
* (c) Arthur Edamov <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Pushok\Tests\Payload; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Pushok\Payload\Sound; | ||
|
||
class SoundTest extends TestCase | ||
{ | ||
public function testSetCritical() | ||
{ | ||
$sound = Sound::create()->setCritical(1); | ||
|
||
$this->assertEquals(1, $sound->getCritical()); | ||
} | ||
|
||
public function testSetName() | ||
{ | ||
$sound = Sound::create()->setName('soundName'); | ||
|
||
$this->assertEquals('soundName', $sound->getName()); | ||
} | ||
|
||
public function testSetVolume() | ||
{ | ||
$sound = Sound::create()->setVolume(1.0); | ||
|
||
$this->assertEquals(1.0, $sound->getVolume()); | ||
} | ||
|
||
public function testSoundConvertingToJson() | ||
{ | ||
$sound = Sound::create() | ||
->setCritical(1) | ||
->setName('soundName') | ||
->setVolume(1.0); | ||
|
||
$this->assertJsonStringEqualsJsonString( | ||
'{"critical":1,"name":"soundName","volume":1.0}', | ||
$sound->toJson() | ||
); | ||
} | ||
} |
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