*/ private $errors = array(); /** * Records an error. * * @since 0.1.0 * * @param string $field Field name. * @param string $message Message. * @return void */ public function add( string $field, string $message ): void { $this->errors[ $field ][] = $message; } /** * Tells whether any rule failed. * * @since 0.1.0 * * @return bool */ public function fails(): bool { return array() !== $this->errors; } /** * All errors per field. * * @since 0.1.0 * * @return array */ public function errors(): array { return $this->errors; } /** * First error of a field, or an empty string. * * @since 0.1.0 * * @param string $field Field name. * @return string */ public function first( string $field ): string { return $this->errors[ $field ][0] ?? ''; } /** * First error of every failing field, keyed by field. Handy for REST error data. * * @since 0.1.0 * * @return array */ public function first_per_field(): array { return array_map( static function ( array $messages ) { return $messages[0]; }, $this->errors ); } }