RegionDataProvider.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Data\Provider;
  11. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
  12. use Symfony\Component\Intl\Locale;
  13. /**
  14. * Data provider for region-related ICU data.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @internal
  19. */
  20. class RegionDataProvider
  21. {
  22. /**
  23. * @var string
  24. */
  25. private $path;
  26. /**
  27. * @var BundleEntryReaderInterface
  28. */
  29. private $reader;
  30. /**
  31. * Creates a data provider that reads locale-related data from .res files.
  32. *
  33. * @param string $path The path to the directory
  34. * containing the .res files.
  35. * @param BundleEntryReaderInterface $reader The reader for reading the .res
  36. * files.
  37. */
  38. public function __construct($path, BundleEntryReaderInterface $reader)
  39. {
  40. $this->path = $path;
  41. $this->reader = $reader;
  42. }
  43. public function getRegions()
  44. {
  45. return $this->reader->readEntry($this->path, 'meta', array('Regions'));
  46. }
  47. public function getName($region, $displayLocale = null)
  48. {
  49. if (null === $displayLocale) {
  50. $displayLocale = Locale::getDefault();
  51. }
  52. return $this->reader->readEntry($this->path, $displayLocale, array('Names', $region));
  53. }
  54. public function getNames($displayLocale = null)
  55. {
  56. if (null === $displayLocale) {
  57. $displayLocale = Locale::getDefault();
  58. }
  59. $names = $this->reader->readEntry($this->path, $displayLocale, array('Names'));
  60. if ($names instanceof \Traversable) {
  61. $names = iterator_to_array($names);
  62. }
  63. $collator = new \Collator($displayLocale);
  64. $collator->asort($names);
  65. return $names;
  66. }
  67. }