Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation for conditional query operators and add :notlike #291

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ composer.phar
composer.lock
vendor
tests/spot_test.sqlite
.idea
.idea
/phpunit.phar
/tests/.phpunit.result.cache
/.phpunit.result.cache
/error.log
/rector.php
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,22 @@ a single loaded entity object, or boolean `false`.

### Conditional Queries

Conditional queries allow you to alter the way the database selects your data.
Supported conditions are:

* Less than `<`, `:lt`
* Less than or equal `<=`, `:lte`
* Greater than `>`, `:gt`
* Greater than or equal `>=`, `:gte`
* RegExp `~=`, `=~`, `:regex`
* Like `:like`
* Not Like `:notlike`
* Full Text `:fulltext`
* Full Text Boolean `:fulltext_boolean`
* In `in`, `:in`
* Not `<>`, `!=`, `:ne`, `:not`
* Equals `=`, `:eq`

```php
# All posts with a 'published' status, descending by date_created
$posts = $mapper->all()
Expand All @@ -345,6 +361,12 @@ $posts = $mapper->all()
->where(['id' => [1, 2, 5, 12, 15]]);
```

For custom query operators you may add your own operator. See `\Spot\Query\Operator\Like` for an simple example.

```php
\Spot\Query::addWhereOperator(':youroperator', YourOperator::class);
```

### Joins

Joins are currently not enabled by Spot's query builder. The Doctine DBAL query
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
}
],
"require": {
"php": ">=5.4.0",
"vlucas/valitron": "~1.1",
"doctrine/dbal": "^2.5.4",
"sabre/event": "~2.0"
"php": ">= 8.1.11",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the PHP version is going to be bumped up to PHP8+, then we will have to do a major version release since this is a huge breaking change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @vlucas It looks like I did something wrong with the branch and repository I pushed to. Especially the last commit today was addressed to my fork. The compatiblity for 8.1 nevertheless is given. Are you willing to make a release out of it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can do a major release to support PHP8+.

"vlucas/valitron": "^1.4",
"doctrine/dbal": "^2.13",
"sabre/event": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.5"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 4 additions & 8 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* @package Spot
*/
class Config implements \Serializable
class Config
{
protected $_defaultConnection;
protected $_connections = [];
Expand Down Expand Up @@ -273,16 +273,12 @@ public static function parseDsn($dsn)
return $parsed;
}

/**
* Default serialization behavior is to not attempt to serialize stored
* adapter connections at all (thanks @TheSavior re: Issue #7)
*/
public function serialize()
public function __serialize(): array
{
return serialize([]);
return [];
}

public function unserialize($serialized)
public function __unserialize(array $serialized): void
{
}
}
2 changes: 1 addition & 1 deletion lib/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public function entity()
*
* @inheritdoc
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return $this->toArray();
}
Expand Down
38 changes: 19 additions & 19 deletions lib/Entity/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function toArray($keyColumn = null, $valueColumn = null)
*
* @inheritdoc
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return $this->toArray();
}
Expand Down Expand Up @@ -232,7 +232,7 @@ public function __toString()
*
* @inheritdoc
*/
public function count()
public function count(): int
{
return count($this->results);
}
Expand All @@ -242,7 +242,7 @@ public function count()
*
* @inheritdoc
*/
public function current()
public function current(): mixed
{
return current($this->results);
}
Expand All @@ -252,7 +252,7 @@ public function current()
*
* @inheritdoc
*/
public function key()
public function key(): mixed
{
return key($this->results);
}
Expand All @@ -262,7 +262,7 @@ public function key()
*
* @inheritdoc
*/
public function next()
public function next(): void
{
next($this->results);
}
Expand All @@ -272,7 +272,7 @@ public function next()
*
* @inheritdoc
*/
public function rewind()
public function rewind(): void
{
reset($this->results);
}
Expand All @@ -282,7 +282,7 @@ public function rewind()
*
* @inheritdoc
*/
public function valid()
public function valid(): bool
{
return (current($this->results) !== false);
}
Expand All @@ -292,32 +292,32 @@ public function valid()
*
* @inheritdoc
*/
public function offsetExists($key)
public function offsetExists(mixed $offset): bool
{
return isset($this->results[$key]);
return isset($this->results[$offset]);
}

/**
* SPL - ArrayAccess
*
* @inheritdoc
*/
public function offsetGet($key)
public function offsetGet(mixed $offset): mixed
{
return $this->results[$key];
return $this->results[$offset];
}

/**
* SPL - ArrayAccess
*
* @inheritdoc
*/
public function offsetSet($key, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
if ($key === null) {
return $this->results[] = $value;
if ($offset === null) {
$this->results[] = $value;
} else {
return $this->results[$key] = $value;
$this->results[$offset] = $value;
}
}

Expand All @@ -326,12 +326,12 @@ public function offsetSet($key, $value)
*
* @inheritdoc
*/
public function offsetUnset($key)
public function offsetUnset(mixed $offset): void
{
if (is_int($key)) {
array_splice($this->results, $key, 1);
if (is_int($offset)) {
array_splice($this->results, $offset, 1);
} else {
unset($this->results[$key]);
unset($this->results[$offset]);
}
}
}
2 changes: 1 addition & 1 deletion lib/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function primaryKey();
/**
* Return array for json_encode()
*/
public function jsonSerialize();
public function jsonSerialize(): mixed;

/**
* String representation of the class (JSON)
Expand Down
1 change: 1 addition & 0 deletions lib/Provider/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Laravel extends ServiceProvider
{
protected $config = [];
protected $app = [];

public function __construct($app)
{
Expand Down
27 changes: 14 additions & 13 deletions lib/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Query implements \Countable, \IteratorAggregate, \ArrayAccess, \JsonSerial
'=~' => 'Spot\Query\Operator\RegExp',
':regex' => 'Spot\Query\Operator\RegExp',
':like' => 'Spot\Query\Operator\Like',
':notlike' => 'Spot\Query\Operator\NotLike',
':fulltext' => 'Spot\Query\Operator\FullText',
':fulltext_boolean' => 'Spot\Query\Operator\FullTextBoolean',
'in' => 'Spot\Query\Operator\In',
Expand Down Expand Up @@ -562,7 +563,7 @@ public function offset($offset)
*
* @return int
*/
public function count()
public function count(): int
{
$countCopy = clone $this->builder();
$stmt = $countCopy->select('COUNT(*)')->resetQueryPart('orderBy')->execute();
Expand All @@ -576,7 +577,7 @@ public function count()
*
* @return \Spot\Entity\Collection
*/
public function getIterator()
public function getIterator(): \Traversable
{
// Execute query and return result set for iteration
$result = $this->execute();
Expand All @@ -603,7 +604,7 @@ public function toArray($keyColumn = null, $valueColumn = null)
*
* @inheritdoc
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return $this->toArray();
}
Expand Down Expand Up @@ -749,37 +750,37 @@ public function fieldWithAlias($field, $escaped = true)
*
* @inheritdoc
*/
public function offsetExists($key)
public function offsetExists(mixed $offset): bool
{
$results = $this->getIterator();

return isset($results[$key]);
return isset($results[$offset]);
}

/**
* SPL - ArrayAccess
*
* @inheritdoc
*/
public function offsetGet($key)
public function offsetGet(mixed $offset): mixed
{
$results = $this->getIterator();

return $results[$key];
return $results[$offset];
}

/**
* SPL - ArrayAccess
*
* @inheritdoc
*/
public function offsetSet($key, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
$results = $this->getIterator();
if ($key === null) {
return $results[] = $value;
if ($offset === null) {
$results[] = $value;
} else {
return $results[$key] = $value;
$results[$offset] = $value;
}
}

Expand All @@ -788,10 +789,10 @@ public function offsetSet($key, $value)
*
* @inheritdoc
*/
public function offsetUnset($key)
public function offsetUnset(mixed $offset): void
{
$results = $this->getIterator();
unset($results[$key]);
unset($results[$offset]);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions lib/Query/Operator/NotLike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Spot\Query\Operator;

use Doctrine\DBAL\Query\QueryBuilder;

/**
* @package Spot\Query\Operator
*/
class NotLike
{
/**
* @param QueryBuilder $builder
* @param $column
* @param $value
* @return string
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
return $column . ' NOT LIKE ' . $builder->createPositionalParameter($value);
}
}
Loading