Xml.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml;
  4. use Gedmo\Uploadable\Mapping\Validator;
  5. /**
  6. * This is a xml mapping driver for Uploadable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from xml specifically for Uploadable
  9. * extension.
  10. *
  11. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class Xml extends BaseXml
  17. {
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function readExtendedMetadata($meta, array &$config)
  22. {
  23. /**
  24. * @var \SimpleXmlElement $xml
  25. */
  26. $xml = $this->_getMapping($meta->name);
  27. $xmlDoctrine = $xml;
  28. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  29. if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
  30. if (isset($xml->uploadable)) {
  31. $xmlUploadable = $xml->uploadable;
  32. $config['uploadable'] = true;
  33. $config['allowOverwrite'] = $this->_isAttributeSet($xmlUploadable, 'allow-overwrite') ?
  34. (bool) $this->_getAttribute($xmlUploadable, 'allow-overwrite') : false;
  35. $config['appendNumber'] = $this->_isAttributeSet($xmlUploadable, 'append-number') ?
  36. (bool) $this->_getAttribute($xmlUploadable, 'append-number') : false;
  37. $config['path'] = $this->_isAttributeSet($xmlUploadable, 'path') ?
  38. $this->_getAttribute($xml->{'uploadable'}, 'path') : '';
  39. $config['pathMethod'] = $this->_isAttributeSet($xmlUploadable, 'path-method') ?
  40. $this->_getAttribute($xml->{'uploadable'}, 'path-method') : '';
  41. $config['callback'] = $this->_isAttributeSet($xmlUploadable, 'callback') ?
  42. $this->_getAttribute($xml->{'uploadable'}, 'callback') : '';
  43. $config['fileMimeTypeField'] = false;
  44. $config['fileNameField'] = false;
  45. $config['filePathField'] = false;
  46. $config['fileSizeField'] = false;
  47. $config['filenameGenerator'] = $this->_isAttributeSet($xmlUploadable, 'filename-generator') ?
  48. $this->_getAttribute($xml->{'uploadable'}, 'filename-generator') :
  49. Validator::FILENAME_GENERATOR_NONE;
  50. $config['maxSize'] = $this->_isAttributeSet($xmlUploadable, 'max-size') ?
  51. (double) $this->_getAttribute($xml->{'uploadable'}, 'max-size') :
  52. (double) 0;
  53. $config['allowedTypes'] = $this->_isAttributeSet($xmlUploadable, 'allowed-types') ?
  54. $this->_getAttribute($xml->{'uploadable'}, 'allowed-types') :
  55. '';
  56. $config['disallowedTypes'] = $this->_isAttributeSet($xmlUploadable, 'disallowed-types') ?
  57. $this->_getAttribute($xml->{'uploadable'}, 'disallowed-types') :
  58. '';
  59. if (isset($xmlDoctrine->field)) {
  60. foreach ($xmlDoctrine->field as $mapping) {
  61. $mappingDoctrine = $mapping;
  62. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  63. $field = $this->_getAttribute($mappingDoctrine, 'name');
  64. if (isset($mapping->{'uploadable-file-mime-type'})) {
  65. $config['fileMimeTypeField'] = $field;
  66. } elseif (isset($mapping->{'uploadable-file-size'})) {
  67. $config['fileSizeField'] = $field;
  68. } elseif (isset($mapping->{'uploadable-file-name'})) {
  69. $config['fileNameField'] = $field;
  70. } elseif (isset($mapping->{'uploadable-file-path'})) {
  71. $config['filePathField'] = $field;
  72. }
  73. }
  74. }
  75. Validator::validateConfiguration($meta, $config);
  76. }
  77. }
  78. }
  79. }