diff options
Diffstat (limited to 'vendor/myclabs/deep-copy/src/DeepCopy/Matcher/PropertyTypeMatcher.php')
-rw-r--r-- | vendor/myclabs/deep-copy/src/DeepCopy/Matcher/PropertyTypeMatcher.php | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/vendor/myclabs/deep-copy/src/DeepCopy/Matcher/PropertyTypeMatcher.php b/vendor/myclabs/deep-copy/src/DeepCopy/Matcher/PropertyTypeMatcher.php new file mode 100644 index 0000000..a6b0c0b --- /dev/null +++ b/vendor/myclabs/deep-copy/src/DeepCopy/Matcher/PropertyTypeMatcher.php @@ -0,0 +1,46 @@ +<?php + +namespace DeepCopy\Matcher; + +use DeepCopy\Reflection\ReflectionHelper; +use ReflectionException; + +/** + * Matches a property by its type. + * + * It is recommended to use {@see DeepCopy\TypeFilter\TypeFilter} instead, as it applies on all occurrences + * of given type in copied context (eg. array elements), not just on object properties. + * + * @final + */ +class PropertyTypeMatcher implements Matcher +{ + /** + * @var string + */ + private $propertyType; + + /** + * @param string $propertyType Property type + */ + public function __construct($propertyType) + { + $this->propertyType = $propertyType; + } + + /** + * {@inheritdoc} + */ + public function matches($object, $property) + { + try { + $reflectionProperty = ReflectionHelper::getProperty($object, $property); + } catch (ReflectionException $exception) { + return false; + } + + $reflectionProperty->setAccessible(true); + + return $reflectionProperty->getValue($object) instanceof $this->propertyType; + } +} |