Xml.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Gedmo\Exception\InvalidMappingException,
  5. Gedmo\Uploadable\Mapping\Validator;
  6. /**
  7. * This is a xml mapping driver for Uploadable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from xml specificaly for Uploadable
  10. * extension.
  11. *
  12. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Xml extends BaseXml
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function readExtendedMetadata($meta, array &$config)
  23. {
  24. /**
  25. * @var \SimpleXmlElement $xml
  26. */
  27. $xml = $this->_getMapping($meta->name);
  28. $xmlDoctrine = $xml;
  29. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  30. if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
  31. if (isset($xml->uploadable)) {
  32. $xmlUploadable = $xml->uploadable;
  33. $config['uploadable'] = true;
  34. $config['allowOverwrite'] = $this->_isAttributeSet($xmlUploadable, 'allow-overwrite') ?
  35. (bool) $this->_getAttribute($xmlUploadable, 'allow-overwrite') : false;
  36. $config['appendNumber'] = $this->_isAttributeSet($xmlUploadable, 'append-number') ?
  37. (bool) $this->_getAttribute($xmlUploadable, 'append-number') : false;
  38. $config['path'] = $this->_isAttributeSet($xmlUploadable, 'path') ?
  39. $this->_getAttribute($xml->{'uploadable'}, 'path') : '';
  40. $config['pathMethod'] = $this->_isAttributeSet($xmlUploadable, 'path-method') ?
  41. $this->_getAttribute($xml->{'uploadable'}, 'path-method') : '';
  42. $config['callback'] = $this->_isAttributeSet($xmlUploadable, 'callback') ?
  43. $this->_getAttribute($xml->{'uploadable'}, 'callback') : '';
  44. $config['fileMimeTypeField'] = 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. } else if (isset($mapping->{'uploadable-file-size'})) {
  67. $config['fileSizeField'] = $field;
  68. } else if (isset($mapping->{'uploadable-file-path'})) {
  69. $config['filePathField'] = $field;
  70. }
  71. }
  72. }
  73. Validator::validateConfiguration($meta, $config);
  74. }
  75. }
  76. }
  77. }