Browse Source

Make exclusion contexts return their parent on change

microsub
J. King 5 years ago
parent
commit
18d52ea402
  1. 6
      lib/Context/Context.php
  2. 20
      lib/Context/ExclusionContext.php
  3. 2
      tests/cases/Misc/TestContext.php

6
lib/Context/Context.php

@ -27,7 +27,7 @@ class Context extends ExclusionContext {
public $notMarkedSince; public $notMarkedSince;
public function __construct() { public function __construct() {
$this->not = new ExclusionContext; $this->not = new ExclusionContext($this);
} }
public function __clone() { public function __clone() {
@ -35,6 +35,10 @@ class Context extends ExclusionContext {
$this->not = clone $this->not; $this->not = clone $this->not;
} }
public function __destruct() {
unset($this->not);
}
public function reverse(bool $spec = null) { public function reverse(bool $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }

20
lib/Context/ExclusionContext.php

@ -24,6 +24,24 @@ class ExclusionContext {
public $authorTerms; public $authorTerms;
protected $props = []; protected $props = [];
protected $parent;
public function __construct(self $c = null) {
$this->parent = $c;
}
public function __clone() {
if ($this->parent) {
$p = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1]['object'] ?? null;
if ($p instanceof self) {
$this->parent = $p;
}
}
}
public function __destruct() {
unset($this->parent);
}
protected function act(string $prop, int $set, $value) { protected function act(string $prop, int $set, $value) {
if ($set) { if ($set) {
@ -34,7 +52,7 @@ class ExclusionContext {
$this->props[$prop] = true; $this->props[$prop] = true;
$this->$prop = $value; $this->$prop = $value;
} }
return $this; return $this->parent ?? $this;
} else { } else {
return isset($this->props[$prop]); return isset($this->props[$prop]);
} }

2
tests/cases/Misc/TestContext.php

@ -104,5 +104,7 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
$this->assertEquals($c1->not, $c2->not); $this->assertEquals($c1->not, $c2->not);
$this->assertNotSame($c1, $c2); $this->assertNotSame($c1, $c2);
$this->assertNotSame($c1->not, $c2->not); $this->assertNotSame($c1->not, $c2->not);
$this->assertSame($c1, $c1->not->article(null));
$this->assertSame($c2, $c2->not->article(null));
} }
} }

Loading…
Cancel
Save