private function orderFields(array $fields, array $fieldProperties, array $showFields = null): array
    {
        $originalFields = $fields['fields'];
        $desiredFieldsOrder = $fields['section']['fields'];
        $desiredFieldsOrderFiltered = [];

        if (!is_null($showFields)) {
            foreach ($desiredFieldsOrder as $fieldHandle) {
                $propertyName = $this->handleToPropertyName($fieldHandle, $fieldProperties);
                if (in_array($propertyName, $showFields)) {
                    $desiredFieldsOrderFiltered[] = $propertyName;
                }
            }
        } else {
            $desiredFieldsOrderFiltered = $desiredFieldsOrder;
        }

        $originalFieldsOrder = [];
        $result = [];

        foreach ($originalFields as $field) {
            $handle = array_keys($field)[0];
            if (is_null($showFields) || in_array($handle, $showFields)) {
                $originalFieldsOrder[] = $handle;
            }
        }

        foreach ($desiredFieldsOrderFiltered as $handle) {
            if (is_null($showFields) || in_array($handle, $showFields)) {
                $fieldIndex = array_search($handle, $originalFieldsOrder);
                if ($fieldIndex !== false) {
                    $result[] = $originalFields[$fieldIndex];
                }
            }
        }

        return $result;
    }
private function orderFields(array $fields, array $fieldProperties, array $showFields = null): array
    {
        $originalFields = $fields['fields'];
        $desiredFieldsOrder = $fields['section']['fields'];
        $desiredFieldsOrderFiltered = [];

        if (!is_null($showFields)) {
            foreach ($desiredFieldsOrder as $fieldHandle) {
                $propertyName = $this->handleToPropertyName($fieldHandle, $fieldProperties);
                if (in_array($propertyName, $showFields)) {
                    $desiredFieldsOrderFiltered[] = $propertyName;
                }
            }
        } else {
            $desiredFieldsOrderFiltered = $desiredFieldsOrder;
        }

        $originalFieldsOrder = [];
        $result = [];

        foreach ($originalFields as $field) {
            $handle = array_keys($field)[0];
            if (is_null($showFields) || in_array($handle, $showFields)) {
                $originalFieldsOrder[] = $handle;
            }
        }

        foreach ($desiredFieldsOrderFiltered as $handle) {
            if (is_null($showFields) || in_array($handle, $showFields)) {
                $fieldIndex = array_search($handle, $originalFieldsOrder);
                if ($fieldIndex !== false) {
                    $result[] = $originalFields[$fieldIndex];
                }
            }
        }

        return $result;
    }
private function orderFields ( array $ fields , array $ fieldProperties , array $ showFields = null ) : array { $ originalFields = $ fields [ 'fields' ] ; $ desiredFieldsOrder = $ fields [ 'section' ] [ 'fields' ] ; $ desiredFieldsOrderFiltered = [ ] ; if ( ! is_null ( $ showFields ) ) { foreach ( $ desiredFieldsOrder as $ fieldHandle ) { $ propertyName = $ this -> handleToPropertyName ( $ fieldHandle , $ fieldProperties ) ; if ( in_array ( $ propertyName , $ showFields ) ) { $ desiredFieldsOrderFiltered [ ] = $ propertyName ; } } } else { $ desiredFieldsOrderFiltered = $ desiredFieldsOrder ; } $ originalFieldsOrder = [ ] ; $ result = [ ] ; foreach ( $ originalFields as $ field ) { $ handle = array_keys ( $ field ) [ 0 ] ; if ( is_null ( $ showFields ) || in_array ( $ handle , $ showFields ) ) { $ originalFieldsOrder [ ] = $ handle ; } } foreach ( $ desiredFieldsOrderFiltered as $ handle ) { if ( is_null ( $ showFields ) || in_array ( $ handle , $ showFields ) ) { $ fieldIndex = array_search ( $ handle , $ originalFieldsOrder ) ; if ( $ fieldIndex !== false ) { $ result [ ] = $ originalFields [ $ fieldIndex ] ; } } } return $ result ; }
Make sure the fields are returned in the order you have configured them

@todo I feel this method is lacking a lot of elegance, make better when the brain is energised.

@param array $fields
@param array $fieldProperties
@param array|null $showFields
@return array
Make sure the fields are returned in the order you have configured them