Values.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Twilio;
  3. class Values implements \ArrayAccess {
  4. const NONE = 'Twilio\\Values\\NONE';
  5. protected $options;
  6. public static function array_get($array, $key, $default = null) {
  7. if (array_key_exists($key, $array)) {
  8. return $array[$key];
  9. }
  10. return $default;
  11. }
  12. public static function of($array) {
  13. $result = array();
  14. foreach ($array as $key => $value) {
  15. if ($value === self::NONE) {
  16. continue;
  17. }
  18. $result[$key] = $value;
  19. }
  20. return $result;
  21. }
  22. public function __construct($options) {
  23. $this->options = array();
  24. foreach ($options as $key => $value) {
  25. $this->options[strtolower($key)] = $value;
  26. }
  27. }
  28. /**
  29. * (PHP 5 &gt;= 5.0.0)<br/>
  30. * Whether a offset exists
  31. * @link http://php.net/manual/en/arrayaccess.offsetexists.php
  32. * @param mixed $offset <p>
  33. * An offset to check for.
  34. * </p>
  35. * @return boolean true on success or false on failure.
  36. * </p>
  37. * <p>
  38. * The return value will be casted to boolean if non-boolean was returned.
  39. */
  40. public function offsetExists($offset) {
  41. return true;
  42. }
  43. /**
  44. * (PHP 5 &gt;= 5.0.0)<br/>
  45. * Offset to retrieve
  46. * @link http://php.net/manual/en/arrayaccess.offsetget.php
  47. * @param mixed $offset <p>
  48. * The offset to retrieve.
  49. * </p>
  50. * @return mixed Can return all value types.
  51. */
  52. public function offsetGet($offset) {
  53. $offset = strtolower($offset);
  54. return array_key_exists($offset, $this->options) ? $this->options[$offset] : self::NONE;
  55. }
  56. /**
  57. * (PHP 5 &gt;= 5.0.0)<br/>
  58. * Offset to set
  59. * @link http://php.net/manual/en/arrayaccess.offsetset.php
  60. * @param mixed $offset <p>
  61. * The offset to assign the value to.
  62. * </p>
  63. * @param mixed $value <p>
  64. * The value to set.
  65. * </p>
  66. * @return void
  67. */
  68. public function offsetSet($offset, $value) {
  69. $this->options[strtolower($offset)] = $value;
  70. }
  71. /**
  72. * (PHP 5 &gt;= 5.0.0)<br/>
  73. * Offset to unset
  74. * @link http://php.net/manual/en/arrayaccess.offsetunset.php
  75. * @param mixed $offset <p>
  76. * The offset to unset.
  77. * </p>
  78. * @return void
  79. */
  80. public function offsetUnset($offset) {
  81. unset($this->options[$offset]);
  82. }
  83. }