data = $data; } /** * From a job. * * @param Job $job Job. * @return self */ public static function of( Job $job ): self { return new self( $job->params ); } /** * Default options. * * @return array */ public static function defaultOptions(): array { return array( 'mode' => 'create_update', 'only_fields' => array(), 'on_error' => 'import_valid', 'decimal' => '.', 'thousand' => ',', 'rollback' => true, ); } /** * File reference. * * @return string */ public function ref(): string { return (string) ( $this->data['file']['ref'] ?? '' ); } /** * Normalized file path. * * @return string */ public function path(): string { return FileStore::path( $this->ref(), 'normalized.csv' ); } /** * Delimiter. * * @return string */ public function delimiter(): string { return (string) ( $this->data['file']['delimiter'] ?? ',' ); } /** * Data rows (non-blank). * * @return int */ public function rows(): int { return (int) ( $this->data['file']['rows'] ?? 0 ); } /** * Offset after the header. * * @return int */ public function headerEnd(): int { return (int) ( $this->data['file']['header_end'] ?? 0 ); } /** * Header cells. * * @return string[] */ public function header(): array { return (array) ( $this->data['file']['header'] ?? array() ); } /** * Sha256 of the normalized copy. * * @return string */ public function sha256(): string { return (string) ( $this->data['file']['sha256_norm'] ?? '' ); } /** * Mapping: field => column index. * * @return array */ public function mapping(): array { return array_map( 'intval', (array) ( $this->data['mapping']['map'] ?? array() ) ); } /** * One option with default. * * @param string $key Key. * @param mixed $default Default. * @return mixed */ public function option( string $key, $default = null ) { $opts = (array) ( $this->data['options'] ?? array() ) + self::defaultOptions(); return array_key_exists( $key, $opts ) ? $opts[ $key ] : $default; } /** * Decimal separator. * * @return string */ public function decimal(): string { return (string) $this->option( 'decimal', '.' ); } /** * Thousand separator. * * @return string */ public function thousand(): string { return (string) $this->option( 'thousand', ',' ); } /** * Ledger batch id (0 = none, for dry runs). * * @return int */ public function batchId(): int { return (int) ( $this->data['batch_id'] ?? 0 ); } /** * Creation time of the root job (UTC), for the row_key window. * * @return string */ public function rootStarted(): string { return (string) ( $this->data['root_started'] ?? gmdate( 'Y-m-d H:i:s' ) ); } /** * Random key of the root job, part of its row keys ('' for a job created before it existed). * * @return string */ public function rootKey(): string { return (string) ( $this->data['root_key'] ?? '' ); } /** * Root job id of a retry (0 for a root job). * * @return int */ public function retryOf(): int { return (int) ( $this->data['retry_of'] ?? 0 ); } /** * Dry run mode: import_diff|preflight|rollback_preview. * * @return string */ public function mode(): string { return (string) ( $this->data['mode'] ?? 'import_diff' ); } /** * Key of the scan files: the same mapping and separators give the same scan. * * @return string */ public function scanKey(): string { return substr( sha1( $this->sha256() . '|' . wp_json_encode( $this->mapping() ) . '|' . $this->decimal() . $this->thousand() . '|' . $this->delimiter() ), 0, 16 ); } /** * Hash of mapping and options (identifies "the same import"). * * @return string */ public function configHash(): string { return sha1( (string) wp_json_encode( array( $this->data['mapping'] ?? array(), $this->data['options'] ?? array() ) ) ); } /** * Whether the job was created already blocked (on_error = block_all with errors in the preview). * * @return bool */ public function blocked(): bool { return ! empty( $this->data['blocked'] ); } }