ArrayAdapter.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ yTnQ2z0Nsi/n2uT0dpFNhSpbTIhz5Lmn+TS+zwwNebU=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. namespace Cube\Translate\Adapter;
  13. use Cube\Config\AbstractConfig,
  14. Cube\Locale;
  15. /**
  16. *
  17. * array translate adapter
  18. * will accept arrays as inputs
  19. *
  20. * Class ArrayAdapter
  21. *
  22. * @package Cube\Translate\Adapter
  23. */
  24. class ArrayAdapter extends AbstractAdapter
  25. {
  26. /**
  27. *
  28. * add translation to the adapter
  29. *
  30. * @param array $options
  31. *
  32. * @return $this
  33. * @throws \InvalidArgumentException
  34. */
  35. public function addTranslation($options = array())
  36. {
  37. if (!is_array($options) && !($options instanceof AbstractConfig)) {
  38. throw new \InvalidArgumentException("The translation object requires an
  39. array or an object of type \Cube\ConfigAbstract.");
  40. }
  41. else {
  42. if ($options instanceof AbstractConfig) {
  43. $options = $options->getData();
  44. }
  45. $file = (isset($options['file'])) ? $options['file'] : null;
  46. $locale = (isset($options['locale'])) ? $options['locale'] : null;
  47. if (file_exists($file) === false) {
  48. throw new \InvalidArgumentException(
  49. sprintf("Add translation method error: The translation file '%s' could not be found.", $file));
  50. }
  51. if (Locale::isLocale($locale) === false) {
  52. throw new \InvalidArgumentException(
  53. sprintf("Add translation method error: '%s' is an invalid locale.", $locale));
  54. }
  55. $this->_translate[$locale] = include($file);
  56. }
  57. return $this;
  58. }
  59. }