Xml.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * @package Gedmo.Uploadable.Mapping.Driver
  16. * @subpackage Xml
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class Xml extends BaseXml
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function readExtendedMetadata($meta, array &$config)
  26. {
  27. /**
  28. * @var \SimpleXmlElement $xml
  29. */
  30. $xml = $this->_getMapping($meta->name);
  31. $xmlDoctrine = $xml;
  32. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  33. if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
  34. if (isset($xml->uploadable)) {
  35. $xmlUploadable = $xml->uploadable;
  36. $config['uploadable'] = true;
  37. $config['allowOverwrite'] = $this->_isAttributeSet($xmlUploadable, 'allow-overwrite') ?
  38. (bool) $this->_getAttribute($xmlUploadable, 'allow-overwrite') : false;
  39. $config['appendNumber'] = $this->_isAttributeSet($xmlUploadable, 'append-number') ?
  40. (bool) $this->_getAttribute($xmlUploadable, 'append-number') : false;
  41. $config['path'] = $this->_isAttributeSet($xmlUploadable, 'path') ?
  42. $this->_getAttribute($xml->{'uploadable'}, 'path') : '';
  43. $config['pathMethod'] = $this->_isAttributeSet($xmlUploadable, 'path-method') ?
  44. $this->_getAttribute($xml->{'uploadable'}, 'path-method') : '';
  45. $config['callback'] = $this->_isAttributeSet($xmlUploadable, 'callback') ?
  46. $this->_getAttribute($xml->{'uploadable'}, 'callback') : '';
  47. $config['fileMimeTypeField'] = false;
  48. $config['filePathField'] = false;
  49. $config['fileSizeField'] = false;
  50. $config['filenameGenerator'] = $this->_isAttributeSet($xmlUploadable, 'filename-generator') ?
  51. $this->_getAttribute($xml->{'uploadable'}, 'filename-generator') :
  52. Validator::FILENAME_GENERATOR_NONE;
  53. $config['maxSize'] = $this->_isAttributeSet($xmlUploadable, 'max-size') ?
  54. (double) $this->_getAttribute($xml->{'uploadable'}, 'max-size') :
  55. (double) 0;
  56. $config['allowedTypes'] = $this->_isAttributeSet($xmlUploadable, 'allowed-types') ?
  57. $this->_getAttribute($xml->{'uploadable'}, 'allowed-types') :
  58. '';
  59. $config['disallowedTypes'] = $this->_isAttributeSet($xmlUploadable, 'disallowed-types') ?
  60. $this->_getAttribute($xml->{'uploadable'}, 'disallowed-types') :
  61. '';
  62. if (isset($xmlDoctrine->field)) {
  63. foreach ($xmlDoctrine->field as $mapping) {
  64. $mappingDoctrine = $mapping;
  65. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  66. $field = $this->_getAttribute($mappingDoctrine, 'name');
  67. if (isset($mapping->{'uploadable-file-mime-type'})) {
  68. $config['fileMimeTypeField'] = $field;
  69. } else if (isset($mapping->{'uploadable-file-size'})) {
  70. $config['fileSizeField'] = $field;
  71. } else if (isset($mapping->{'uploadable-file-path'})) {
  72. $config['filePathField'] = $field;
  73. }
  74. }
  75. }
  76. Validator::validateConfiguration($meta, $config);
  77. }
  78. }
  79. }
  80. }