Browse Source

Add context options for multiple tags, labels, etc

microsub
J. King 5 years ago
parent
commit
ba32ad2f17
  1. 68
      lib/Context/ExclusionContext.php
  2. 25
      tests/cases/Misc/TestContext.php

68
lib/Context/ExclusionContext.php

@ -11,16 +11,23 @@ use JKingWeb\Arsse\Misc\Date;
class ExclusionContext { class ExclusionContext {
public $folder; public $folder;
public $folders;
public $folderShallow; public $folderShallow;
public $foldersShallow;
public $tag; public $tag;
public $tags;
public $tagName; public $tagName;
public $tagNames;
public $subscription; public $subscription;
public $subscriptions;
public $edition; public $edition;
public $article;
public $editions; public $editions;
public $article;
public $articles; public $articles;
public $label; public $label;
public $labels;
public $labelName; public $labelName;
public $labelNames;
public $annotationTerms; public $annotationTerms;
public $searchTerms; public $searchTerms;
public $titleTerms; public $titleTerms;
@ -70,16 +77,18 @@ class ExclusionContext {
} }
} }
protected function cleanIdArray(array $spec): array { protected function cleanIdArray(array $spec, bool $allowZero = false): array {
$spec = array_values($spec); $spec = array_values($spec);
for ($a = 0; $a < sizeof($spec); $a++) { for ($a = 0; $a < sizeof($spec); $a++) {
if (ValueInfo::id($spec[$a])) { if (ValueInfo::id($spec[$a], $allowZero)) {
$spec[$a] = (int) $spec[$a]; $spec[$a] = (int) $spec[$a];
} else { } else {
$spec[$a] = 0; $spec[$a] = null;
} }
} }
return array_values(array_unique(array_filter($spec))); return array_values(array_unique(array_filter($spec, function ($v) {
return !is_null($v);
})));
} }
protected function cleanStringArray(array $spec): array { protected function cleanStringArray(array $spec): array {
@ -99,22 +108,57 @@ class ExclusionContext {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
public function folders(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanIdArray($spec, true);
}
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function folderShallow(int $spec = null) { public function folderShallow(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
public function foldersShallow(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanIdArray($spec, true);
}
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function tag(int $spec = null) { public function tag(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
public function tags(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanIdArray($spec);
}
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function tagName(string $spec = null) { public function tagName(string $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
public function tagNames(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanStringArray($spec);
}
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function subscription(int $spec = null) { public function subscription(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
public function subscriptions(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanIdArray($spec);
}
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function edition(int $spec = null) { public function edition(int $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
@ -141,10 +185,24 @@ class ExclusionContext {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
public function labels(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanIdArray($spec);
}
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function labelName(string $spec = null) { public function labelName(string $spec = null) {
return $this->act(__FUNCTION__, func_num_args(), $spec); return $this->act(__FUNCTION__, func_num_args(), $spec);
} }
public function labelNames(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanStringArray($spec);
}
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function annotationTerms(array $spec = null) { public function annotationTerms(array $spec = null) {
if (isset($spec)) { if (isset($spec)) {
$spec = $this->cleanStringArray($spec); $spec = $this->cleanStringArray($spec);

25
tests/cases/Misc/TestContext.php

@ -29,10 +29,15 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
'limit' => 10, 'limit' => 10,
'offset' => 5, 'offset' => 5,
'folder' => 42, 'folder' => 42,
'folders' => [12,22],
'folderShallow' => 42, 'folderShallow' => 42,
'foldersShallow' => [0,1],
'tag' => 44, 'tag' => 44,
'tags' => [44, 2112],
'tagName' => "XLIV", 'tagName' => "XLIV",
'tagNames' => ["XLIV", "MMCXII"],
'subscription' => 2112, 'subscription' => 2112,
'subscriptions' => [44, 2112],
'article' => 255, 'article' => 255,
'edition' => 65535, 'edition' => 65535,
'latestArticle' => 47, 'latestArticle' => 47,
@ -48,7 +53,9 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
'editions' => [1,2], 'editions' => [1,2],
'articles' => [1,2], 'articles' => [1,2],
'label' => 2112, 'label' => 2112,
'labels' => [2112, 1984],
'labelName' => "Rush", 'labelName' => "Rush",
'labelNames' => ["Rush", "Orwell"],
'labelled' => true, 'labelled' => true,
'annotated' => true, 'annotated' => true,
'searchTerms' => ["foo", "bar"], 'searchTerms' => ["foo", "bar"],
@ -79,9 +86,19 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
} }
public function testCleanIdArrayValues() { public function testCleanIdArrayValues() {
$methods = ["articles", "editions"]; $methods = ["articles", "editions", "tags", "labels", "subscriptions"];
$in = [1, "2", 3.5, 3.0, "ook", 0, -20, true, false, null, new \DateTime(), -1.0]; $in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime(), -1.0];
$out = [1,2, 3]; $out = [1, 2, 4];
$c = new Context;
foreach ($methods as $method) {
$this->assertSame($out, $c->$method($in)->$method, "Context method $method did not return the expected results");
}
}
public function testCleanFolderIdArrayValues() {
$methods = ["folders", "foldersShallow"];
$in = [1, "2", 3.5, 4.0, 4, "ook", 0, -20, true, false, null, new \DateTime(), -1.0];
$out = [1, 2, 4, 0];
$c = new Context; $c = new Context;
foreach ($methods as $method) { foreach ($methods as $method) {
$this->assertSame($out, $c->$method($in)->$method, "Context method $method did not return the expected results"); $this->assertSame($out, $c->$method($in)->$method, "Context method $method did not return the expected results");
@ -89,7 +106,7 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest {
} }
public function testCleanStringArrayValues() { public function testCleanStringArrayValues() {
$methods = ["searchTerms", "annotationTerms", "titleTerms", "authorTerms"]; $methods = ["searchTerms", "annotationTerms", "titleTerms", "authorTerms", "tagNames", "labelNames"];
$now = new \DateTime; $now = new \DateTime;
$in = [1, 3.0, "ook", 0, true, false, null, $now, ""]; $in = [1, 3.0, "ook", 0, true, false, null, $now, ""];
$out = ["1", "3", "ook", "0", valueInfo::normalize($now, ValueInfo::T_STRING)]; $out = ["1", "3", "ook", "0", valueInfo::normalize($now, ValueInfo::T_STRING)];

Loading…
Cancel
Save