From bac94806efe4e9bb794bcde71a4665cf8644c71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BB=C3=B3=C5=82tak?= Date: Fri, 1 Sep 2023 17:54:35 +0200 Subject: [PATCH] PredicateTemplate and QuadTemplate: make immutable and add with*() methods PredicateTemplate: add $negate parameter to the constructor --- src/termTemplates/PredicateTemplate.php | 30 +++++++++++++++---- src/termTemplates/QuadTemplate.php | 39 ++++++++++++++++++------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/termTemplates/PredicateTemplate.php b/src/termTemplates/PredicateTemplate.php index 4610a03..6a0f6a6 100644 --- a/src/termTemplates/PredicateTemplate.php +++ b/src/termTemplates/PredicateTemplate.php @@ -26,7 +26,6 @@ namespace termTemplates; -use rdfInterface\TermInterface; use rdfInterface\TermCompareInterface; use rdfInterface\QuadCompareInterface; @@ -39,10 +38,11 @@ class PredicateTemplate implements TermCompareInterface, QuadCompareInterface { private QuadTemplate $tmpl; - public function __construct(TermCompareInterface | TermInterface | null $predicate = null, - TermCompareInterface | TermInterface | null $object = null, - TermCompareInterface | TermInterface | null $graph = null) { - $this->tmpl = new QuadTemplate(null, $predicate, $object, $graph); + public function __construct(TermCompareInterface | null $predicate = null, + TermCompareInterface | null $object = null, + TermCompareInterface | null $graph = null, + bool $negate = false) { + $this->tmpl = new QuadTemplate(null, $predicate, $object, $graph, $negate); } public function __toString(): string { @@ -68,4 +68,24 @@ public function getObject(): TermCompareInterface | null { public function getGraph(): TermCompareInterface | null { return $this->tmpl->getGraph(); } + + public function withSubject(TermCompareInterface | string | null $subject = null): self { + return $this->tmpl->withSubject($subject); + } + + public function withPredicate(TermCompareInterface | string | null $predicate = null): self { + return $this->tmpl->withPredicate($predicate); + } + + public function withObject(TermCompareInterface | string | null $object = null): self { + return $this->tmpl->withObject($object); + } + + public function withGraph(TermCompareInterface | string | null $graph = null): self { + return $this->tmpl->withGraph($graph); + } + + public function withNegate(bool $negate): self { + return $this->tmpl->withNegate($negate); + } } diff --git a/src/termTemplates/QuadTemplate.php b/src/termTemplates/QuadTemplate.php index e176741..26a70c3 100644 --- a/src/termTemplates/QuadTemplate.php +++ b/src/termTemplates/QuadTemplate.php @@ -27,7 +27,6 @@ namespace termTemplates; use rdfInterface\QuadInterface; -use rdfInterface\TermInterface; use rdfInterface\TermCompareInterface; use rdfInterface\QuadCompareInterface; use rdfInterface\DefaultGraphInterface; @@ -39,16 +38,16 @@ */ class QuadTemplate implements TermCompareInterface, QuadCompareInterface { - public TermCompareInterface | TermInterface | null $subject; - public TermCompareInterface | TermInterface | null $predicate; - public TermCompareInterface | TermInterface | null $object; - public TermCompareInterface | TermInterface | null $graph; - public bool $negate; + private TermCompareInterface | null $subject; + private TermCompareInterface | null $predicate; + private TermCompareInterface | null $object; + private TermCompareInterface | null $graph; + private bool $negate; - public function __construct(TermCompareInterface | TermInterface | string | null $subject = null, - TermCompareInterface | TermInterface | string | null $predicate = null, - TermCompareInterface | TermInterface | string | null $object = null, - TermCompareInterface | TermInterface | string | null $graph = null, + public function __construct(TermCompareInterface | string | null $subject = null, + TermCompareInterface | string | null $predicate = null, + TermCompareInterface | string | null $object = null, + TermCompareInterface | string | null $graph = null, bool $negate = false) { $this->subject = is_string($subject) ? new ValueTemplate($subject) : $subject; $this->predicate = is_string($predicate) ? new ValueTemplate($predicate) : $predicate; @@ -88,4 +87,24 @@ public function getObject(): TermCompareInterface | null { public function getGraph(): TermCompareInterface | null { return $this->graph; } + + public function withSubject(TermCompareInterface | string | null $subject = null): self { + return new QuadTemplate($subject, $this->predicate, $this->object, $this->graph, $this->negate); + } + + public function withPredicate(TermCompareInterface | string | null $predicate = null): self { + return new QuadTemplate($this->subject, $predicate, $this->object, $this->graph, $this->negate); + } + + public function withObject(TermCompareInterface | string | null $object = null): self { + return new QuadTemplate($this->subject, $this->predicate, $object, $this->graph, $this->negate); + } + + public function withGraph(TermCompareInterface | string | null $graph = null): self { + return new QuadTemplate($this->subject, $this->predicate, $this->object, $graph, $this->negate); + } + + public function withNegate(bool $negate): self { + return new QuadTemplate($this->subject, $this->predicate, $this->object, $this->graph, $negate); + } }