/**
  * {@inheritDoc}
  */
 public function setValue($object, $value = null)
 {
     $embeddedObject = $this->parentProperty->getValue($object);
     if (null === $embeddedObject) {
         $this->instantiator = $this->instantiator ?: new Instantiator();
         $embeddedObject = $this->instantiator->instantiate($this->embeddedClass);
         $this->parentProperty->setValue($object, $embeddedObject);
     }
     $this->childProperty->setValue($embeddedObject, $value);
 }
 /**
  * Note: do not use EmbeddedField strategy. Discriminators will not work.
  *
  * @param $targetDocument
  * @param $document
  *
  * @return object
  */
 protected function hydrateSingle($targetDocument, $document)
 {
     if (is_object($document)) {
         return $document;
     }
     $instantiator = new Instantiator();
     $object = $instantiator->instantiate($targetDocument);
     $hydrator = $this->getDoctrineHydrator($targetDocument);
     $hydrator->hydrate($document, $object);
     return $object;
 }
Ejemplo n.º 3
0
 /**
  * @param string $class
  * @param array $data
  * @return object
  * @throws \Exception
  */
 private function hydrate($class, $data)
 {
     $object = $this->instantiator->instantiate($class);
     $metadata = $this->manager->getMetadataFor($class);
     /** @var PropertyMetadata $property */
     foreach ($metadata->propertyMetadata as $property) {
         $value = isset($data[$property->name]) ? $data[$property->name] : null;
         if ($property->type) {
             $type = $this->manager->getTypeRegisty()->get($property->type);
             $property->setValue($object, $type->transformToPhp($value));
         }
         if (!$value) {
             continue;
         }
         if ($property->reference === PropertyMetadata::REFERENCE_ONE) {
             $property->setValue($object, $this->manager->find($property->target, $value));
         }
         if ($property->reference === PropertyMetadata::REFERENCE_MANY) {
             $result = [];
             foreach ($value as $k => $v) {
                 $result[] = $this->manager->find($property->target, $v);
             }
             $property->setValue($object, $result);
         }
         if ($property->reference === PropertyMetadata::REFERENCE_KEY) {
             $result = new ObjectCollection();
             $type = $this->manager->getTypeRegisty()->get($property->value['name']);
             foreach ($value as $k => $v) {
                 $result[$this->manager->find($property->target, $k)] = $type->transformToPhp($v);
             }
             $property->setValue($object, $result);
         }
         if ($property->embed === PropertyMetadata::EMBED_ONE) {
             if ($property->mapping) {
                 $value = $this->mapping($value, $property->mapping);
             }
             $property->setValue($object, $this->hydrate($property->target, $value));
         }
         if ($property->embed === PropertyMetadata::EMBED_MANY) {
             $result = [];
             foreach ($value as $k => $v) {
                 if ($property->mapping) {
                     $v = $this->mapping($v, $property->mapping);
                 }
                 $result[] = $this->hydrate($property->target, $v);
             }
             $property->setValue($object, $result);
         }
     }
     $event = new DocumentEvent($this->manager, $metadata, $object);
     $this->eventDispatcher->dispatch(Events::POST_LOAD, $event);
     return $object;
 }
 /**
  * @param mixed $value
  *
  * @return array|mixed
  */
 public function hydrate($value)
 {
     $mapping = $this->metadata->fieldMappings[$this->collectionName];
     $targetDocument = $mapping['targetDocument'];
     if (is_object($value)) {
         return $value;
     }
     $instantiator = new Instantiator();
     $object = $instantiator->instantiate($targetDocument);
     $hydrator = $this->getDoctrineHydrator($targetDocument);
     $hydrator->hydrate($value, $object);
     return $object;
 }
 /**
  * Throws predefined exception.
  *
  * @param array          $args
  * @param ObjectProphecy $object
  * @param MethodProphecy $method
  *
  * @throws object
  */
 public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
 {
     if (is_string($this->exception)) {
         $classname = $this->exception;
         $reflection = new ReflectionClass($classname);
         $constructor = $reflection->getConstructor();
         if ($constructor->isPublic() && 0 == $constructor->getNumberOfRequiredParameters()) {
             throw $reflection->newInstance();
         }
         if (!$this->instantiator) {
             $this->instantiator = new Instantiator();
         }
         throw $this->instantiator->instantiate($classname);
     }
     throw $this->exception;
 }
