LightSetting.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin\Msg\Android;
  3. use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
  4. use Exception;
  5. class LightSetting
  6. {
  7. private $color;
  8. private $light_on_duration;
  9. private $light_off_duration;
  10. private $fields;
  11. public function __construct()
  12. {
  13. $this->color = array();
  14. $this->fields = array();
  15. }
  16. public function color($value)
  17. {
  18. $this->color = $value;
  19. }
  20. public function light_on_duration($value)
  21. {
  22. $this->light_on_duration = $value;
  23. }
  24. public function light_off_duration($value)
  25. {
  26. $this->light_off_duration = $value;
  27. }
  28. public function getFields()
  29. {
  30. return $this->fields;
  31. }
  32. public function buildFields()
  33. {
  34. try {
  35. $this->check_parameter();
  36. } catch (Exception $e) {
  37. echo $e;
  38. }
  39. $keys = array(
  40. 'color',
  41. 'light_on_duration',
  42. 'light_off_duration'
  43. );
  44. foreach ($keys as $key) {
  45. if (isset($this->$key)) {
  46. $this->fields[$key] = $this->$key;
  47. }
  48. }
  49. }
  50. private function check_parameter()
  51. {
  52. if (empty($this->color)) {
  53. throw new Exception("color must be selected when light_settings is set");
  54. }
  55. if (empty($this->light_on_duration)) {
  56. throw new Exception("light_on_duration must be selected when light_settings is set");
  57. }
  58. if (empty($this->light_off_duration)) {
  59. throw new Exception("light_off_duration must be selected when light_settings is set");
  60. }
  61. $LIGTH_DURATION_PATTERN = "/\\d+|\\d+[sS]|\\d+.\\d{1,9}|\\d+.\\d{1,9}[sS]/";
  62. if (FALSE == ValidatorUtil::validatePattern($LIGTH_DURATION_PATTERN, $this->light_on_duration)) {
  63. throw new Exception("light_on_duration format is wrong");
  64. }
  65. if (FALSE == ValidatorUtil::validatePattern($LIGTH_DURATION_PATTERN, $this->light_off_duration)) {
  66. throw new Exception("light_off_duration format is wrong");
  67. }
  68. }
  69. }