*/ abstract protected function columns(): array; /** * Every row, active first, then by sort order and name. * * @param bool $active_only Leave out archived ones. * @return array> */ public function all( bool $active_only = false ): array { $where = $active_only ? "WHERE status = 'active'" : "WHERE status <> 'archived'"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( "SELECT * FROM {$this->table} {$where} ORDER BY sort, name, id", ARRAY_A ); return array_map( array( $this, 'hydrate' ), $rows ); } /** * Rows by ID. * * @param int[] $ids IDs. * @return array> Keyed by ID. */ public function many( array $ids ): array { $ids = array_values( array_unique( array_filter( array_map( 'intval', $ids ) ) ) ); if ( array() === $ids ) { return array(); } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT * FROM {$this->table} WHERE id IN (" . self::in( $ids ) . ')', $ids ), ARRAY_A ); $out = array(); foreach ( $rows as $row ) { $out[ (int) $row['id'] ] = $this->hydrate( $row ); } return $out; } public function find( int $id ): ?array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $row = $this->db->get_row( $this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id ), ARRAY_A ); return $row ? $this->hydrate( $row ) : null; } /** * Insert a row. * * @param array $data Column values; settings as an array. * @return int New ID. */ public function create( array $data ): int { $this->db->insert( $this->table, $this->row( $data ) + array( 'created_at' => self::now() ) ); return (int) $this->db->insert_id; } /** * Update a row. * * @param int $id ID. * @param array $data Columns to change. */ public function update( int $id, array $data ): void { $row = $this->row( $data ); if ( array() !== $row ) { $this->db->update( $this->table, $row, array( 'id' => $id ) ); } } /** * Archive rather than delete, so past bookings keep their names. * * @param int $id ID. */ public function archive( int $id ): void { $this->db->update( $this->table, array( 'status' => 'archived' ), array( 'id' => $id ) ); } /** * Known columns from data, settings encoded. * * @param array $data Data. * @return array */ protected function row( array $data ): array { $row = array_intersect_key( $data, $this->columns() ); if ( array_key_exists( 'settings', $data ) ) { $row['settings'] = wp_json_encode( (object) $data['settings'] ); } return $row; } /** * Cast a database row. * * @param array $row Row. * @return array */ protected function hydrate( array $row ): array { foreach ( $this->columns() as $column => $format ) { if ( '%d' === $format && isset( $row[ $column ] ) ) { $row[ $column ] = (int) $row[ $column ]; } } $row['id'] = (int) $row['id']; $row['settings'] = (array) self::json( $row['settings'] ?? null ); return $row; } }