From 57f01b5e0635291e7334293a929b005c5172af8c Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 31 Oct 2018 18:40:45 +0100 Subject: migrating php7 Signed-off-by: Maxence Lange --- lib/Model/Index.php | 36 +++++++++------- lib/Model/IndexOptions.php | 49 +++++++++++---------- lib/Model/PlatformWrapper.php | 16 ++++--- lib/Model/ProviderIndexes.php | 15 ++++++- lib/Model/ProviderWrapper.php | 18 +++++--- lib/Model/Runner.php | 70 ++++++++++++++++++------------ lib/Model/SearchRequest.php | 80 ++++++++++++++++++---------------- lib/Model/SearchResult.php | 76 +++++++++++++++++++-------------- lib/Model/Tick.php | 99 +++++++++++++++++++++++++++++++------------ 9 files changed, 282 insertions(+), 177 deletions(-) (limited to 'lib') diff --git a/lib/Model/Index.php b/lib/Model/Index.php index 34ba9dc..dd25e68 100644 --- a/lib/Model/Index.php +++ b/lib/Model/Index.php @@ -30,13 +30,23 @@ declare(strict_types=1); namespace OCA\FullTextSearch\Model; + +use daita\MySmallPhpTools\Traits\TArrayTools; use JsonSerializable; use OCP\FullTextSearch\Model\IIndex; +/** + * Class Index + * + * @package OCA\FullTextSearch\Model + */ class Index implements IIndex, JsonSerializable { + use TArrayTools; + + /** @var string */ private $providerId; @@ -229,11 +239,7 @@ class Index implements IIndex, JsonSerializable { * @return string */ public function getOption(string $option, string $default = ''): string { - if (!array_key_exists($option, $this->options)) { - return $default; - } - - return $this->options[$option]; + return $this->get($option, $this->options, $default); } @@ -244,13 +250,10 @@ class Index implements IIndex, JsonSerializable { * @return int */ public function getOptionInt(string $option, int $default = 0): int { - if (!array_key_exists($option, $this->options)) { - return $default; - } - - return $this->options[$option]; + return $this->getInt($option, $this->options, $default); } + /** * @param int $err * @@ -276,9 +279,8 @@ class Index implements IIndex, JsonSerializable { return array_values(array_slice($this->errors, -1))[0]; } - /** - * + * @return IIndex */ public function resetErrors(): IIndex { $this->setErrors([]); @@ -351,9 +353,9 @@ class Index implements IIndex, JsonSerializable { /** - * @return array + * @return array */ - public function jsonSerialize() { + public function jsonSerialize(): array { return [ 'ownerId' => $this->getOwnerId(), 'providerId' => $this->getProviderId(), @@ -368,14 +370,18 @@ class Index implements IIndex, JsonSerializable { } + /** + * + */ public function __destruct() { unset($this->providerId); unset($this->documentId); + unset($this->source); unset($this->ownerId); unset($this->status); unset($this->options); unset($this->err); - unset($this->message); + unset($this->errors); unset($this->lastIndex); } diff --git a/lib/Model/IndexOptions.php b/lib/Model/IndexOptions.php index 1261bab..e5fdd25 100644 --- a/lib/Model/IndexOptions.php +++ b/lib/Model/IndexOptions.php @@ -1,4 +1,7 @@ options = $options; } + /** * @return array */ @@ -104,14 +125,9 @@ class IndexOptions implements IIndexOptions, JsonSerializable { * @return string */ public function getOption(string $option, string $default = ''): string { - if (array_key_exists($option, $this->options)) { - return $this->options[$option]; - } - - return $default; + return $this->get($option, $this->options, $default); } - /** * @param string $option * @param array $default @@ -119,14 +135,7 @@ class IndexOptions implements IIndexOptions, JsonSerializable { * @return array */ public function getOptionArray(string $option, array $default = []): array { - if (array_key_exists($option, $this->options)) { - $options = $this->options[$option]; - if (is_array($options)) { - return $this->options[$option]; - } - } - - return $default; + return $this->getArray($option, $this->options, $default); } @@ -137,21 +146,15 @@ class IndexOptions implements IIndexOptions, JsonSerializable { * @return bool */ public function getOptionBool(string $option, bool $default): bool { - if (array_key_exists($option, $this->options)) { - $options = $this->options[$option]; - if (is_bool($options)) { - return $this->options[$option]; - } - } - - return $default; + return $this->getBool($option, $this->options, $default); } /** * @return array */ - public function jsonSerialize() { + public function jsonSerialize(): array { return $this->options; } } + diff --git a/lib/Model/PlatformWrapper.php b/lib/Model/PlatformWrapper.php index 6a9673a..2c57dba 100644 --- a/lib/Model/PlatformWrapper.php +++ b/lib/Model/PlatformWrapper.php @@ -1,4 +1,7 @@ class; } @@ -94,7 +98,7 @@ class PlatformWrapper { * * @return PlatformWrapper */ - public function setClass($class) { + public function setClass(string $class): PlatformWrapper { $this->class = $class; return $this; @@ -104,7 +108,7 @@ class PlatformWrapper { /** * @return IFullTextSearchPlatform */ - public function getPlatform() { + public function getPlatform(): IFullTextSearchPlatform { return $this->platform; } @@ -113,7 +117,7 @@ class PlatformWrapper { * * @return PlatformWrapper */ - public function setPlatform($platform) { + public function setPlatform(IFullTextSearchPlatform $platform): PlatformWrapper { $this->platform = $platform; return $this; @@ -123,7 +127,7 @@ class PlatformWrapper { /** * @return string */ - public function getVersion() { + public function getVersion(): string { return $this->version; } @@ -132,7 +136,7 @@ class PlatformWrapper { * * @return PlatformWrapper */ - public function setVersion($version) { + public function setVersion(string $version): PlatformWrapper { $this->version = $version; return $this; diff --git a/lib/Model/ProviderIndexes.php b/lib/Model/ProviderIndexes.php index 3177066..1475631 100644 --- a/lib/Model/ProviderIndexes.php +++ b/lib/Model/ProviderIndexes.php @@ -1,4 +1,7 @@ indexes = $indexes; } @@ -44,7 +55,7 @@ class ProviderIndexes { /** * @return IIndex[] */ - public function getIndexes():array { + public function getIndexes(): array { return $this->indexes; } diff --git a/lib/Model/ProviderWrapper.php b/lib/Model/ProviderWrapper.php index d1de140..05ed653 100644 --- a/lib/Model/ProviderWrapper.php +++ b/lib/Model/ProviderWrapper.php @@ -1,4 +1,7 @@ appId = $appId; $this->provider = $provider; } @@ -63,7 +66,7 @@ class ProviderWrapper { /** * @return string */ - public function getAppId() { + public function getAppId(): string { return $this->appId; } @@ -72,7 +75,7 @@ class ProviderWrapper { * * @return ProviderWrapper */ - public function setAppId($appId) { + public function setAppId(string $appId): ProviderWrapper { $this->appId = $appId; return $this; @@ -82,7 +85,7 @@ class ProviderWrapper { /** * @return IFullTextSearchProvider */ - public function getProvider() { + public function getProvider(): IFullTextSearchProvider { return $this->provider; } @@ -91,7 +94,7 @@ class ProviderWrapper { * * @return ProviderWrapper */ - public function setProvider($provider) { + public function setProvider(IFullTextSearchProvider $provider): ProviderWrapper { $this->provider = $provider; return $this; @@ -101,7 +104,7 @@ class ProviderWrapper { /** * @return string */ - public function getVersion() { + public function getVersion(): string { return $this->version; } @@ -110,7 +113,7 @@ class ProviderWrapper { * * @return ProviderWrapper */ - public function setVersion($version) { + public function setVersion(string $version): ProviderWrapper { $this->version = $version; return $this; @@ -118,3 +121,4 @@ class ProviderWrapper { } + diff --git a/lib/Model/Runner.php b/lib/Model/Runner.php index 37ae39c..ac78684 100644 --- a/lib/Model/Runner.php +++ b/lib/Model/Runner.php @@ -1,4 +1,7 @@ runningService = $runningService; $this->source = $source; @@ -122,7 +133,6 @@ class Runner implements IRunner { * @throws RunnerAlreadyUpException */ public function start() { -// $this->strict = $strict; $this->tickId = $this->runningService->start($this->source); } @@ -167,7 +177,9 @@ class Runner implements IRunner { } usleep(300000); - $this->base->abort(); + if ($this->base !== null) { + $this->base->abort(); + } } $this->pauseRunning(false); @@ -205,7 +217,6 @@ class Runner implements IRunner { */ public function setInfoArray(array $data) { $keys = array_keys($data); - //$this->info['info'] = ''; foreach ($keys as $k) { $this->info[$k] = $data[$k]; } @@ -243,39 +254,37 @@ class Runner implements IRunner { if ($color !== '') { $this->info[$info . 'Colored'] = '<' . $color . '>' . $value . ''; } - - } /** * @return array */ - public function getInfoAll() { + public function getInfoAll(): array { return $this->info; } /** - * @param string $k + * @param string $info * * @return string */ - public function getInfo($k) { - return MiscService::get($k, $this->info, ''); + public function getInfo(string $info): string { + return $this->get($info, $this->info, ''); } /** * @param array $method */ - public function onKeyPress($method) { + public function onKeyPress(array $method) { $this->methodOnKeyPress[] = $method; } /** - * @param $key + * @param string $key */ - public function keyPressed($key) { + public function keyPressed(string $key) { foreach ($this->methodOnKeyPress as $method) { call_user_func($method, $key); } @@ -285,11 +294,14 @@ class Runner implements IRunner { /** * @param array $method */ - public function onInfoUpdate($method) { + public function onInfoUpdate(array $method) { $this->methodOnInfoUpdate[] = $method; } + /** + * + */ public function infoUpdated() { foreach ($this->methodOnInfoUpdate as $method) { call_user_func($method, $this->info); @@ -300,7 +312,7 @@ class Runner implements IRunner { /** * @param array $method */ - public function onNewIndexError($method) { + public function onNewIndexError(array $method) { $this->methodOnIndexError[] = $method; } @@ -328,7 +340,7 @@ class Runner implements IRunner { /** * @param array $method */ - public function onNewIndexResult($method) { + public function onNewIndexResult(array $method) { $this->methodOnIndexResult[] = $method; } @@ -359,7 +371,7 @@ class Runner implements IRunner { * * @throws TickDoesNotExistException */ - private function updateTick($tick, $action) { + private function updateTick(int $tick, string $action) { if ($this->oldAction === $action && ($this->tickUpdate + self::TICK_UPDATE > $tick)) { return; } @@ -376,9 +388,9 @@ class Runner implements IRunner { /** - * @param $tick + * @param int $tick */ - private function updateRamInfo($tick) { + private function updateRamInfo(int $tick) { if (($this->ramUpdate + self::MEMORY_UPDATE) > $tick) { return; } @@ -424,7 +436,7 @@ class Runner implements IRunner { /** * @param bool $pause */ - public function pause($pause) { + public function pause(bool $pause) { $this->paused = $pause; $this->infoUpdated(); } @@ -432,7 +444,7 @@ class Runner implements IRunner { /** * @return bool */ - public function isPaused() { + public function isPaused(): bool { return $this->paused; } @@ -440,13 +452,15 @@ class Runner implements IRunner { /** * @param bool $running */ - public function pauseRunning($running) { + public function pauseRunning(bool $running) { $this->pauseRunning = $running; $this->infoUpdated(); } - - public function isPauseRunning() { + /** + * @return bool + */ + public function isPauseRunning(): bool { return $this->pauseRunning; } @@ -454,7 +468,7 @@ class Runner implements IRunner { /** * @param string $line */ - public function output($line) { + public function output(string $line) { if ($this->outputInterface === null) { return; } diff --git a/lib/Model/SearchRequest.php b/lib/Model/SearchRequest.php index f5730be..bc98d05 100644 --- a/lib/Model/SearchRequest.php +++ b/lib/Model/SearchRequest.php @@ -1,4 +1,7 @@ options)) { - return $this->options[$option]; - } - - return $default; + return $this->get($option, $this->options, $default); } @@ -331,14 +342,7 @@ class SearchRequest implements ISearchRequest, JsonSerializable { * @return array */ public function getOptionArray(string $option, array $default = []): array { - if (array_key_exists($option, $this->options)) { - $options = $this->options[$option]; - if (is_array($options)) { - return $this->options[$option]; - } - } - - return $default; + return $this->getArray($option, $this->options, $default); } @@ -648,7 +652,7 @@ class SearchRequest implements ISearchRequest, JsonSerializable { /** * @return array */ - public function jsonSerialize() { + public function jsonSerialize(): array { return [ 'providers' => $this->getProviders(), 'author' => $this->getAuthor(), @@ -664,40 +668,42 @@ class SearchRequest implements ISearchRequest, JsonSerializable { } - /** - * @param string $json - * - * @return SearchRequest - */ - public static function fromJSON(string $json): SearchRequest { - return self::fromArray(json_decode($json, true)); - } - /** * @param array $arr * * @return SearchRequest */ - public static function fromArray($arr): SearchRequest { + public function importFromArray($arr): SearchRequest { $providers = $arr['providers']; if (!is_array($providers)) { $providers = [$providers]; } - $request = new SearchRequest(); - $request->setProviders($providers); - $request->setAuthor(MiscService::get('author', $arr, '')); - $request->setSearch(MiscService::get('search', $arr, '')); - $request->setPage(MiscService::get('page', $arr, 0)); - $request->setParts(MiscService::get('parts', $arr, [])); - $request->setSize(MiscService::get('size', $arr, 10)); - $request->setOptions(MiscService::get('options', $arr, [])); - $request->setMetaTags(MiscService::get('metatags', $arr, [])); - $request->setSubTags(MiscService::get('subtags', $arr, [])); - $request->setTags(MiscService::get('tags', $arr, [])); + $this->setProviders($providers); + $this->setAuthor($this->get('author', $arr, '')); + $this->setSearch($this->get('search', $arr, '')); + $this->setPage($this->getInt('page', $arr, 0)); + $this->setParts($this->getArray('parts', $arr, [])); + $this->setSize($this->getInt('size', $arr, 10)); + $this->setOptions($this->getArray('options', $arr, [])); + $this->setMetaTags($this->getArray('metatags', $arr, [])); + $this->setSubTags($this->getArray('subtags', $arr, [])); + $this->setTags($this->getArray('tags', $arr, [])); - return $request; + return $this; } + /** + * @param string $json + * + * @return SearchRequest + */ + public static function fromJSON(string $json): SearchRequest { + $searchRequest = new SearchRequest(); + $searchRequest->importFromArray(json_decode($json, true)); + + return $searchRequest; + } + } diff --git a/lib/Model/SearchResult.php b/lib/Model/SearchResult.php index f51a7f0..4d77445 100644 --- a/lib/Model/SearchResult.php +++ b/lib/Model/SearchResult.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OCA\FullTextSearch\Model; + use JsonSerializable; use OCP\FullTextSearch\IFullTextSearchPlatform; use OCP\FullTextSearch\IFullTextSearchProvider; @@ -38,6 +39,11 @@ use OCP\FullTextSearch\Model\ISearchRequest; use OCP\FullTextSearch\Model\ISearchResult; +/** + * Class SearchResult + * + * @package OCA\FullTextSearch\Model + */ class SearchResult implements ISearchResult, JsonSerializable { /** @var IndexDocument[] */ @@ -68,7 +74,12 @@ class SearchResult implements ISearchResult, JsonSerializable { private $request; - public function __construct(SearchRequest $searchRequest = null) { + /** + * SearchResult constructor. + * + * @param SearchRequest $searchRequest + */ + public function __construct(SearchRequest $searchRequest) { $this->request = $searchRequest; } @@ -160,7 +171,7 @@ class SearchResult implements ISearchResult, JsonSerializable { * * @return ISearchResult */ - public function setPlatform($platform): ISearchResult { + public function setPlatform(IFullTextSearchPlatform $platform): ISearchResult { $this->platform = $platform; return $this; @@ -263,9 +274,38 @@ class SearchResult implements ISearchResult, JsonSerializable { /** - * @return array|IndexDocument[]|integer> + * @since 15.0.0 + * + * @param string $category + * @param string $value + * @param int $count + * + * @return ISearchResult */ - public function jsonSerialize() { + public function addAggregation(string $category, string $value, int $count): ISearchResult { + // TODO: Implement addAggregation() method. + + return $this; + } + + /** + * @since 15.0.0 + * + * @param string $category + * + * @return array + */ + public function getAggregations(string $category): array { + // TODO: Implement getAggregations() method. + + return []; + } + + + /** + * @return array + */ + public function jsonSerialize(): array { $providerObj = $this->getProvider(); $provider = []; @@ -300,31 +340,5 @@ class SearchResult implements ISearchResult, JsonSerializable { ]; } - /** - * @since 15.0.0 - * - * @param string $category - * @param string $value - * @param int $count - * - * @return ISearchResult - */ - public function addAggregation(string $category, string $value, int $count): ISearchResult { - // TODO: Implement addAggregation() method. - - return $this; - } - - /** - * @since 15.0.0 - * - * @param string $category - * - * @return array - */ - public function getAggregations(string $category): array { - // TODO: Implement getAggregations() method. - - return []; - } } + diff --git a/lib/Model/Tick.php b/lib/Model/Tick.php index bd7649d..46bb7b2 100644 --- a/lib/Model/Tick.php +++ b/lib/Model/Tick.php @@ -1,4 +1,7 @@ source = $source; $this->id = $id; } @@ -61,7 +81,7 @@ class Tick { /** * @return int */ - public function getId() { + public function getId(): int { return $this->id; } @@ -70,7 +90,7 @@ class Tick { * * @return $this */ - public function setId($id) { + public function setId(int $id): Tick { $this->id = $id; return $this; @@ -80,7 +100,7 @@ class Tick { /** * @return string */ - public function getSource() { + public function getSource(): string { return $this->source; } @@ -88,7 +108,7 @@ class Tick { /** * @return array */ - public function getData() { + public function getData(): array { return $this->data; } @@ -97,7 +117,7 @@ class Tick { * * @return $this */ - public function setData($data) { + public function setData(array $data): Tick { $this->data = $data; return $this; @@ -107,7 +127,7 @@ class Tick { /** * @return int */ - public function getTick() { + public function getTick(): int { return $this->tick; } @@ -116,7 +136,7 @@ class Tick { * * @return $this */ - public function setTick($tick = 0) { + public function setTick(int $tick = 0): Tick { if ($tick === 0) { $tick = time(); } @@ -130,7 +150,7 @@ class Tick { /** * @return int */ - public function getFirstTick() { + public function getFirstTick(): int { return $this->firstTick; } @@ -139,7 +159,7 @@ class Tick { * * @return $this */ - public function setFirstTick($tick = 0) { + public function setFirstTick(int $tick = 0): Tick { if ($tick === 0) { $tick = time(); } @@ -153,7 +173,7 @@ class Tick { /** * @return string */ - public function getStatus() { + public function getStatus(): string { return $this->status; } @@ -162,7 +182,7 @@ class Tick { * * @return $this */ - public function setStatus($status) { + public function setStatus(string $status): Tick { $this->status = $status; return $this; @@ -172,7 +192,7 @@ class Tick { /** * @return string */ - public function getAction() { + public function getAction(): string { return $this->action; } @@ -181,7 +201,7 @@ class Tick { * * @return $this */ - public function setAction($action) { + public function setAction(string $action): Tick { $this->action = $action; return $this; @@ -189,33 +209,56 @@ class Tick { /** - * @param string $key - * @param string|int $value + * @param string $info + * @param string $value + * + * @return $this + */ + public function setInfo(string $info, string $value): Tick { + $this->data[$info] = $value; + + return $this; + } + + /** + * @param string $info + * @param int $value * * @return $this */ - public function setInfo($key, $value) { - $this->data[$key] = $value; + public function setInfoInt(string $info, int $value): Tick { + $this->data[$info] = $value; return $this; } - public function unsetInfo($key) { - unset($this->data[$key]); + /** + * @param string $info + */ + public function unsetInfo(string $info) { + unset($this->data[$info]); } /** - * @param $key - * @param int|string $default + * @param string $info + * @param string $default * - * @return int|string + * @return string */ - public function getInfo($key, $default = '') { - if (!array_key_exists($key, $this->data)) { - return $default; - } + public function getInfo(string $info, string $default = ''): string { + return $this->get($info, $this->data, $default); + } - return $this->data[$key]; + /** + * @param string $info + * @param int $default + * + * @return int + */ + public function getInfoInt(string $info, int $default = 0): int { + return $this->getInt($info, $this->data, $default); } + } + -- cgit v1.2.3