public function visitComplex(\stdClass $data, Property\ComplexType $property, $path) { if ($this->validate) { parent::visitComplex($data, $property, $path); } $reference = $property->getReference(); if (!empty($reference)) { $class = new ReflectionClass($reference); if ($class->implementsInterface('PSX\\Data\\RecordInterface')) { $record = $class->newInstance(); foreach ($data as $key => $val) { try { $methodName = 'set' . ucfirst($key); $method = $class->getMethod($methodName); if ($method instanceof ReflectionMethod) { $method->invokeArgs($record, array($val)); } } catch (ReflectionException $e) { // method does not exist } } return $record; } elseif ($class->implementsInterface('PSX\\Data\\Record\\FactoryInterface')) { if ($this->factory !== null) { return $this->factory->getFactory($reference)->factory($data); } else { throw new RuntimeException('No factory provided to resolve ' . $path); } } else { try { $class = new ReflectionClass($reference); $constructor = $class->getConstructor(); if ($constructor instanceof ReflectionMethod && $constructor->getNumberOfRequiredParameters() == 1) { return $class->newInstance($data); } } catch (ReflectionException $e) { // class does not exist } } } return new Record($property->getName(), (array) $data); }
/** * @expectedException \RuntimeException */ public function testVisitComplexNoProperties() { $visitor = new ValidationVisitor(); $property = Property::getComplex('test'); $visitor->visitComplex(new \stdClass(), $property, ''); }