Mapping.Class.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php // vim:set ts=4 sw=4 et:
  2. namespace Mall\Framework\SearchClient;
  3. /**
  4. * This file is part of the ElasticSearch PHP client
  5. *
  6. * (c) Raymond Julin <raymond.julin@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. class Mapping {
  12. protected $properties = array();
  13. protected $config = array();
  14. /**
  15. * Build mapping data
  16. *
  17. * @param array $properties
  18. * @param array $config
  19. * @return Mapping
  20. */
  21. public function __construct(array $properties = array(), array $config = array()) {
  22. $this->properties = $properties;
  23. $this->config = $config;
  24. }
  25. /**
  26. * Export mapping data as a json-ready array
  27. *
  28. * @return string
  29. */
  30. public function export() {
  31. return array(
  32. 'properties' => $this->properties
  33. );
  34. }
  35. /**
  36. * Add or overwrite existing field by name
  37. *
  38. * @param string $field
  39. * @param string|array $config
  40. * @return $this
  41. */
  42. public function field($field, $config = array()) {
  43. if (is_string($config)) $config = array('type' => $config);
  44. $this->properties[$field] = $config;
  45. return $this;
  46. }
  47. /**
  48. * Get or set a config
  49. *
  50. * @param string $key
  51. * @param mixed $value
  52. * @throws \Exception
  53. * @return array|void
  54. */
  55. public function config($key, $value = null) {
  56. if (is_array($key))
  57. $this->config = $key + $this->config;
  58. else {
  59. if ($value !== null) $this->config[$key] = $value;
  60. if (!isset($this->config[$key]))
  61. throw new \Exception("Configuration key `type` is not set");
  62. return $this->config[$key];
  63. }
  64. }
  65. }