| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Form\Style; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class CssBuilder |
| 17 |
* |
| 18 |
* Helper for building CSS rules |
| 19 |
* |
| 20 |
* @package IvyForms\Services\Form\StyleHelpers |
| 21 |
*/ |
| 22 |
class CssBuilder |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @var string[] |
| 26 |
*/ |
| 27 |
private array $css = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* Add a CSS rule block |
| 31 |
* |
| 32 |
* @param string|string[] $selectors |
| 33 |
* @param string[] $properties |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function addRule($selectors, array $properties): void |
| 37 |
{ |
| 38 |
if (empty($properties)) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$selectorString = is_array($selectors) ? implode(',', $selectors) : $selectors; |
| 43 |
$this->css[] = $selectorString . ' {'; |
| 44 |
$this->css = array_merge($this->css, $properties); |
| 45 |
$this->css[] = '}'; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Add raw CSS lines |
| 50 |
* |
| 51 |
* @param string[] $lines |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function addLines(array $lines): void |
| 55 |
{ |
| 56 |
$this->css = array_merge($this->css, $lines); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the generated CSS |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function build(): string |
| 65 |
{ |
| 66 |
return implode("\n", $this->css); |
| 67 |
} |
| 68 |
} |
| 69 |
|