4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Fixtures;
14 use Symfony\Component\Validator\Exception\NoSuchMetadataException;
15 use Symfony\Component\Validator\MetadataFactoryInterface;
16 use Symfony\Component\Validator\MetadataInterface;
18 class FakeMetadataFactory implements MetadataFactoryInterface
20 protected $metadatas = array();
22 public function getMetadataFor($class)
26 if (is_object($class)) {
27 $hash = spl_object_hash($class);
28 $class = get_class($class);
31 if (!is_string($class)) {
32 throw new NoSuchMetadataException(sprintf('No metadata for type %s', gettype($class)));
35 if (!isset($this->metadatas[$class])) {
36 if (isset($this->metadatas[$hash])) {
37 return $this->metadatas[$hash];
40 throw new NoSuchMetadataException(sprintf('No metadata for "%s"', $class));
43 return $this->metadatas[$class];
46 public function hasMetadataFor($class)
50 if (is_object($class)) {
51 $hash = spl_object_hash($class);
52 $class = get_class($class);
55 if (!is_string($class)) {
59 return isset($this->metadatas[$class]) || isset($this->metadatas[$hash]);
62 public function addMetadata($metadata)
64 $this->metadatas[$metadata->getClassName()] = $metadata;
67 public function addMetadataForValue($value, MetadataInterface $metadata)
69 $key = is_object($value) ? spl_object_hash($value) : $value;
70 $this->metadatas[$key] = $metadata;