Advertisement
timschoch

Extbase Optional Relation Property

Oct 13th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. /***********************************************
  2.  *
  3.  * Change Setter in DomainObject
  4.  *
  5.  **********************************************/
  6.  
  7.  
  8.  
  9.  
  10.  
  11. /**
  12.  * Sets the Related Property
  13.  *
  14.  * @param Tx_Myext_Domain_Model_RelatedObject $relatedObject
  15.  * @return Tx_Myext_Domain_Model_ParentObject
  16.  */
  17. public function setRelatedObject( $relatedObject = NULL ) {
  18.     // reset the object if something other then a valid object is passed
  19.   if ( ! $relatedObject instanceof Tx_Myext_Domain_Model_RelatedObject ) {
  20.     $relatedObject = NULL;
  21.   }
  22.   $this->relatedObject = $relatedObject;
  23.   return $this;
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30. /***********************************************
  31.  *
  32.  * custom Converter
  33.  * To enable this in your extension, add the following to you ext_localconf.php
  34.  * Tx_Extbase_Utility_Extension::registerTypeConverter( 'Tx_Myext_Property_TypeConverter_PersistentObjectConverter' );
  35.  *
  36.  **********************************************/
  37.  
  38.  
  39. /**
  40.  * allows for empty values - in this case it will return NULL
  41.  *
  42.  * To enable this in your extension, add the following to you ext_localconf.php
  43.  *  // Register type converters
  44.  * Tx_Extbase_Utility_Extension::registerTypeConverter( 'Tx_Myext_Property_TypeConverter_PersistentObjectConverter' );
  45.  */
  46. class Tx_Myext_Property_TypeConverter_PersistentObjectConverter extends Tx_Extbase_Property_TypeConverter_PersistentObjectConverter {
  47.  
  48.   /**
  49.    * @var integer
  50.    */
  51.   protected $priority = 2;
  52.  
  53.  
  54.   /**
  55.    * We can only convert if the $targetType is either tagged with entity or value object.
  56.    * Empty strings CAN NOT be converted BUT will return NULL
  57.    *
  58.    * @param mixed $source
  59.    * @param string $targetType
  60.    * @return boolean
  61.    */
  62.   public function canConvertFrom($source, $targetType) {
  63.     if ( is_array( $source ) && isset( $source['__identity'] ) && 1 === count( $source ) && 0 >= intval( $source['__identity'] ) ) {
  64.       return TRUE;
  65.     }
  66.     elseif ( '' === $source || -1 === intval( $source ) || 0 === intval( $source ) ) {
  67.       return TRUE;
  68.     } else {
  69.       return parent::canConvertFrom( $source, $targetType );
  70.     }
  71.   }
  72.  
  73.   /**
  74.    * Convert an object from $source to an entity or a value object.
  75.    * Empty strings CAN NOT be converted BUT will return NULL
  76.    *
  77.    * @param mixed $source
  78.    * @param string $targetType
  79.    * @param array $convertedChildProperties
  80.    * @param Tx_Extbase_Property_PropertyMappingConfigurationInterface $configuration
  81.    * @return object the target type
  82.    */
  83.   public function convertFrom($source, $targetType, array $convertedChildProperties = array(), Tx_Extbase_Property_PropertyMappingConfigurationInterface $configuration = NULL) {
  84.     if ( is_array( $source ) && isset( $source['__identity'] ) && 1 === count( $source ) && 0 >= intval( $source['__identity'] ) ) {
  85.       return 0;
  86.     }
  87.     elseif ( '' === $source || -1 === intval( $source ) || 0 === intval( $source ) ) {
  88.       return 0;
  89.     } else {
  90.       return parent::convertFrom( $source, $targetType, $convertedChildProperties, $configuration );
  91.     }
  92.   }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement