1<?php
2
3namespace DeepCopy\Matcher;
4
5use DeepCopy\Reflection\ReflectionHelper;
6use ReflectionException;
7
8/**
9 * Matches a property by its type.
10 *
11 * It is recommended to use {@see DeepCopy\TypeFilter\TypeFilter} instead, as it applies on all occurrences
12 * of given type in copied context (eg. array elements), not just on object properties.
13 *
14 * @final
15 */
16class PropertyTypeMatcher implements Matcher
17{
18    /**
19     * @var string
20     */
21    private $propertyType;
22
23    /**
24     * @param string $propertyType Property type
25     */
26    public function __construct($propertyType)
27    {
28        $this->propertyType = $propertyType;
29    }
30
31    /**
32     * {@inheritdoc}
33     */
34    public function matches($object, $property)
35    {
36        try {
37            $reflectionProperty = ReflectionHelper::getProperty($object, $property);
38        } catch (ReflectionException $exception) {
39            return false;
40        }
41
42        $reflectionProperty->setAccessible(true);
43
44        return $reflectionProperty->getValue($object) instanceof $this->propertyType;
45    }
46}
47