Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SectionAction
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 fromData
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
56
 isExpandAction
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 isNewLeadSection
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 isNewNumberedSection
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 isSandboxAction
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 needsSectionTitlePrepending
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace ContentTranslation;
5
6/**
7 * Represents the possible actions that can be taken when publishing a section.
8 *
9 * @author Nik Gkountas
10 * @license GPL-2.0-or-later
11 * @since 2025.06
12 */
13enum SectionAction: int {
14    case CREATE_LEAD_SECTION = 0;
15    case CREATE_NUMBERED_SECTION = 1;
16    case CREATE_SECTION_AT_END = 2;
17    case EXPAND_LEAD_SECTION = 3;
18    case EXPAND_NUMBERED_SECTION = 4;
19    case REPLACE_NUMBERED_SECTION = 5;
20    case CREATE_SANDBOX_SECTION = 6;
21
22    /**
23     * Determines the appropriate SectionAction based on the given section number and existing title.
24     *
25     * @param int|string $sectionNumber Section index (0 for lead, positive int for numbered, or "new" for appending).
26     * @param string $publishTarget The publishing target ('EXPAND', 'REPLACE', 'SANDBOX', 'NEW_SECTION')
27     *
28     * @return self The corresponding SectionAction case.
29     */
30    public static function fromData( $sectionNumber, string $publishTarget ): self {
31        if ( $publishTarget === 'SANDBOX' ) {
32            return self::CREATE_SANDBOX_SECTION;
33        }
34
35        if ( $sectionNumber === 0 ) {
36            return $publishTarget === 'EXPAND' ? self::EXPAND_LEAD_SECTION : self::CREATE_LEAD_SECTION;
37        } elseif ( (int)$sectionNumber > 0 ) {
38            if ( $publishTarget === 'EXPAND' ) {
39                return self::EXPAND_NUMBERED_SECTION;
40            } elseif ( $publishTarget === 'REPLACE' ) {
41                return self::REPLACE_NUMBERED_SECTION;
42            } else {
43                return self::CREATE_NUMBERED_SECTION;
44            }
45        }
46
47        return self::CREATE_SECTION_AT_END;
48    }
49
50    public function isExpandAction(): bool {
51        return match ( $this ) {
52            self::EXPAND_LEAD_SECTION,
53            self::EXPAND_NUMBERED_SECTION => true,
54            default => false,
55        };
56    }
57
58    public function isNewLeadSection(): bool {
59        return match ( $this ) {
60            self::CREATE_LEAD_SECTION => true,
61            default => false,
62        };
63    }
64
65    public function isNewNumberedSection(): bool {
66        return match ( $this ) {
67            self::CREATE_NUMBERED_SECTION => true,
68            default => false,
69        };
70    }
71
72    public function isSandboxAction(): bool {
73        return match ( $this ) {
74            self::CREATE_SANDBOX_SECTION => true,
75            default => false,
76        };
77    }
78
79    public function needsSectionTitlePrepending(): bool {
80        return match ( $this ) {
81            self::CREATE_NUMBERED_SECTION,
82            self::REPLACE_NUMBERED_SECTION,
83            self::EXPAND_NUMBERED_SECTION => true,
84            default => false,
85        };
86    }
87}