*/ public static function plan( int $doors ): array { $doors = max( 1, $doors ); $specs = []; for ( $n = 1; $n <= $doors; $n++ ) { if ( $n === $doors && $doors > 1 ) { $row = self::PLAN[ count( self::PLAN ) - 1 ]; } elseif ( $n <= count( self::PLAN ) - 1 ) { $row = self::PLAN[ $n - 1 ]; } else { // Past day 23: cycle days 5–23 again. $row = self::PLAN[ 4 + ( ( $n - 24 ) % 19 ) ]; } $specs[ $n ] = self::spec( $row ); } return $specs; } /** * Turns specs into a door_list for Config::sanitize(). $gifts are product ids * for gift-idea doors (used in turn), $freeProduct the product a free-gift * door gives away (0 = none, the door falls back to 10% off), $texts the * translated wording keyed by note. * * @param int[] $gifts * @param array $texts */ public static function doors( int $doors, array $gifts, int $freeProduct, array $texts ): array { $list = []; $gift = 0; foreach ( self::plan( $doors ) as $n => $spec ) { $door = Config::defaultDoor(); $kind = $spec['kind']; if ( 'free_product' === $kind && $freeProduct <= 0 ) { $kind = 'percent'; $spec['amount'] = 10.0; } $door['type'] = $spec['type']; // Content doors keep the default coupon, ready if the owner adds one later. if ( null !== $kind ) { $door['coupon']['kind'] = $kind; $door['coupon']['amount'] = $spec['amount']; $door['coupon']['min_spend'] = $spec['min_spend']; $door['coupon']['validity_hours'] = 'finale' === $spec['note'] ? 0 : 24; if ( 'free_product' === $kind ) { // Only reached when an add-on registers the kind and supplies the product. $door['coupon']['product_id'] = $freeProduct; } } $note = $spec['note']; if ( 'gift' === $note && ! $gifts ) { // No products to suggest: a gift-idea door without a product is just a coupon. $note = null; $door['type'] = 'coupon'; } if ( null !== $note && isset( $texts[ $note ] ) ) { $product = 0; if ( 'gift' === $note ) { $product = $gifts[ $gift % count( $gifts ) ]; ++$gift; } $door['content'] = [ 'title' => $texts[ $note ]['title'], 'message' => $texts[ $note ]['message'], 'image_id' => 0, 'product_id' => $product, 'button_label' => $texts[ $note ]['button'], ]; if ( 'coupon' === $door['type'] ) { $door['type'] = 'coupon_content'; } } $list[ $n ] = $door; } return $list; } private static function spec( array $row ): array { $kind = $row[0]; return [ 'type' => 'content' === $kind ? 'content' : 'coupon', 'kind' => 'content' === $kind ? null : $kind, 'amount' => (float) ( $row[1] ?? 0 ), 'min_spend' => (float) ( $row[2] ?? 0 ), 'note' => $row[3] ?? null, ]; } }