vendor/mark-gerarts/auto-mapper-plus/src/MappingOperation/DefaultMappingOperation.php line 51

Open in your IDE?
  1. <?php
  2. namespace AutoMapperPlus\MappingOperation;
  3. use AutoMapperPlus\Configuration\Options;
  4. use AutoMapperPlus\NameResolver\NameResolverInterface;
  5. use AutoMapperPlus\PropertyAccessor\PropertyReaderInterface;
  6. use AutoMapperPlus\PropertyAccessor\PropertyWriterInterface;
  7. /**
  8.  * Class DefaultMappingOperation
  9.  *
  10.  * @package AutoMapperPlus\MappingOperation
  11.  */
  12. class DefaultMappingOperation implements MappingOperationInterface
  13. {
  14.     /**
  15.      * @var Options
  16.      */
  17.     protected $options;
  18.     /**
  19.      * Options that are used will be stored for performance reasons, because
  20.      * each property being mapped incurs a method call.
  21.      */
  22.     /**
  23.      * @var NameResolverInterface
  24.      */
  25.     protected $nameResolver;
  26.     /**
  27.      * @var PropertyReaderInterface
  28.      */
  29.     protected $propertyReader;
  30.     /**
  31.      * @var PropertyWriterInterface
  32.      */
  33.     protected $propertyWriter;
  34.     /**
  35.      * @inheritdoc
  36.      */
  37.     public function mapProperty(string $propertyName$source$destination): void
  38.     {
  39.         if (!$this->canMapProperty($propertyName$source)) {
  40.             // Alternatively throw an error here.
  41.             return;
  42.         }
  43.         $sourceValue $this->getSourceValue($source$propertyName);
  44.         $this->setDestinationValue($destination$propertyName$sourceValue);
  45.     }
  46.     /**
  47.      * @inheritdoc
  48.      */
  49.     public function setOptions(Options $options): void
  50.     {
  51.         $this->options $options;
  52.         $this->nameResolver $options->getNameResolver();
  53.         $this->propertyReader $options->getPropertyReader();
  54.         $this->propertyWriter $options->getPropertyWriter();
  55.     }
  56.     /**
  57.      * @param string $propertyName
  58.      * @param $source
  59.      * @return bool
  60.      */
  61.     protected function canMapProperty(string $propertyName$source): bool
  62.     {
  63.         $sourcePropertyName $this->getSourcePropertyName($propertyName);
  64.         return $this->getPropertyReader()->hasProperty($source$sourcePropertyName);
  65.     }
  66.     /**
  67.      * @param $source
  68.      * @param string $propertyName
  69.      * @return mixed
  70.      */
  71.     protected function getSourceValue($sourcestring $propertyName)
  72.     {
  73.         return $this->getPropertyReader()->getProperty(
  74.             $source,
  75.             $this->getSourcePropertyName($propertyName)
  76.         );
  77.     }
  78.     /**
  79.      * @param $destination
  80.      * @param string $propertyName
  81.      * @param $value
  82.      */
  83.     protected function setDestinationValue(
  84.         $destination,
  85.         string $propertyName,
  86.         $value
  87.     ): void {
  88.         if ($value === null && $this->options->shouldIgnoreNullProperties()) {
  89.             return;
  90.         }
  91.         $this->getPropertyWriter()->setProperty(
  92.             $destination,
  93.             $propertyName,
  94.             $value
  95.         );
  96.     }
  97.     /**
  98.      * @return PropertyAccessorInterface
  99.      * @deprecated The PropertyAccessorInterface has been split up in reading and writing. Use these instead.
  100.      */
  101.     protected function getPropertyAccessor(): PropertyAccessorInterface
  102.     {
  103.         return $this->options->getPropertyAccessor();
  104.     }
  105.     /**
  106.      * @return PropertyReaderInterface
  107.      */
  108.     protected function getPropertyReader(): PropertyReaderInterface
  109.     {
  110.         return $this->propertyReader;
  111.     }
  112.     /**
  113.      * @return PropertyWriterInterface
  114.      */
  115.     protected function getPropertyWriter(): PropertyWriterInterface
  116.     {
  117.         return $this->propertyWriter;
  118.     }
  119.     /**
  120.      * Returns the name of the property we should fetch from the source object.
  121.      *
  122.      * @param string $propertyName
  123.      * @return string
  124.      */
  125.     protected function getSourcePropertyName(string $propertyName): string
  126.     {
  127.         return $this->nameResolver->getSourcePropertyName(
  128.             $propertyName,
  129.             $this,
  130.             $this->options
  131.         );
  132.     }
  133. }