private function buildFieldValue(ClassMetadata $metadata, $propertyPath, $value)
    {
        $type = $metadata->getTypeOfField($propertyPath);

        switch ($type) {
            case 'smallint':
            case 'integer':
            case 'bigint':
                if (!is_int($value)) {
                    throw new \Exception('Expected integer.');
                }
                return intval($value);

            case 'boolean':
                if (!is_bool($value)) {
                    throw new \Exception('Expected boolean.');
                }
                return (bool)$value;

            case 'float':
            case 'double':
            case 'decimal':
                if (!is_numeric($value)) {
                    throw new \Exception('Expected float.');
                }
                return floatval($value);

            case 'datetime':
                return new \DateTime($value);

            case 'string':
                return (string)$value;
        }

        throw new \Exception("Unsupported field type '$type' for path '$propertyPath'.");
    }