Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| config_section | |
0.00% |
0 / 21 |
|
0.00% |
0 / 6 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| set_end_comment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_variable_by_name | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| delete_variables_by_name | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| create_variable | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| to_string | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\search\backend\sphinx; |
| 15 | |
| 16 | /** |
| 17 | * \phpbb\search\backend\sphinx\config_section |
| 18 | * Represents a single section inside the sphinx configuration |
| 19 | */ |
| 20 | class config_section extends config_item |
| 21 | { |
| 22 | /** @var string Section comment */ |
| 23 | private $comment; |
| 24 | |
| 25 | /** @var string Section end comment */ |
| 26 | private $end_comment; |
| 27 | |
| 28 | /** @var array Section variables array */ |
| 29 | private $variables = []; |
| 30 | |
| 31 | /** |
| 32 | * Construct a new section |
| 33 | * |
| 34 | * @param string $name Name of the section |
| 35 | * @param string $comment Comment that should be appended after the name in the |
| 36 | * textual format. |
| 37 | */ |
| 38 | public function __construct(string $name, string $comment) |
| 39 | { |
| 40 | $this->name = $name; |
| 41 | $this->comment = $comment; |
| 42 | $this->end_comment = ''; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Adds a comment after the closing bracket in the textual representation |
| 47 | * |
| 48 | * @param string $end_comment |
| 49 | */ |
| 50 | public function set_end_comment(string $end_comment): void |
| 51 | { |
| 52 | $this->end_comment = $end_comment; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get a variable object by its name |
| 57 | * |
| 58 | * @param string $name The name of the variable that shall be returned |
| 59 | * |
| 60 | * @return config_variable|null The first variable object from this section with the |
| 61 | * given name or null if none was found |
| 62 | */ |
| 63 | public function get_variable_by_name(string $name): config_variable|null |
| 64 | { |
| 65 | for ($i = 0, $size = count($this->variables); $i < $size; $i++) |
| 66 | { |
| 67 | // Make sure this is a variable object and not a comment |
| 68 | if ($this->variables[$i]->get_name() == $name) |
| 69 | { |
| 70 | return $this->variables[$i]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Deletes all variables with the given name |
| 79 | * |
| 80 | * @param string $name The name of the variable objects that are supposed to be removed |
| 81 | */ |
| 82 | public function delete_variables_by_name(string $name) |
| 83 | { |
| 84 | for ($i = 0, $size = count($this->variables); $i < $size; $i++) |
| 85 | { |
| 86 | // Make sure this is a variable object and not a comment |
| 87 | if ($this->variables[$i]->get_name() == $name) |
| 88 | { |
| 89 | array_splice($this->variables, $i, 1); |
| 90 | $i--; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Create a new variable object and append it to the variables list of this section |
| 97 | * |
| 98 | * @param string $name The name for the new variable |
| 99 | * @param string $value The value for the new variable |
| 100 | * |
| 101 | * @return config_variable Variable object that was created |
| 102 | */ |
| 103 | public function create_variable(string $name, string $value): config_variable |
| 104 | { |
| 105 | $this->variables[] = new config_variable($name, $value); |
| 106 | return $this->variables[count($this->variables) - 1]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Turns this object into a string which can be written to a config file |
| 111 | * |
| 112 | * @return string Config data in textual form, parsable for sphinx |
| 113 | */ |
| 114 | public function to_string(): string |
| 115 | { |
| 116 | $content = $this->name . ' ' . $this->comment . "\n{\n"; |
| 117 | |
| 118 | // Make sure we don't get too many newlines after the opening bracket |
| 119 | while (trim($this->variables[0]->to_string()) == '') |
| 120 | { |
| 121 | array_shift($this->variables); |
| 122 | } |
| 123 | |
| 124 | foreach ($this->variables as $variable) |
| 125 | { |
| 126 | $content .= $variable->to_string(); |
| 127 | } |
| 128 | $content .= '}' . $this->end_comment . "\n"; |
| 129 | |
| 130 | return $content; |
| 131 | } |
| 132 | } |