*/ private $keys = array(); /** @var array */ private $values = array(); /** @var array */ private $index = array(); /** * @param mixed $value */ public function set( string $key, $value ): void { if ( array_key_exists( $key, $this->index ) ) { $this->values[ $this->index[ $key ] ] = $value; return; } $this->index[ $key ] = count( $this->keys ); $this->keys[] = $key; $this->values[] = $value; } public function has( string $key ): bool { return array_key_exists( $key, $this->index ); } /** * @param mixed $default * @return mixed */ public function get( string $key, $default = null ) { if ( ! array_key_exists( $key, $this->index ) ) { return $default; } return $this->values[ $this->index[ $key ] ]; } /** @return array Keys in insertion order. */ public function keys(): array { return $this->keys; } public function count(): int { return count( $this->keys ); } /** * A plain PHP array, for the places where key coercion cannot bite because * the keys are known constants. Never used on the canonicalisation path. * * @return array */ public function toArray(): array { $out = array(); foreach ( $this->keys as $i => $key ) { $out[ $key ] = $this->values[ $i ]; } return $out; } }