4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Doubler;
14 use Prophecy\Exception\Doubler\DoubleException;
15 use Prophecy\Exception\Doubler\ClassNotFoundException;
16 use Prophecy\Exception\Doubler\InterfaceNotFoundException;
21 * Gives simple interface to describe double before creating it.
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
29 private $interfaces = array();
30 private $arguments = null;
34 * Initializes lazy double.
36 * @param Doubler $doubler
38 public function __construct(Doubler $doubler)
40 $this->doubler = $doubler;
44 * Tells doubler to use specific class as parent one for double.
46 * @param string|ReflectionClass $class
48 * @throws \Prophecy\Exception\Doubler\ClassNotFoundException
49 * @throws \Prophecy\Exception\Doubler\DoubleException
51 public function setParentClass($class)
53 if (null !== $this->double) {
54 throw new DoubleException('Can not extend class with already instantiated double.');
57 if (!$class instanceof ReflectionClass) {
58 if (!class_exists($class)) {
59 throw new ClassNotFoundException(sprintf('Class %s not found.', $class), $class);
62 $class = new ReflectionClass($class);
65 $this->class = $class;
69 * Tells doubler to implement specific interface with double.
71 * @param string|ReflectionClass $interface
73 * @throws \Prophecy\Exception\Doubler\InterfaceNotFoundException
74 * @throws \Prophecy\Exception\Doubler\DoubleException
76 public function addInterface($interface)
78 if (null !== $this->double) {
79 throw new DoubleException(
80 'Can not implement interface with already instantiated double.'
84 if (!$interface instanceof ReflectionClass) {
85 if (!interface_exists($interface)) {
86 throw new InterfaceNotFoundException(
87 sprintf('Interface %s not found.', $interface),
92 $interface = new ReflectionClass($interface);
95 $this->interfaces[] = $interface;
99 * Sets constructor arguments.
101 * @param array $arguments
103 public function setArguments(array $arguments = null)
105 $this->arguments = $arguments;
109 * Creates double instance or returns already created one.
111 * @return DoubleInterface
113 public function getInstance()
115 if (null === $this->double) {
116 if (null !== $this->arguments) {
117 return $this->double = $this->doubler->double(
118 $this->class, $this->interfaces, $this->arguments
122 $this->double = $this->doubler->double($this->class, $this->interfaces);
125 return $this->double;