Skip to content

syntax error, unexpected 'elseif' (T_ELSEIF) Error in: ROOT\vendor\hayko\mongodb\src\ORM\Document.php, line 70  #33

@sourjya

Description

@sourjya

Hi,
I'm using "hayko/mongodb": "dev-master" via composer in my CakePHP 3.8.6 project.

When I do a find(), I get a error 500 dump, stating:

syntax error, unexpected 'elseif' (T_ELSEIF)
ParseError
Error in: ROOT\vendor\hayko\mongodb\src\ORM\Document.php, line 70

Upon inspecting the file, it appears a closing curly-brace for the if() statement is missing.

Original code:

    public function cakefy()
    {
        $document = [];
       
        foreach ($this->_document as $field => $value) {
            $type = gettype($value);
            if ($type == 'object') {
                switch (get_class($value)) {
                    case 'MongoDB\BSON\ObjectId':
                        $document[$field] = $value->__toString();
                        break;

                    case 'MongoDB\BSON\UTCDateTime':
                        $document[$field] = $value->toDateTime();
                        break;

                    default:     
                        if ($value instanceof \MongoDB\BSON\Serializable) {                  
                                $document[$field] = $this->serializeObjects($value);
                           } else {
                            throw new Exception(get_class($value) . ' conversion not implemented.');
                         }
                // THERE NEEDS TO BE AN ADDITIONAL CURLY BRACE HERE
            } elseif ($type == 'array') {
               $document[$field] = $this->cakefy();
            } else {
                $document[$field] = $value;
            }
        }
        
        $inflector = new \Cake\Utility\Inflector();
        $entityName = '\\App\\Model\\Entity\\'.$inflector->singularize($this->_registryAlias);
        return new $entityName($document, ['markClean' => true, 'markNew' => false, 'source' => $this->_registryAlias]);
    }

If I modify the code and add in the curly brace, things get back to normal and the find() works:

    public function cakefy()
    {
        $document = [];
       
        foreach ($this->_document as $field => $value) {
            $type = gettype($value);
            if ($type == 'object') {
                switch (get_class($value)) {
                    case 'MongoDB\BSON\ObjectId':
                        $document[$field] = $value->__toString();
                        break;

                    case 'MongoDB\BSON\UTCDateTime':
                        $document[$field] = $value->toDateTime();
                        break;

                    default:     
                        if ($value instanceof \MongoDB\BSON\Serializable) {                  
                                $document[$field] = $this->serializeObjects($value);
                           } else {
                            throw new Exception(get_class($value) . ' conversion not implemented.');
                         }
				} // ADDED CLOSING BRACE
            } elseif ($type == 'array') {
               $document[$field] = $this->cakefy();
            } else {
                $document[$field] = $value;
            }
        }
        
        $inflector = new \Cake\Utility\Inflector();
        $entityName = '\\App\\Model\\Entity\\'.$inflector->singularize($this->_registryAlias);
        return new $entityName($document, ['markClean' => true, 'markNew' => false, 'source' => $this->_registryAlias]);
    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions