tiaguinho / mongodb-cakephp3

An Mongodb datasource for CakePHP 3.0
MIT License
28 stars 29 forks source link

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

Open sourjya opened 4 years ago

sourjya commented 4 years ago

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 commented 4 years ago

Can you open a PR with these changes?