public function parse($ldif)
    {
        $ldifObject = new Ldif();

        $this->setup($ldif);
        while (!$this->isEndOfLdif()) {
            if ($this->isComment()) {
                $this->addCommentToQueueOrLdif($ldifObject);
                $this->nextLine();
            } elseif ($this->isStartOfEntry()) {
                $ldifObject->addEntry($this->parseEntry());
            } elseif ($this->startsWith(Ldif::DIRECTIVE_VERSION.Ldif::KEY_VALUE_SEPARATOR)) {
                $this->setLdifVersion($ldifObject, $this->getKeyAndValue($this->currentLine())[1]);
                $this->nextLine();
            } elseif ($this->isEndOfEntry()) {
                $this->nextLine();
            } else {
                $this->throwException('Unexpected line in LDIF');
            }
        }
        $this->cleanup();

        return $ldifObject;
    }