Base.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace WhichBrowser\Model\Primitive;
  3. /**
  4. * @internal
  5. */
  6. class Base
  7. {
  8. /**
  9. * Set the properties of the object the the values specified in the array
  10. *
  11. * @param array|null An array, the key of an element determines the name of the property
  12. */
  13. public function __construct($defaults = null)
  14. {
  15. if (is_array($defaults)) {
  16. $this->set($defaults);
  17. }
  18. }
  19. /**
  20. * Set the properties of the object the the values specified in the array
  21. *
  22. * @param array $properties An array, the key of an element determines the name of the property
  23. *
  24. * @internal
  25. */
  26. public function set($properties)
  27. {
  28. foreach ($properties as $k => $v) {
  29. $this->{$k} = $v;
  30. }
  31. }
  32. /**
  33. * Get a string containing a JavaScript representation of the object
  34. *
  35. * @internal
  36. *
  37. * @return string
  38. */
  39. public function toJavaScript()
  40. {
  41. $lines = [];
  42. foreach (get_object_vars($this) as $key => $value) {
  43. if (!is_null($value)) {
  44. $line = $key . ": ";
  45. if ($key == 'version') {
  46. $line .= 'new Version({ ' . $value->toJavaScript() . ' })';
  47. } elseif ($key == 'family') {
  48. $line .= 'new Family({ ' . $value->toJavaScript() . ' })';
  49. } elseif ($key == 'using') {
  50. $line .= 'new Using({ ' . $value->toJavaScript() . ' })';
  51. } else {
  52. switch (gettype($value)) {
  53. case 'boolean':
  54. $line .= $value ? 'true' : 'false';
  55. break;
  56. case 'string':
  57. $line .= '"' . addslashes($value) . '"';
  58. break;
  59. case 'integer':
  60. $line .= $value;
  61. break;
  62. }
  63. }
  64. $lines[] = $line;
  65. }
  66. }
  67. return implode($lines, ", ");
  68. }
  69. }