Yaml.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Gedmo\SoftDeleteable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File;
  4. use Gedmo\Mapping\Driver;
  5. use Gedmo\Exception\InvalidMappingException;
  6. use Gedmo\SoftDeleteable\Mapping\Validator;
  7. /**
  8. * This is a yaml mapping driver for Timestampable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specifically for Timestampable
  11. * extension.
  12. *
  13. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Yaml extends File implements Driver
  18. {
  19. /**
  20. * File extension
  21. * @var string
  22. */
  23. protected $_extension = '.dcm.yml';
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function readExtendedMetadata($meta, array &$config)
  28. {
  29. $mapping = $this->_getMapping($meta->name);
  30. if (isset($mapping['gedmo'])) {
  31. $classMapping = $mapping['gedmo'];
  32. if (isset($classMapping['soft_deleteable'])) {
  33. $config['softDeleteable'] = true;
  34. if (!isset($classMapping['soft_deleteable']['field_name'])) {
  35. throw new InvalidMappingException('Field name for SoftDeleteable class is mandatory.');
  36. }
  37. $fieldName = $classMapping['soft_deleteable']['field_name'];
  38. Validator::validateField($meta, $fieldName);
  39. $config['fieldName'] = $fieldName;
  40. $config['timeAware'] = false;
  41. if (isset($classMapping['soft_deleteable']['time_aware'])) {
  42. if (!is_bool($classMapping['soft_deleteable']['time_aware'])) {
  43. throw new InvalidMappingException("timeAware must be boolean. ".gettype($classMapping['soft_deleteable']['time_aware'])." provided.");
  44. }
  45. $config['timeAware'] = $classMapping['soft_deleteable']['time_aware'];
  46. }
  47. $config['hardDelete'] = true;
  48. if (isset($classMapping['soft_deleteable']['hard_delete'])) {
  49. if (!is_bool($classMapping['soft_deleteable']['hard_delete'])) {
  50. throw new InvalidMappingException("hardDelete must be boolean. ".gettype($classMapping['soft_deleteable']['hard_delete'])." provided.");
  51. }
  52. $config['hardDelete'] = $classMapping['soft_deleteable']['hard_delete'];
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. protected function _loadMappingFile($file)
  61. {
  62. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  63. }
  64. }