Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

ReplaceFilter.php

Same filename in this branch
  1. 11.1.x vendor/myclabs/deep-copy/src/DeepCopy/TypeFilter/ReplaceFilter.php

Namespace

DeepCopy\Filter

File

vendor/myclabs/deep-copy/src/DeepCopy/Filter/ReplaceFilter.php

View source
<?php

namespace DeepCopy\Filter;

use DeepCopy\Reflection\ReflectionHelper;

/**
 * @final
 */
class ReplaceFilter implements Filter {
    
    /**
     * @var callable
     */
    protected $callback;
    
    /**
     * @param callable $callable Will be called to get the new value for each property to replace
     */
    public function __construct(callable $callable) {
        $this->callback = $callable;
    }
    
    /**
     * Replaces the object property by the result of the callback called with the object property.
     *
     * {@inheritdoc}
     */
    public function apply($object, $property, $objectCopier) {
        $reflectionProperty = ReflectionHelper::getProperty($object, $property);
        $reflectionProperty->setAccessible(true);
        $value = call_user_func($this->callback, $reflectionProperty->getValue($object));
        $reflectionProperty->setValue($object, $value);
    }

}

Classes

Title Deprecated Summary
ReplaceFilter @final

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal