-
Notifications
You must be signed in to change notification settings - Fork 46
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
5 changed files
with
196 additions
and
11 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
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,85 @@ | ||
<?php | ||
|
||
namespace Tgallice\FBMessenger\Model\ThreadSetting; | ||
|
||
use Tgallice\FBMessenger\Model\ThreadSetting; | ||
|
||
class DomainWhitelisting implements ThreadSetting, \JsonSerializable | ||
{ | ||
const TYPE_ADD = 'add'; | ||
const TYPE_REMOVE = 'remove'; | ||
|
||
const WHITELISTED_DOMAINS = 'whitelisted_domains'; | ||
const DOMAIN_ACTION_TYPE = 'domain_action_type'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $action; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $domains; | ||
|
||
/** | ||
* | ||
* @param string $action | ||
* @param array $domains | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct($domains, $action = DomainWhitelisting::TYPE_ADD) | ||
{ | ||
self::validateAction($action); | ||
self::validateDomains($domains); | ||
|
||
$this->action = $action; | ||
$this->domains = $domains; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $action | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function validateAction($action) | ||
{ | ||
if(!in_array($action, [DomainWhitelisting::TYPE_ADD, DomainWhitelisting::TYPE_REMOVE])) { | ||
throw new \InvalidArgumentException('The action must be type: "add" or "remove".'); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param array $domains | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function validateDomains($domains) | ||
{ | ||
if(!is_array($domains)) { | ||
throw new \InvalidArgumentException('$domains must be a array.'); | ||
} | ||
|
||
//https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting | ||
foreach($domains as $domain) { | ||
if(!preg_match('#^https:\/\/#', $domain)) { | ||
throw new \InvalidArgumentException('Each domain must be a "https" protocol.'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function jsonSerialize() | ||
{ | ||
return [ | ||
DomainWhitelisting::WHITELISTED_DOMAINS => $this->domains, | ||
DomainWhitelisting::DOMAIN_ACTION_TYPE => $this->action | ||
]; | ||
} | ||
} | ||
|
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,41 @@ | ||
<?php | ||
|
||
namespace Tgallice\FBMessenger\Model; | ||
|
||
use Tgallice\FBMessenger\Model\ThreadSetting\DomainWhitelisting; | ||
|
||
class WhitelistedDomains | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $data; | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* @return array|null | ||
*/ | ||
public function getDomains() | ||
{ | ||
return isset($this->data['data'][0][DomainWhitelisting::WHITELISTED_DOMAINS]) ? | ||
$this->data['data'][0][DomainWhitelisting::WHITELISTED_DOMAINS] : | ||
null; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return WhitelistedDomains | ||
*/ | ||
public static function create(array $data) | ||
{ | ||
return new self($data); | ||
} | ||
} |