FieldDescription.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) 2010-2011 Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\DoctrineORMAdminBundle\Admin;
  11. use Sonata\AdminBundle\Admin\BaseFieldDescription;
  12. class FieldDescription extends BaseFieldDescription
  13. {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function __construct()
  18. {
  19. $this->parentAssociationMappings = array();
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function setAssociationMapping($associationMapping)
  25. {
  26. if (!is_array($associationMapping)) {
  27. throw new \RuntimeException('The association mapping must be an array');
  28. }
  29. $this->associationMapping = $associationMapping;
  30. $this->type = $this->type ? : $associationMapping['type'];
  31. $this->mappingType = $this->mappingType ? : $associationMapping['type'];
  32. $this->fieldName = $associationMapping['fieldName'];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getTargetEntity()
  38. {
  39. if ($this->associationMapping) {
  40. return $this->associationMapping['targetEntity'];
  41. }
  42. return null;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function setFieldMapping($fieldMapping)
  48. {
  49. if (!is_array($fieldMapping)) {
  50. throw new \RuntimeException('The field mapping must be an array');
  51. }
  52. $this->fieldMapping = $fieldMapping;
  53. $this->type = $this->type ? : $fieldMapping['type'];
  54. $this->mappingType = $this->mappingType ? : $fieldMapping['type'];
  55. $this->fieldName = $this->fieldName ? : $fieldMapping['fieldName'];
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function setParentAssociationMappings(array $parentAssociationMappings)
  61. {
  62. foreach ($parentAssociationMappings as $parentAssociationMapping) {
  63. if (!is_array($parentAssociationMapping)) {
  64. throw new \RuntimeException('An association mapping must be an array');
  65. }
  66. }
  67. $this->parentAssociationMappings = $parentAssociationMappings;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function isIdentifier()
  73. {
  74. return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getValue($object)
  80. {
  81. foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
  82. $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
  83. }
  84. return $this->getFieldValue($object, $this->fieldName);
  85. }
  86. }