Skip to content

Commit 8587712

Browse files
authored
[TASK] Add interface RuleContainer for RuleSet (#1256)
This covers the maniplation of `Rule`s within the container, and may be implemented by other classes in future (e.g. #1194). Note that the naming is consistent with the current codebase, rather than what the CSS entities are now called: - `Rule` represents what is now called a "declaration"; - `RuleSet` represents what is now called a "declaration block"; - `DeclarationBlock` represents what is now called a "style rule"; - `CSSListItem` (closely) represents what is now generically called a "rule". Renaming things is part of a longer-term plan touched on in #1189.
1 parent 9e18840 commit 8587712

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Please also have a look at our
1010

1111
### Added
1212

13+
- Interface `RuleContainer` for `RuleSet` `Rule` manipulation methods (#1256)
1314
- `RuleSet::removeMatchingRules()` method
1415
(for the implementing classes `AtRuleSet` and `DeclarationBlock`) (#1249)
1516
- `RuleSet::removeAllRules()` method

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ classDiagram
636636
class CSSListItem {
637637
<<interface>>
638638
}
639+
class RuleContainer {
640+
<<interface>>
641+
}
639642
class DeclarationBlock {
640643
}
641644
class RuleSet {
@@ -730,6 +733,7 @@ classDiagram
730733
Positionable <|.. RuleSet: realization
731734
CSSElement <|.. RuleSet: realization
732735
CSSListItem <|.. RuleSet: realization
736+
RuleContainer <|.. RuleSet: realization
733737
RuleSet <|-- AtRuleSet: inheritance
734738
AtRule <|.. AtRuleSet: realization
735739
Renderable <|.. Selector: realization

src/CSSList/CSSBlockList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sabberworm\CSS\Property\Selector;
99
use Sabberworm\CSS\Rule\Rule;
1010
use Sabberworm\CSS\RuleSet\DeclarationBlock;
11+
use Sabberworm\CSS\RuleSet\RuleContainer;
1112
use Sabberworm\CSS\RuleSet\RuleSet;
1213
use Sabberworm\CSS\Value\CSSFunction;
1314
use Sabberworm\CSS\Value\Value;
@@ -95,7 +96,7 @@ public function getAllValues(
9596
);
9697
}
9798
}
98-
} elseif ($element instanceof RuleSet) {
99+
} elseif ($element instanceof RuleContainer) {
99100
foreach ($element->getRules($ruleSearchPattern) as $rule) {
100101
$result = \array_merge(
101102
$result,

src/RuleSet/RuleContainer.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS\RuleSet;
6+
7+
use Sabberworm\CSS\Rule\Rule;
8+
9+
/**
10+
* Represents a CSS item that contains `Rules`, defining the methods to manipulate them.
11+
*/
12+
interface RuleContainer
13+
{
14+
public function addRule(Rule $ruleToAdd, ?Rule $sibling = null): void;
15+
16+
public function removeRule(Rule $ruleToRemove): void;
17+
18+
public function removeMatchingRules(string $searchPattern): void;
19+
20+
public function removeAllRules(): void;
21+
22+
/**
23+
* @param array<Rule> $rules
24+
*/
25+
public function setRules(array $rules): void;
26+
27+
/**
28+
* @return array<int<0, max>, Rule>
29+
*/
30+
public function getRules(?string $searchPattern = null): array;
31+
32+
/**
33+
* @return array<string, Rule>
34+
*/
35+
public function getRulesAssoc(?string $searchPattern = null): array;
36+
}

src/RuleSet/RuleSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Note that `CSSListItem` extends both `Commentable` and `Renderable`,
2828
* so those interfaces must also be implemented by concrete subclasses.
2929
*/
30-
abstract class RuleSet implements CSSElement, CSSListItem, Positionable
30+
abstract class RuleSet implements CSSElement, CSSListItem, Positionable, RuleContainer
3131
{
3232
use CommentContainer;
3333
use Position;

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sabberworm\CSS\CSSElement;
99
use Sabberworm\CSS\CSSList\CSSListItem;
1010
use Sabberworm\CSS\Rule\Rule;
11+
use Sabberworm\CSS\RuleSet\RuleContainer;
1112
use Sabberworm\CSS\Tests\Unit\RuleSet\Fixtures\ConcreteRuleSet;
1213

1314
/**
@@ -41,6 +42,14 @@ public function implementsCSSListItem(): void
4142
self::assertInstanceOf(CSSListItem::class, $this->subject);
4243
}
4344

45+
/**
46+
* @test
47+
*/
48+
public function implementsRuleContainer(): void
49+
{
50+
self::assertInstanceOf(RuleContainer::class, $this->subject);
51+
}
52+
4453
/**
4554
* @return array<string, array{0: list<Rule>, 1: string, 2: list<string>}>
4655
*/

0 commit comments

Comments
 (0)