Reader.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * PHP Exif Reader: Reads EXIF metadata from a file
  4. *
  5. * @link http://github.com/miljar/PHPExif for the canonical source repository
  6. * @copyright Copyright (c) 2013 Tom Van Herreweghe <tom@theanalogguy.be>
  7. * @license http://github.com/miljar/PHPExif/blob/master/LICENSE MIT License
  8. * @category PHPExif
  9. * @package Reader
  10. */
  11. namespace PHPExif\Reader;
  12. use PHPExif\Adapter\AdapterInterface;
  13. use PHPExif\Adapter\NoAdapterException;
  14. use PHPExif\Adapter\Exiftool as ExiftoolAdapter;
  15. use PHPExif\Adapter\Native as NativeAdapter;
  16. /**
  17. * PHP Exif Reader
  18. *
  19. * Responsible for all the read operations on a file's EXIF metadata
  20. *
  21. * @category PHPExif
  22. * @package Reader
  23. * @
  24. */
  25. class Reader implements ReaderInterface
  26. {
  27. const TYPE_NATIVE = 'native';
  28. const TYPE_EXIFTOOL = 'exiftool';
  29. /**
  30. * The current adapter
  31. *
  32. * @var \PHPExif\Adapter\AdapterInterface
  33. */
  34. protected $adapter;
  35. /**
  36. * Reader constructor
  37. *
  38. * @param \PHPExif\Adapter\AdapterInterface $adapter
  39. */
  40. public function __construct(AdapterInterface $adapter)
  41. {
  42. $this->adapter = $adapter;
  43. }
  44. /**
  45. * Getter for the reader adapter
  46. *
  47. * @return \PHPExif\Adapter\AdapterInterface
  48. * @throws NoAdapterException When no adapter is set
  49. */
  50. public function getAdapter()
  51. {
  52. if (empty($this->adapter)) {
  53. throw new NoAdapterException('No adapter set in the reader');
  54. }
  55. return $this->adapter;
  56. }
  57. /**
  58. * Factory for the reader
  59. *
  60. * @param string $type
  61. * @return $this
  62. * @throws \InvalidArgumentException When given type is invalid
  63. */
  64. public static function factory($type)
  65. {
  66. $classname = get_called_class();
  67. switch ($type) {
  68. case self::TYPE_NATIVE:
  69. $adapter = new NativeAdapter();
  70. break;
  71. case self::TYPE_EXIFTOOL:
  72. $adapter = new ExiftoolAdapter();
  73. break;
  74. default:
  75. throw new \InvalidArgumentException(
  76. sprintf('Unknown type "%1$s"', $type)
  77. );
  78. }
  79. return new $classname($adapter);
  80. }
  81. /**
  82. * Reads & parses the EXIF data from given file
  83. *
  84. * @param string $file
  85. * @return \PHPExif\Exif Instance of Exif object with data
  86. */
  87. public function read($file)
  88. {
  89. return $this->getAdapter()->getExifFromFile($file);
  90. }
  91. /**
  92. * alias to read method
  93. *
  94. * @param string $file
  95. * @return \PHPExif\Exif Instance of Exif object with data
  96. */
  97. public function getExifFromFile($file)
  98. {
  99. return $this->read($file);
  100. }
  101. }