protected function transformData(Scope $scope, $data): array
    {
        $transformer = $scope->transformer();

        // Handle when no transformer is present
        if (empty($transformer)) {
            // No transformation
            return (array) $data;
        }

        // Handle when transformer is a callable
        if (\is_callable($transformer)) {
            // Simply run callable on the data and return the result
            return (array) $transformer($data);
        }

        // Ensure we're working with a real transformer from this point forward.
        if (!($transformer instanceof TransformerInterface)) {
            throw new InvalidTransformerException('Expected a valid transformer');
        }

        // Transform the data, and filter any sparse field-set for the scope.
        $transformedData = $scope->filterData($transformer->getTransformedData($data));

        // Add includes to the payload.
        $includeMap = $scope->includeMap();
        foreach ($scope->resolvedIncludeKeys() as $includeKey) {
            $child = $this->executeTransformerInclude($scope, $includeKey, $includeMap[$includeKey], $data);

            // Create a new child scope.
            $childScope = new Scope($child, $scope->includes()->splice($includeKey), $scope);

            // If working with a ResourceInterface, use it's own key (if present).
            $childKey = $child instanceof ResourceInterface && $child->getResourceKey() ?
                $child->getResourceKey() : $includeKey;

            ArrayHelper::mutate(
                $transformedData,
                $childKey,
                $this->transform($childScope)
            );
        }

        return $transformedData;
    }