Ejemplo n.º 6
0
 /**
  * @param string|object $class
  * @throws ValidationException
  * @return \JsonSerializable
  */
 private function retrieveClass($class)
 {
     if (is_object($class) === true) {
         $classInstance = $class;
     } else {
         try {
             $classInstance = $this->instantiator->instantiate($class);
         } catch (InvalidArgumentException $e) {
             throw new ClassDoesNotExistException($e->getMessage());
         }
     }
     if (in_array('JsonSerializable', class_implements($classInstance)) === false) {
         throw new InvalidTypeException(sprintf('Class "%s" must implements "JsonSerializable"', get_class($classInstance)));
     }
     return $classInstance;
 }
Ejemplo n.º 7
0
 /**
  * @param EmbeddedMetadataInterface $embeddedMetadata
  * @param array                     $data
  *
  * @return object
  */
 private function getEmbeddedObject(EmbeddedMetadataInterface $embeddedMetadata, array $data)
 {
     $embedded = $this->instantiator->instantiate($embeddedMetadata->getClassAttribute());
     foreach ($embeddedMetadata->getEmbeddableClasses() as $embeddableMetadata) {
         if ($embeddableMetadata instanceof IndexMetadataInterface) {
             $name = $embeddableMetadata->getNameAttribute();
             $type = $embeddableMetadata->getTypeAttribute();
             $value = Type::getType($type)->convertToPHPValue($this->get($data, $name));
             $embeddableMetadata->setValue($embedded, $value);
         }
         if ($embeddableMetadata instanceof EmbeddedMetadataInterface) {
             $object = $this->getEmbeddedObject($embeddableMetadata, $this->get($data, $embeddableMetadata->getPropertyName(), []));
             $embeddableMetadata->setValue($embedded, $object);
         }
     }
     return $embedded;
 }
Ejemplo n.º 8
0
 /**
  * Creates an instance of the data transfer object.
  *
  * @param string $class
  *
  * @return object
  */
 private function getDTOInstance($class)
 {
     static $instantiator;
     if (!$instantiator) {
         $instantiator = new Instantiator();
     }
     return $instantiator->instantiate($class);
 }
Ejemplo n.º 9
0
 public function testInstancesAreNotCloned()
 {
     $className = 'TemporaryClass' . uniqid();
     eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{}');
     $instance = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
     $instance->foo = 'bar';
     $instance2 = $this->instantiator->instantiate(__NAMESPACE__ . '\\' . $className);
     $this->assertObjectNotHasAttribute('foo', $instance2);
 }
 /**
  * {@inheritDoc}
  */
 protected function setUp()
 {
     $this->instantiator = new Instantiator();
     $this->instantiator->instantiate(__CLASS__);
     $this->instantiator->instantiate('ArrayObject');
     $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SimpleSerializableAsset');
     $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\SerializableArrayObjectAsset');
     $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset');
 }
Ejemplo n.º 11
0
 /**
  * Creates double from specific class or/and list of interfaces.
  *
  * @param ReflectionClass   $class
  * @param ReflectionClass[] $interfaces Array of ReflectionClass instances
  * @param array             $args       Constructor arguments
  *
  * @return DoubleInterface
  *
  * @throws \Prophecy\Exception\InvalidArgumentException
  */
 public function double(ReflectionClass $class = null, array $interfaces, array $args = null)
 {
     foreach ($interfaces as $interface) {
         if (!$interface instanceof ReflectionClass) {
             throw new InvalidArgumentException(sprintf("[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n" . "a second argument to `Doubler::double(...)`, but got %s.", is_object($interface) ? get_class($interface) . ' class' : gettype($interface)));
         }
     }
     $classname = $this->createDoubleClass($class, $interfaces);
     $reflection = new ReflectionClass($classname);
     if (null !== $args) {
         return $reflection->newInstanceArgs($args);
     }
     if (null === ($constructor = $reflection->getConstructor()) || $constructor->isPublic() && !$constructor->isFinal()) {
         return $reflection->newInstance();
     }
     if (!$this->instantiator) {
         $this->instantiator = new Instantiator();
     }
     return $this->instantiator->instantiate($classname);
 }
