objectManager = $objectManager; $this->expectedClass = $expectedClass; } /** * {@inheritdoc} * @return object * @throws \InvalidArgumentException * @throws \UnexpectedValueException */ public function evaluate(array $data) { if (!isset($data['value'])) { throw new \InvalidArgumentException('Object class name is missing.'); } $className = $data['value']; $result = $this->objectManager->create($className); if ($this->expectedClass && !$result instanceof $this->expectedClass) { throw new \UnexpectedValueException( sprintf("Instance of %s is expected, got %s instead.", $this->expectedClass, get_class($result)) ); } return $result; } }