Country.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Geocoder package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. namespace Geocoder\Model;
  11. use Geocoder\Exception\InvalidArgument;
  12. /**
  13. * A Country has either a name or a code. A Country will never be without data.
  14. *
  15. * @author William Durand <william.durand1@gmail.com>
  16. */
  17. final class Country
  18. {
  19. /**
  20. * @var string|null
  21. */
  22. private $name;
  23. /**
  24. * @var string|null
  25. */
  26. private $code;
  27. /**
  28. * @param string $name
  29. * @param string $code
  30. */
  31. public function __construct(string $name = null, string $code = null)
  32. {
  33. if (null === $name && null === $code) {
  34. throw new InvalidArgument('A country must have either a name or a code');
  35. }
  36. $this->name = $name;
  37. $this->code = $code;
  38. }
  39. /**
  40. * Returns the country name.
  41. *
  42. * @return string|null
  43. */
  44. public function getName()
  45. {
  46. return $this->name;
  47. }
  48. /**
  49. * Returns the country ISO code.
  50. *
  51. * @return string|null
  52. */
  53. public function getCode()
  54. {
  55. return $this->code;
  56. }
  57. /**
  58. * Returns a string with the country name.
  59. *
  60. * @return string
  61. */
  62. public function __toString(): string
  63. {
  64. return $this->getName() ?: '';
  65. }
  66. }