Ejemplo n.º 12
0
 /**
  * Create a new global CI controller instance
  * @param  string         $className
  * @return \CI_Controller
  */
 public function newControllerInstance($className)
 {
     $oldInstance = get_instance();
     if (version_compare(APP_VER, '2.6', '<')) {
         require_once APPPATH . 'controllers/ee.php';
     } else {
         require_once APPPATH . 'core/EE_Controller.php';
     }
     $instantiator = new Instantiator();
     $newInstance = $instantiator->instantiate($className);
     // copy existing controller props over to new instance
     $controllerProperties = get_object_vars($oldInstance);
     foreach ($controllerProperties as $key => $value) {
         $newInstance->{$key} = $value;
     }
     // replace the global instance
     $reflectedClass = new ReflectionClass('CI_Controller');
     $reflectedProperty = $reflectedClass->getProperty('instance');
     $reflectedProperty->setAccessible(true);
     $reflectedProperty->setValue($newInstance);
     // boot the new instance, if necessaory
     if ($newInstance instanceof BootableInterface) {
         $newInstance->boot($this);
     }
     return $newInstance;
 }
Ejemplo n.º 13
0
 /**
  * @param  string       $code
  * @param  string       $className
  * @param  array|string $type
  * @param  boolean      $callOriginalConstructor
  * @param  boolean      $callAutoload
  * @param  array        $arguments
  * @param  boolean      $callOriginalMethods
  * @param  object       $proxyTarget
  *
  * @return object
  */
 protected function getObject($code, $className, $type = '', $callOriginalConstructor = false, $callAutoload = false, array $arguments = array(), $callOriginalMethods = false, $proxyTarget = null)
 {
     $this->evalClass($code, $className);
     if ($callOriginalConstructor && is_string($type) && !interface_exists($type, $callAutoload)) {
         if (count($arguments) == 0) {
             $object = new $className();
         } else {
             $class = new ReflectionClass($className);
             $object = $class->newInstanceArgs($arguments);
         }
     } else {
         try {
             $instantiator = new Instantiator();
             $object = $instantiator->instantiate($className);
         } catch (InstantiatorUnexpectedValueException $exception) {
             if ($exception->getPrevious()) {
                 $exception = $exception->getPrevious();
             }
             throw new PHPUnit_Framework_MockObject_RuntimeException($exception->getMessage());
         } catch (InstantiatorInvalidArgumentException $exception) {
             throw new PHPUnit_Framework_MockObject_RuntimeException($exception->getMessage());
         }
     }
     if ($callOriginalMethods) {
         if (!is_object($proxyTarget)) {
             if (count($arguments) == 0) {
                 $proxyTarget = new $type();
             } else {
                 $class = new ReflectionClass($type);
                 $proxyTarget = $class->newInstanceArgs($arguments);
             }
         }
         $object->__phpunit_setOriginalObject($proxyTarget);
     }
     return $object;
 }
Ejemplo n.º 14
0
 /**
  * Create a new global CI controller instance
  * @param  string $className
  * @return void
  */
 public function newInstance($className)
 {
     $oldInstance = get_instance();
     $instantiator = new Instantiator();
     $newInstance = $instantiator->instantiate($className);
     // copy existing controller props over to new instance
     $controllerProperties = get_object_vars($oldInstance);
     foreach ($controllerProperties as $key => $value) {
         $newInstance->{$key} = $value;
     }
     // replace the global instance
     $reflectedClass = new ReflectionClass('CI_Controller');
     $reflectedProperty = $reflectedClass->getProperty('instance');
     $reflectedProperty->setAccessible(true);
     $reflectedProperty = $reflectedProperty->setValue($newInstance);
     // boot the new instance, if necessaory
     if ($newInstance instanceof BootableInterface) {
         $newInstance->boot($this);
     }
 }
 /**
  * @iterations 20000
  * @group instantiation
  */
 public function testInstantiateUnCloneableAsset()
 {
     $this->instantiator->instantiate('DoctrineTest\\InstantiatorTestAsset\\UnCloneableAsset');
 }
 /**
  * @param ReflectionProperty $parentProperty  property of the embeddable/entity where to write the embeddable to
  * @param ReflectionProperty $childProperty   property of the embeddable class where to write values to
  * @param string             $embeddableClass name of the embeddable class to be instantiated
  *
  * @dataProvider getTestedReflectionProperties
  */
 public function testWillSkipReadingPropertiesFromNullEmbeddable(ReflectionProperty $parentProperty, ReflectionProperty $childProperty, $embeddableClass)
 {
     $embeddedPropertyReflection = new ReflectionEmbeddedProperty($parentProperty, $childProperty, $embeddableClass);
     $instantiator = new Instantiator();
     $this->assertNull($embeddedPropertyReflection->getValue($instantiator->instantiate($parentProperty->getDeclaringClass()->getName())));
 }
Ejemplo n.º 17
0
 /**
  * Creates a new instance of the mapped class, without invoking the constructor.
  *
  * @return object
  */
 public function newInstance()
 {
     return $this->instantiator->instantiate($this->name);
 }