Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sourjya opened this issue Nov 26, 2019 · 1 comment

Comments

@sourjya
Copy link

sourjya commented Nov 26, 2019

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]);
    }
@tiaguinho
Copy link
Owner

Can you open a PR with these changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants