Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ docs/ @JoshuaEstes
/src/SonsOfPHP/**/Logger @JoshuaEstes
/src/SonsOfPHP/**/Money @JoshuaEstes
/src/SonsOfPHP/**/Pager @JoshuaEstes
/src/SonsOfPHP/**/Search @JoshuaEstes
/src/SonsOfPHP/**/Version @JoshuaEstes
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Pager:
- docs/components/pager/*
- src/SonsOfPHP/**/Pager/*

Search:
- docs/components/search/*
- src/SonsOfPHP/**/Search/*

Version:
- docs/components/version/*
- src/SonsOfPHP/**/Version/*
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ To get the diff between two versions, go to https://github.com/SonsOfPHP/sonsofp
* [PR #134](https://github.com/SonsOfPHP/sonsofphp/pull/134) [Pager] New Component
* [PR #170](https://github.com/SonsOfPHP/sonsofphp/pull/170) [Link] New Component (PSR-13)
* [PR #173](https://github.com/SonsOfPHP/sonsofphp/pull/173) [Money] Twig Bridge
* [PR #176](https://github.com/SonsOfPHP/sonsofphp/pull/176) [Search] New Component and Contract

## [0.3.8]

Expand Down
8 changes: 8 additions & 0 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@
"path": "src/SonsOfPHP/Contract/Pager",
"repository": "[email protected]:SonsOfPHP/pager-contract.git"
},
{
"path": "src/SonsOfPHP/Component/Search",
"repository": "[email protected]:SonsOfPHP/search.git"
},
{
"path": "src/SonsOfPHP/Contract/Search",
"repository": "[email protected]:SonsOfPHP/search-contract.git"
},
{
"path": "src/SonsOfPHP/Contract/Version",
"repository": "[email protected]:SonsOfPHP/version-contract.git"
Expand Down
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"psr/log-implementation": "^1.0 || ^2.0 || ^3.0",
"sonsofphp/logger-implementation": "0.3.x-dev",
"sonsofphp/pager-implementation": "0.3.x-dev",
"psr/link-implementation": "^1.0 || ^2.0"
"psr/link-implementation": "^1.0 || ^2.0",
"sonsofphp/search-implementation": "0.3.x-dev"
},
"require": {
"php": ">=8.1",
Expand Down Expand Up @@ -110,7 +111,9 @@
"sonsofphp/money-twig": "self.version",
"sonsofphp/pager-doctrine-collections": "self.version",
"sonsofphp/pager-doctrine-dbal": "self.version",
"sonsofphp/pager-doctrine-orm": "self.version"
"sonsofphp/pager-doctrine-orm": "self.version",
"sonsofphp/search-contract": "self.version",
"sonsofphp/search": "self.version"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -146,6 +149,8 @@
"SonsOfPHP\\Contract\\Logger\\": "src/SonsOfPHP/Contract/Logger",
"SonsOfPHP\\Contract\\Money\\": "src/SonsOfPHP/Contract/Money",
"SonsOfPHP\\Contract\\Pager\\": "src/SonsOfPHP/Contract/Pager",
"SonsOfPHP\\Component\\Search\\": "src/SonsOfPHP/Component/Search",
"SonsOfPHP\\Contract\\Search\\": "src/SonsOfPHP/Contract/Search",
"SonsOfPHP\\Contract\\Version\\": "src/SonsOfPHP/Contract/Version"
},
"exclude-from-classmap": [
Expand All @@ -172,7 +177,8 @@
"src/SonsOfPHP/Bridge/Doctrine/Collections/Pager/Tests",
"src/SonsOfPHP/Bridge/Doctrine/DBAL/Pager/Tests",
"src/SonsOfPHP/Bridge/Doctrine/ORM/Pager/Tests",
"src/SonsOfPHP/Component/Version/Tests"
"src/SonsOfPHP/Component/Version/Tests",
"src/SonsOfPHP/Component/Search/Tests"
]
},
"extra": {
Expand Down
28 changes: 28 additions & 0 deletions docs/components/search/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Search
description: Abstract PHP Search Library that supports many different backends
---

## Installation

```shell
composer require sonsofphp/search
```

## Usage

```php
<?php

use SonsOfPHP\Component\Search\Backend\ElasticBackend;
use SonsOfPHP\Component\Search\Search;
use SonsOfPHP\Component\Search\Query;

$search = new Search(new ElasticBackend(/*...*/));
$query = new Query();
// ...
$pager = $search->query($query); // @var \SonsOfPHP\Contract\Pager\PagerInterface
foreach ($pager as $result) {
// ...
}
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ nav:
- Operators: components/money/operators.md
- Queries: components/money/queries.md
- Pager: components/pager/index.md
- Search: components/search/index.md
- Version: components/version/index.md
- Contracts:
- contracts/index.md
Expand Down
10 changes: 5 additions & 5 deletions src/SonsOfPHP/Component/Pager/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Sons of PHP - Logger
====================
Sons of PHP - Pager
===================

## Learn More

Expand All @@ -11,6 +11,6 @@ Sons of PHP - Logger
[discussions]: https://github.com/orgs/SonsOfPHP/discussions
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp
[contributing]: https://docs.sonsofphp.com/contributing/
[docs]: https://docs.sonsofphp.com/components/logger/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3ALogger
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3ALogger
[docs]: https://docs.sonsofphp.com/components/pager/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3APager
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3APager
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Component/Search/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Component/Search/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
34 changes: 34 additions & 0 deletions src/SonsOfPHP/Component/Search/Backend/MockBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Search\Null;

use SonsOfPHP\Contract\Search\BackendInterface;
use SonsOfPHP\Contract\Search\QueryInterface;

/**
* Mock Backend is mainly used for testing. It allows you to set the results
* that will be returned
*
* @author Joshua Estes <[email protected]>
*/
class MockBackend implements BackendInterface
{
public function __construct(private array $results = []) {}

public function pushResult(mixed $result): self
{
$this->results[] = $result;

return $this;
}

/**
* {@inheritdoc}
*/
public function query(QueryInterface|string $query): iterable
{
return $this->results;
}
}
25 changes: 25 additions & 0 deletions src/SonsOfPHP/Component/Search/Backend/NullBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Search\Null;

use SonsOfPHP\Contract\Search\BackendInterface;
use SonsOfPHP\Contract\Search\QueryInterface;

/**
* Null Backend is used for testing. It does not return any data and all
* methods will be successful
*
* @author Joshua Estes <[email protected]>
*/
class NullBackend implements BackendInterface
{
/**
* {@inheritdoc}
*/
public function query(QueryInterface|string $query): iterable
{
return [];
}
}
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Component/Search/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2022 to Present Joshua Estes

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
112 changes: 112 additions & 0 deletions src/SonsOfPHP/Component/Search/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Search;

use SonsOfPHP\Contract\Search\QueryInterface;

/**
* @author Joshua Estes <[email protected]>
*/
class Query implements QueryInterface, \ArrayAccess, \JsonSerializable
{
private array $fields = [
'offset' => 0,
'length' => null,
];

/**
* {@inheritdoc}
*/
public function has(string $field): bool
{
return array_key_exists($field, $this->fields);
}

/**
* {@inheritdoc}
*/
public function get(string $field): mixed
{
if (!$this->has($field)) {
return null;
}

return $this->fields[$field];
}

/**
* {@inheritdoc}
*/
public function remove(string $field): self
{
if (!in_array(strtolower($field), ['offset', 'length'])) {
unset($this->fields[$field]);
}

return $this;
}

/**
* {@inheritdoc}
*/
public function set(string $field, mixed $value): self
{
if (0 === strcasecmp('offset', $field) || 0 === strcasecmp('length', $field)) {
if (0 === strcasecmp('offset', $field) && !is_int($value)) {
throw new \InvalidArgumentException('Offset must be integer');
}

if (0 === strcasecmp('length', $field) && !is_int($value) && !is_null($value)) {
throw new \InvalidArgumentException('Length must be integer or null');
}
$field = strtolower($field);
}

$this->fields[$field] = $value;

return $this;
}

/**
* {@inheritdoc}
*/
public function getOffset(): int
{
return $this->fields['offset'];
}

/**
* {@inheritdoc}
*/
public function getLength(): ?int
{
return $this->fields['length'];
}

public function offsetExists(mixed $offset): bool
{
return $this->has($offset);
}

public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}

public function offsetSet(mixed $offset, mixed $value): void
{
$this->set($offset, $value);
}

public function offsetUnset(mixed $offset): void
{
$this->remove($offset);
}

public function jsonSerialize(): mixed
{
return $this->fields;
}
}
16 changes: 16 additions & 0 deletions src/SonsOfPHP/Component/Search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Sons of PHP - Search
====================

## Learn More

* [Documentation][docs]
* [Contributing][contributing]
* [Report Issues][issues] and [Submit Pull Requests][pull-requests] in the [Mother Repository][mother-repo]
* Get Help & Support using [Discussions][discussions]

[discussions]: https://github.com/orgs/SonsOfPHP/discussions
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp
[contributing]: https://docs.sonsofphp.com/contributing/
[docs]: https://docs.sonsofphp.com/components/search/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3ASearch
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3ASearch
32 changes: 32 additions & 0 deletions src/SonsOfPHP/Component/Search/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Search;

use SonsOfPHP\Contract\Search\QueryInterface;
use SonsOfPHP\Contract\Search\BackendInterface;

/**
* @author Joshua Estes <[email protected]>
*/
final class Search implements SearchInterface
{
public function __construct(private BackendInterface $backend) {}

/**
* {@inheritdoc}
*/
public function getBackend(): BackendInterface
{
return $this->backend;
}

/**
* {@inheritdoc}
*/
public function query(QueryInterface|string $query): iterable
{
return $this->backend->query($query);
}
}
Loading