### Property Name Matcher Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Demonstrates the use of `PropertyNameMatcher` to target properties by their name across any object. This is useful for applying a specific filter to all properties named 'id', for example. ```php use DeepCopy\Matcher\PropertyNameMatcher; // Will apply a filter to any property of any objects named "id" $matcher = new PropertyNameMatcher('id'); ``` -------------------------------- ### DoctrineEmptyCollectionFilter for Resetting Collections Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Resets a Doctrine Collection property to an empty state during a deep copy. This is useful when you want a copied entity to start with empty collections for certain properties. ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter; use DeepCopy\Matcher\PropertyMatcher; $copier = new DeepCopy(); $copier->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'myProperty')); $copy = $copier->copy($object); // $copy->myProperty will be an empty collection ``` -------------------------------- ### Running Tests Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Instructions on how to execute the project's tests using the PHPUnit framework. This command should be run from the project's root directory. ```bash vendor/bin/phpunit ``` -------------------------------- ### Deep Copy with Configuration Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Shows how to instantiate DeepCopy with specific configuration options, such as enabling a feature (indicated by `true` in the constructor), and then performing a copy operation. ```php use DeepCopy\DeepCopy; $copier = new DeepCopy(true); $copy = $copier->copy($var); ``` -------------------------------- ### Basic Deep Copy Usage Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Demonstrates the fundamental usage of the DeepCopy library to create a deep copy of an object. It initializes the DeepCopy object and then uses its copy method. ```php use DeepCopy\DeepCopy; $copier = new DeepCopy(); $myCopy = $copier->copy($myObject); ``` -------------------------------- ### Specific Property Matcher Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Shows how to use `PropertyMatcher` to target a specific property ('id') of a specific class ('MyClass'). This provides more granular control over which properties are matched. ```php use DeepCopy\Matcher\PropertyMatcher; // Will apply a filter to the property "id" of any objects of the class "MyClass" $matcher = new PropertyMatcher('MyClass', 'id'); ``` -------------------------------- ### Custom Deep Copy Function Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Illustrates how to create a custom deep copy function within a namespace that utilizes the DeepCopy library. It includes a static instance of DeepCopy for efficiency. ```php namespace Acme; use DeepCopy\DeepCopy; function deep_copy($var) { static $copier = null; if (null === $copier) { $copier = new DeepCopy(true); } return $copier->copy($var); } ``` -------------------------------- ### Type Matcher Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Illustrates the `TypeMatcher` for matching elements based on their type, such as instances of a specific class (e.g., `DoctrineCommonCollectionsCollection`) or values compatible with `gettype()`. ```php use DeepCopy\TypeMatcher\TypeMatcher; // Will apply a filter to any object that is an instance of Doctrine\Common\Collections\Collection $matcher = new TypeMatcher('Doctrine\Common\Collections\Collection'); ``` -------------------------------- ### ShallowCopyFilter Usage Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Demonstrates how to use the ShallowCopyFilter to prevent deep copying of specific types, such as mock objects, by using a TypeMatcher. This ensures that only a shallow clone is performed for matched types. ```php use DeepCopy\DeepCopy; use DeepCopy\TypeFilter\ShallowCopyFilter; use DeepCopy\TypeMatcher\TypeMatcher; use Mockery as m; $this->deepCopy = new DeepCopy(); $this->deepCopy->addTypeFilter( new ShallowCopyFilter, new TypeMatcher(m\MockInterface::class) ); $myServiceWithMocks = new MyService(m::mock(MyDependency1::class), m::mock(MyDependency2::class)); // All mocks will be just cloned, not deep copied ``` -------------------------------- ### ChainableFilter with DoctrineProxyFilter Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Allows applying multiple filters in sequence, particularly useful when dealing with Doctrine proxy classes. It decorates a primary filter (like DoctrineProxyFilter) and allows subsequent filters in the chain to be applied. ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\ChainableFilter; use DeepCopy\Filter\Doctrine\DoctrineProxyFilter; use DeepCopy\Filter\SetNullFilter; use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher; use DeepCopy\Matcher\PropertyNameMatcher; $copier = new DeepCopy(); $copier->addFilter(new ChainableFilter(new DoctrineProxyFilter()), new DoctrineProxyMatcher()); $copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id')); $copy = $copier->copy($object); // $copy->id will be null ``` -------------------------------- ### ReplaceFilter for Property Value Replacement Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Replaces the value of a specific property with the result of a callback function. The callback receives the current property value and should return the new value. ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\ReplaceFilter; use DeepCopy\Matcher\PropertyMatcher; $copier = new DeepCopy(); $callback = function ($currentValue) { return $currentValue . ' (copy)'; }; $copier->addFilter(new ReplaceFilter($callback), new PropertyMatcher('MyClass', 'title')); $copy = $copier->copy($object); // $copy->title will be 'The title (copy)' ``` -------------------------------- ### DoctrineProxyFilter for Lazy Loaded Entities Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Loads Doctrine proxy classes before other filters are applied, preventing errors related to missing fields in lazy-loaded entities. It's recommended to use this filter early in the chain, often decorated with ChainableFilter. ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\Doctrine\DoctrineProxyFilter; use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher; $copier = new DeepCopy(); $copier->addFilter(new ChainableFilter(new DoctrineProxyFilter()), new DoctrineProxyMatcher()); $copy = $copier->copy($object); // $copy contains clones of all entities, including lazy-loaded ones. ``` -------------------------------- ### DoctrineCollectionFilter for Collections Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Handles the copying of Doctrine Collections, ensuring that associated collections within an entity are correctly managed during a deep copy. It uses a PropertyTypeMatcher to identify properties of type Collection. ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter; use DeepCopy\Matcher\PropertyTypeMatcher; $copier = new DeepCopy(); $copier->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Doctrine\Common\Collections\Collection')); $copy = $copier->copy($object); ``` -------------------------------- ### ReplaceFilter for Element Replacement Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Replaces entire elements within a structure (like an array) based on their type. A callback function determines the replacement value, receiving the element itself as input. ```php use DeepCopy\DeepCopy; use DeepCopy\TypeFilter\ReplaceFilter; use DeepCopy\TypeMatcher\TypeMatcher; $copier = new DeepCopy(); $callback = function (MyClass $myClass) { return get_class($myClass); }; $copier->addTypeFilter(new ReplaceFilter($callback), new TypeMatcher('MyClass')); $copy = $copier->copy([new MyClass, 'some string', new MyClass]); // $copy will be ['MyClass', 'some string', 'MyClass'] ``` -------------------------------- ### SetNullFilter for Property Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Sets a specified property to null during the deep copy process. Useful for database records or entities where you want to ensure copied objects do not retain original IDs. ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\SetNullFilter; use DeepCopy\Matcher\PropertyNameMatcher; $object = MyClass::load(123); $copier = new DeepCopy(); $copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id')); $copy = $copier->copy($object); // $copy->id will be null ``` -------------------------------- ### KeepFilter for Property Source: https://github.com/myclabs/deepcopy/blob/1.x/README.md Ensures a specific property of an object remains untouched during a deep copy. This is useful for maintaining associations or references to other objects that should not be cloned. ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\KeepFilter; use DeepCopy\Matcher\PropertyMatcher; $copier = new DeepCopy(); $copier->addFilter(new KeepFilter(), new PropertyMatcher('MyClass', 'category')); $copy = $copier->copy($object); // $copy->category has not been touched ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.