1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Validator\Tests\Fixtures;
13
14use Symfony\Component\Validator\Exception\NoSuchMetadataException;
15use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
16use Symfony\Component\Validator\MetadataInterface;
17
18class FakeMetadataFactory implements MetadataFactoryInterface
19{
20    protected $metadatas = [];
21
22    public function getMetadataFor($class)
23    {
24        $hash = null;
25
26        if (\is_object($class)) {
27            $hash = spl_object_hash($class);
28            $class = \get_class($class);
29        }
30
31        if (!\is_string($class)) {
32            throw new NoSuchMetadataException(sprintf('No metadata for type "%s".', \gettype($class)));
33        }
34
35        if (!isset($this->metadatas[$class])) {
36            if (isset($this->metadatas[$hash])) {
37                return $this->metadatas[$hash];
38            }
39
40            throw new NoSuchMetadataException(sprintf('No metadata for "%s"', $class));
41        }
42
43        return $this->metadatas[$class];
44    }
45
46    public function hasMetadataFor($class)
47    {
48        $hash = null;
49
50        if (\is_object($class)) {
51            $hash = spl_object_hash($class);
52            $class = \get_class($class);
53        }
54
55        if (!\is_string($class)) {
56            return false;
57        }
58
59        return isset($this->metadatas[$class]) || isset($this->metadatas[$hash]);
60    }
61
62    public function addMetadata($metadata)
63    {
64        $this->metadatas[$metadata->getClassName()] = $metadata;
65    }
66
67    public function addMetadataForValue($value, MetadataInterface $metadata)
68    {
69        $key = \is_object($value) ? spl_object_hash($value) : $value;
70        $this->metadatas[$key] = $metadata;
71    }
72}
73