currentPath = ''; $this->callback = $callback; $this->fields = $fields; $this->output = array_fill_keys(array_keys($fields), ''); $this->stack = null; $this->arrayStack = null; } public function startDocument(): void { } public function endDocument(): void { \call_user_func($this->callback, $this->output); } public function startObject(): void { \call_user_func($this->callback, 'StartObject'); if ( !empty($this->currentPath) && in_array($this->currentPath, $this->fields) && $this->stack === null ) { $this->stack = []; } } public function endObject(): void { \call_user_func($this->callback, 'EndObject'); if ( $this->stack !== null ) { if ( empty($this->stack) && ($k = array_search($this->currentPath, $this->fields, true)) !== false ) { $this->output[$k] = $this->currentValue; $this->reset(); } else { // Sinon, retirer le dernier chemin de la variable currentPath $this->currentPath = substr($this->currentPath, 0, strrpos($this->currentPath, '/')); } } else { // Sinon, retirer le dernier chemin de la variable currentPath $this->currentPath = substr($this->currentPath, 0, strrpos($this->currentPath, '/')); } } public function startArray(): void { \call_user_func($this->callback, 'StartArray'); if ( $this->stack !== null ) { $this->arrayStack = []; } } public function endArray(): void { \call_user_func($this->callback, 'EndArray'); $this->currentPath = substr($this->currentPath, 0, strrpos($this->currentPath, '/')); } public function key(string $key): void { \call_user_func($this->callback, 'Enter key'); \call_user_func($this->callback, $this->currentPath); if ( $this->stack !== null ) { $this->stack[] = $key; } else { $this->currentPath .= '/' . $key; } \call_user_func($this->callback, $this->currentPath); \call_user_func($this->callback, 'Leave key'); } public function value($value): void { \call_user_func($this->callback, 'Enter value'); \call_user_func($this->callback, $this->currentPath); if ( $this->stack !== null ) { $key = array_pop($this->stack); $this->currentValue[$key] = $value; } else { // Vérifie si le noeud correspond à un champ à récupérer if ( ($k = array_search($this->currentPath, $this->fields, true)) !== false ) { \call_user_func($this->callback, $k); // Ajoute la valeur au tableau de sortie $this->output[$k] = $value; // Réinitialise le chemin $this->reset(); } else { if ( $this->arrayStack !== null ) { } else { // Sinon, retirer le dernier chemin de la variable currentPath $this->currentPath = substr($this->currentPath, 0, strrpos($this->currentPath, '/')); } } } \call_user_func($this->callback, $this->currentPath); \call_user_func($this->callback, 'Leave value'); } public function whitespace(string $whitespace): void { } public function reset(): void { $this->currentPath = ''; $this->currentValue = []; $this->stack = null; } } endif;