src/Parser/Parse.php
public function splitInsertsByNewline(array $inserts): array
    {
        $new_deltas = [];

        foreach ($inserts as $insert) {

            if ($insert['insert'] !== null) {

                // Check to see if we are dealing with a media based insert
                if (is_array($insert['insert']) === false) {

                    // We only want to split if there are no attributes
                    if (array_key_exists('attributes', $insert) === false) {

                        // First check for multiple newlines
                        if (preg_match("/[\n]{2,}/", $insert['insert']) !== 0) {

                            $multiple_matches = preg_split("/[\n]{2,}/", $insert['insert']);

                            foreach ($multiple_matches as $k => $match) {

                                $newlines = true;
                                if ($k === count($multiple_matches) - 1) {
                                    $newlines = false;
                                }

                                // Now check for single new matches
                                if (preg_match("/[\n]{1}/", $match) !== 0) {
                                    $new_deltas = array_merge(
                                        $new_deltas,
                                        $this->splitOnSingleNewlineOccurrences($match, $newlines)
                                    );
                                } else {
                                    $new_deltas[] = ['insert' => $match . ($newlines === true ? "\n\n" : null)];
                                }
                            }
                        } else {
                            // No multiple newlines detected, check for single new line matches
                            if (preg_match("/[\n]{1}/", $insert['insert']) !== 0) {
                                $new_deltas = array_merge(
                                    $new_deltas,
                                    $this->splitOnSingleNewlineOccurrences($insert['insert'])
                                );
                            } else {
                                $new_deltas[] = $insert;
                            }
                        }
                    } else {
                        // Attributes, for now return unaffected
                        $new_deltas[] = $insert;
                    }
                } else {
                    // Media based insert, return unaffected
                    $new_deltas[] = $insert;
                }
            }
        }

        return $new_deltas;
    }
public function splitInsertsByNewline(array $inserts): array
    {
        $new_deltas = [];

        foreach ($inserts as $insert) {

            if ($insert['insert'] !== null) {

                // Check to see if we are dealing with a media based insert
                if (is_array($insert['insert']) === false) {

                    // We only want to split if there are no attributes
                    if (array_key_exists('attributes', $insert) === false) {

                        // First check for multiple newlines
                        if (preg_match("/[\n]{2,}/", $insert['insert']) !== 0) {

                            $multiple_matches = preg_split("/[\n]{2,}/", $insert['insert']);

                            foreach ($multiple_matches as $k => $match) {

                                $newlines = true;
                                if ($k === count($multiple_matches) - 1) {
                                    $newlines = false;
                                }

                                // Now check for single new matches
                                if (preg_match("/[\n]{1}/", $match) !== 0) {
                                    $new_deltas = array_merge(
                                        $new_deltas,
                                        $this->splitOnSingleNewlineOccurrences($match, $newlines)
                                    );
                                } else {
                                    $new_deltas[] = ['insert' => $match . ($newlines === true ? "\n\n" : null)];
                                }
                            }
                        } else {
                            // No multiple newlines detected, check for single new line matches
                            if (preg_match("/[\n]{1}/", $insert['insert']) !== 0) {
                                $new_deltas = array_merge(
                                    $new_deltas,
                                    $this->splitOnSingleNewlineOccurrences($insert['insert'])
                                );
                            } else {
                                $new_deltas[] = $insert;
                            }
                        }
                    } else {
                        // Attributes, for now return unaffected
                        $new_deltas[] = $insert;
                    }
                } else {
                    // Media based insert, return unaffected
                    $new_deltas[] = $insert;
                }
            }
        }

        return $new_deltas;
    }
public function splitInsertsByNewline ( array $ inserts ) : array { $ new_deltas = [ ] ; foreach ( $ inserts as $ insert ) { if ( $ insert [ 'insert' ] !== null ) { // Check to see if we are dealing with a media based insert if ( is_array ( $ insert [ 'insert' ] ) === false ) { // We only want to split if there are no attributes if ( array_key_exists ( 'attributes' , $ insert ) === false ) { // First check for multiple newlines if ( preg_match ( "/[\n]{2,}/" , $ insert [ 'insert' ] ) !== 0 ) { $ multiple_matches = preg_split ( "/[\n]{2,}/" , $ insert [ 'insert' ] ) ; foreach ( $ multiple_matches as $ k => $ match ) { $ newlines = true ; if ( $ k === count ( $ multiple_matches ) - 1 ) { $ newlines = false ; } // Now check for single new matches if ( preg_match ( "/[\n]{1}/" , $ match ) !== 0 ) { $ new_deltas = array_merge ( $ new_deltas , $ this -> splitOnSingleNewlineOccurrences ( $ match , $ newlines ) ) ; } else { $ new_deltas [ ] = [ 'insert' => $ match . ( $ newlines === true ? "\n\n" : null ) ] ; } } } else { // No multiple newlines detected, check for single new line matches if ( preg_match ( "/[\n]{1}/" , $ insert [ 'insert' ] ) !== 0 ) { $ new_deltas = array_merge ( $ new_deltas , $ this -> splitOnSingleNewlineOccurrences ( $ insert [ 'insert' ] ) ) ; } else { $ new_deltas [ ] = $ insert ; } } } else { // Attributes, for now return unaffected $ new_deltas [ ] = $ insert ; } } else { // Media based insert, return unaffected $ new_deltas [ ] = $ insert ; } } } return $ new_deltas ; }
Iterate over the deltas, create new deltas each time a new line is found,
this should make it simpler o work out which delta belongs to which attribute

@param array $inserts

@return array
Iterate over the deltas create new deltas each time a new line is found this should make it simpler o work out which delta belongs to which attribute