This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/index/events.php

91 lines
2 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2019-08-16 14:18:14 +00:00
namespace Shimmie2;
2019-08-16 14:18:14 +00:00
/*
* SearchTermParseEvent:
* Signal that a search term needs parsing
*/
class SearchTermParseEvent extends Event
{
public int $id = 0;
public ?string $term = null;
public bool $positive = true;
2019-08-16 14:18:14 +00:00
/** @var string[] */
public array $context = [];
/** @var ImgCondition[] */
public array $img_conditions = [];
/** @var TagCondition[] */
public array $tag_conditions = [];
public ?string $order = null;
2019-08-16 14:18:14 +00:00
/**
* @param string[] $context
*/
public function __construct(int $id, ?string $term = null, array $context = [])
2019-08-16 14:18:14 +00:00
{
2020-01-26 13:19:35 +00:00
parent::__construct();
if ($term == "-" || $term == "*") {
throw new SearchTermParseException("'$term' is not a valid search term");
}
$positive = true;
if (is_string($term) && !empty($term) && ($term[0] == '-')) {
$positive = false;
$term = substr($term, 1);
}
$this->id = $id;
$this->positive = $positive;
2019-08-16 14:18:14 +00:00
$this->term = $term;
$this->context = $context;
}
public function add_querylet(Querylet $q): void
2019-08-16 14:18:14 +00:00
{
$this->add_img_condition(new ImgCondition($q, $this->positive));
}
public function add_img_condition(ImgCondition $c): void
{
$this->img_conditions[] = $c;
}
public function add_tag_condition(TagCondition $c): void
{
$this->tag_conditions[] = $c;
2019-08-16 14:18:14 +00:00
}
}
2024-02-11 15:47:40 +00:00
class SearchTermParseException extends InvalidInput
2019-08-16 14:18:14 +00:00
{
}
class PostListBuildingEvent extends Event
{
/** @var string[] */
public array $search_terms = [];
/** @var array<int,string> */
public array $parts = [];
2019-08-16 14:18:14 +00:00
/**
2024-01-01 03:27:39 +00:00
* @param string[] $search
2019-08-16 14:18:14 +00:00
*/
public function __construct(array $search)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
2019-08-16 14:18:14 +00:00
$this->search_terms = $search;
}
public function add_control(string $html, int $position = 50): void
2019-08-16 14:18:14 +00:00
{
while (isset($this->parts[$position])) {
$position++;
}
$this->parts[$position] = $html;
}
}