Adapter.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\g11n\catalog;
  9. /**
  10. * This is the foundation class for all g11n catalog adapters.
  11. */
  12. class Adapter extends \lithium\core\Object {
  13. /**
  14. * Reads data.
  15. *
  16. * Override this method in subclasses if you want the adapter
  17. * to have read support. The method is expected to return `null`
  18. * if the passed category is not supported.
  19. *
  20. * @param string $category A category.
  21. * @param string $locale A locale identifier.
  22. * @param string $scope The scope for the current operation.
  23. * @return null This currently does nothing.
  24. */
  25. public function read($category, $locale, $scope) {
  26. return null;
  27. }
  28. /**
  29. * Writes data.
  30. *
  31. * Override this method in subclasses if you want the adapter
  32. * to have write support. The method is expected to return `false`
  33. * if the passed category is not supported.
  34. *
  35. * Please note that existing data is silently overwritten.
  36. *
  37. * @param string $category A category.
  38. * @param string $locale A locale identifier.
  39. * @param string $scope The scope for the current operation.
  40. * @param array $data The data to write.
  41. * @return false This currently does nothing.
  42. */
  43. public function write($category, $locale, $scope, array $data) {
  44. return false;
  45. }
  46. /**
  47. * Prepares an item before it is being written.
  48. *
  49. * Override this method in sublcasses if you need to
  50. * i.e. escape the item's values.
  51. *
  52. * @param array $item
  53. * @return array
  54. */
  55. protected function _prepareForWrite(array $item) {
  56. return $item;
  57. }
  58. /**
  59. * Merges an item into given data.
  60. *
  61. * @param array $data Data to merge item into.
  62. * @param array $item Item to merge into $data. The item must have an `'id'` key.
  63. * @return array The merged data.
  64. */
  65. protected function _merge(array $data, array $item) {
  66. if (!isset($item['id'])) {
  67. return $data;
  68. }
  69. $id = $item['id'];
  70. $defaults = array(
  71. 'ids' => array(),
  72. 'translated' => null,
  73. 'flags' => array(),
  74. 'comments' => array(),
  75. 'occurrences' => array()
  76. );
  77. $item += $defaults;
  78. if (!isset($data[$id])) {
  79. $data[$id] = $item;
  80. return $data;
  81. }
  82. foreach (array('ids', 'flags', 'comments', 'occurrences') as $field) {
  83. $data[$id][$field] = array_merge($data[$id][$field], $item[$field]);
  84. }
  85. if (!isset($data[$id]['translated'])) {
  86. $data[$id]['translated'] = $item['translated'];
  87. } elseif (is_array($item['translated'])) {
  88. $data[$id]['translated'] = (array) $data[$id]['translated'] + $item['translated'];
  89. }
  90. return $data;
  91. }
  92. }
  93. ?>