FieldDescription.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * Define the association mapping definition
  16. *
  17. * @param array $associationMapping
  18. * @return void
  19. */
  20. public function setAssociationMapping($associationMapping)
  21. {
  22. if (!is_array($associationMapping)) {
  23. throw new \RuntimeException('The association mapping must be an array');
  24. }
  25. $this->associationMapping = $associationMapping;
  26. $this->type = $this->type ?: $associationMapping['type'];
  27. $this->mappingType = $this->mappingType ?: $associationMapping['type'];
  28. $this->fieldName = $associationMapping['fieldName'];
  29. }
  30. /**
  31. * return the related Target Entity
  32. *
  33. * @return string|null
  34. */
  35. public function getTargetEntity()
  36. {
  37. if ($this->associationMapping) {
  38. return $this->associationMapping['targetEntity'];
  39. }
  40. return null;
  41. }
  42. /**
  43. * set the field mapping information
  44. *
  45. * @param array $fieldMapping
  46. * @return void
  47. */
  48. public function setFieldMapping($fieldMapping)
  49. {
  50. if (!is_array($fieldMapping)) {
  51. throw new \RuntimeException('The field mapping must be an array');
  52. }
  53. $this->fieldMapping = $fieldMapping;
  54. $this->type = $this->type ?: $fieldMapping['type'];
  55. $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
  56. $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
  57. }
  58. /**
  59. * return true if the FieldDescription is linked to an identifier field
  60. *
  61. * @return bool
  62. */
  63. public function isIdentifier()
  64. {
  65. return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
  66. }
  67. }