1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Stdlib\Hydrator;
11
12use Zend\ServiceManager\ServiceLocatorInterface;
13
14class DelegatingHydrator implements HydratorInterface
15{
16    /**
17     * @var ServiceLocatorInterface
18     */
19    protected $hydrators;
20
21    /**
22     * Constructor
23     *
24     * @param ServiceLocatorInterface $hydrators
25     */
26    public function __construct(ServiceLocatorInterface $hydrators)
27    {
28        $this->hydrators = $hydrators;
29    }
30
31    /**
32     * {@inheritdoc}
33     */
34    public function hydrate(array $data, $object)
35    {
36        return $this->getHydrator($object)->hydrate($data, $object);
37    }
38
39    /**
40     * {@inheritdoc}
41     */
42    public function extract($object)
43    {
44        return $this->getHydrator($object)->extract($object);
45    }
46
47    /**
48     * Gets hydrator of an object
49     *
50     * @param  object $object
51     * @return HydratorInterface
52     */
53    protected function getHydrator($object)
54    {
55        return $this->hydrators->get(get_class($object));
56    }
57}
58