Skip to content

Commit

Permalink
formats
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed Sep 1, 2022
1 parent 2519c07 commit d36d023
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 264 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@

[//]: # (* point radius / bouding box operator ("array" operators))

[//]: # ()
[//]: # (`andFrom('value')` -> raw operator `AND` + kv operator `from:value`)

[//]: # (`andNotFrom('value')` -> raw operator `AND` + kv operator `-from:value`)

[//]: # (`withTweetsCount($min = 0, $max = null)`)

[//]: # (* `notFrom(['a', 'b'])`)
# Twitter Stream API (v2)

[![Tests](https://github.com/redwebcreation/twitter-stream-api/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/redwebcreation/twitter-stream-api/actions/workflows/tests.yml)
Expand Down
4 changes: 0 additions & 4 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ We recommend that you simply Find and Replace in the order below to avoid any is
* `\RWC\TwitterStream\VolumeStream` becomes `\Felix\TwitterStream\Streams\VolumeStream`
* `\RWC\TwitterStream\FilteredStream` becomes `\Felix\TwitterStream\Streams\FilteredStream`
* `\RWC\TwitterStream\Connection` becomes `\Felix\TwitterStream\TwitterConnection`
* `\RWC\TwitterStream\Operators` becomes `\Felix\TwitterStream\Rule\Operators` **These classes are now marked as internal**.
* `\RWC\TwitterStream\RuleBuilder` becomes `\Felix\TwitterStream\Rule\RuleBuilder`
* `\RWC\TwitterStream\RuleManager` becomes `\Felix\TwitterStream\Rule\RuleManager`
* `\RWC\TwitterStream\Rule` becomes `\Felix\TwitterStream\Rule\Rule`

### Other
* The "operator" classes (found in src/Rule/Operators) are now marked as internal.
Expand Down
3 changes: 3 additions & 0 deletions src/Exceptions/TwitterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ public static function fromResponse(ResponseInterface $response): TwitterExcepti
return new self('Too many requests (reset in: ' . $reset . ').', $decoded);
}

/* @phpstan-ignore-next-line */
return new self(json_encode($decoded['errors'][0]), $decoded);
}

public static function fromPayload(array $payload): TwitterException
{
if (array_key_exists('errors', $payload) && count($payload['errors']) > 0) {
/* @phpstan-ignore-next-line */
return new self(json_encode($payload['errors'][0]), $payload);
}

if (array_key_exists('status', $payload) && $payload['status'] === 429) {
return new self('Too many requests (reset in: unknown).', $payload);
}

/* @phpstan-ignore-next-line */
return new self(json_encode($payload), $payload);
}
}
4 changes: 2 additions & 2 deletions src/Generator/CountOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class CountOperator

public function __construct(string $name)
{
$this->name = Str::camel($name);
$this->name = Str::camel($name);
$this->nameAsMethodName = ucfirst($this->name);
$this->snakeCasedName = Str::snake($this->name);
$this->snakeCasedName = Str::snake($this->name);
}

public function methods(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/ParameterizedOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ParameterizedOperator
public function __construct(
string $name,
) {
$this->name = Str::camel($name);
$this->name = Str::camel($name);
$this->nameAsMethodName = ucfirst($this->name);
$this->snakeCasedName = Str::snake($this->name);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Operators/BoundingBoxOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Felix\TwitterStream\Operators;

use Felix\TwitterStream\Support\Flags;

class BoundingBoxOperator implements Operator
{
public function __construct(public Flags $flags, public int|float $westLong, public int|float $southLat, public int|float $eastLong, public int|float $northLat)
{
}

public function compile(): string
{
$join = $this->flags->has(Operator::OR_FLAG) ? 'OR ' : '';

return sprintf('%sbounding_box:[%s %s %s %s]', $join, $this->westLong, $this->southLat, $this->eastLong, $this->northLat);
}
}
19 changes: 19 additions & 0 deletions src/Operators/NotNullCastOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Felix\TwitterStream\Operators;

use Felix\TwitterStream\Support\Flags;

class NotNullCastOperator implements Operator
{
public function __construct(public Flags $flags)
{
}

public function compile(): string
{
$join = $this->flags->has(Operator::OR_FLAG) ? 'OR ' : '';

return sprintf('%s-is:nullcast', $join);
}
}
19 changes: 19 additions & 0 deletions src/Operators/PointRadiusOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Felix\TwitterStream\Operators;

use Felix\TwitterStream\Support\Flags;

class PointRadiusOperator implements Operator
{
public function __construct(public Flags $flags, public string $longitude, public string $latitude, public string $radius)
{
}

public function compile(): string
{
$join = $this->flags->has(Operator::OR_FLAG) ? 'OR ' : '';

return sprintf('%spoint_radius:[%s %s %s]', $join, $this->longitude, $this->latitude, $this->radius);
}
}
19 changes: 19 additions & 0 deletions src/Operators/SampleOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Felix\TwitterStream\Operators;

use Felix\TwitterStream\Support\Flags;

class SampleOperator implements Operator
{
public function __construct(public Flags $flags, public int $percentage)
{
}

public function compile(): string
{
$join = $this->flags->has(Operator::OR_FLAG) ? 'OR ' : '';

return sprintf('%ssample:%s', $join, $this->percentage);
}
}
83 changes: 27 additions & 56 deletions src/RuleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

use BadMethodCallException;
use Felix\TwitterStream\Exceptions\TwitterException;
use Felix\TwitterStream\Operators\BoundingBoxOperator;
use Felix\TwitterStream\Operators\CountOperator;
use Felix\TwitterStream\Operators\KeyValueOperator;
use Felix\TwitterStream\Operators\NotNullCastOperator;
use Felix\TwitterStream\Operators\Operator;
use Felix\TwitterStream\Operators\PointRadiusOperator;
use Felix\TwitterStream\Operators\RawOperator;
use Felix\TwitterStream\Operators\SampleOperator;
use Felix\TwitterStream\Support\Flags;
use Felix\TwitterStream\Support\Str;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -20,34 +24,32 @@
* @method self sample(int $percentage)
* @method self pointRadius(string $longitude, string $latitude, string $radius)
* @method self boundingBox(string $westLongitude, string $southLatitude, string $eastLongitude, string $northLatitude)
*
* @method self orRaw(string|array $property)
* @method self andRaw(string|array $property)
*
* @method self andNotNullcast()
* @method self orNotNullcast()
*/
class RuleBuilder extends _RuleBuilder
{
public const KEY_VALUE_OPERATORS = [
'from' => 'from',
'to' => 'to',
'url' => 'url',
'retweets_of' => 'retweets_of',
'context' => 'context',
'entity' => 'entity',
'conversation_id' => 'conversation_id',
'bio' => 'bio',
'bio_name' => 'bio_name',
'bio_location' => 'bio_location',
'place' => 'place',
'place_country' => 'place_country',
'lang' => 'lang',
'url_title' => 'url_title',
'url_description' => 'url_description',
'url_contains' => 'url_contains',
'source' => 'source',
'in_reply_to_tweet_id' => 'in_reply_to_tweet_id',
'from' => 'from',
'to' => 'to',
'url' => 'url',
'retweets_of' => 'retweets_of',
'context' => 'context',
'entity' => 'entity',
'conversation_id' => 'conversation_id',
'bio' => 'bio',
'bio_name' => 'bio_name',
'bio_location' => 'bio_location',
'place' => 'place',
'place_country' => 'place_country',
'lang' => 'lang',
'url_title' => 'url_title',
'url_description' => 'url_description',
'url_contains' => 'url_contains',
'source' => 'source',
'in_reply_to_tweet_id' => 'in_reply_to_tweet_id',
'retweets_of_tweet_id' => 'retweets_of_tweet_id',
];
public const IS_OPERATORS = [
Expand All @@ -73,10 +75,10 @@ class RuleBuilder extends _RuleBuilder
'listed' => 'listed',
];
public const CUSTOM_OPERATORS = [
'sample' => 'addSampleOperator',
'null_cast' => 'addNotNullCastOperator',
'bounding_box' => 'addBoundingBoxOperator',
'point_radius' => 'addPointRadiusOperator',
'sample' => SampleOperator::class,
'null_cast' => NotNullCastOperator::class,
'bounding_box' => BoundingBoxOperator::class,
'point_radius' => PointRadiusOperator::class,
];

/** @param SplStack<Operator> $operators */
Expand Down Expand Up @@ -111,7 +113,7 @@ public function __call(string $methodName, array $arguments): self
[$name, $flags] = $this->getNameAndFlags($methodName);

if (array_key_exists($name, self::CUSTOM_OPERATORS)) {
return $this->{self::CUSTOM_OPERATORS[$name]}($flags, ...$arguments);
return $this->push(new (self::CUSTOM_OPERATORS[$name])($flags, ...$arguments));
}

return $this->push(match (true) {
Expand Down Expand Up @@ -197,35 +199,4 @@ public function build(): Rule
{
return new Rule($this->compile(), $this->tag);
}

private function addSampleOperator(Flags $flags, int $percent): self
{
$join = $flags->has(Operator::OR_FLAG) ? 'OR ' : '';

return $this->push(new RawOperator($join . 'sample:' . $percent));
}

// bounding_box:[west_long south_lat east_long north_lat]
private function addBoundingBoxOperator(Flags $flags, float $westLong, float $southLat, float $eastLong, float $northLat): self
{
$join = $flags->has(Operator::OR_FLAG) ? 'OR ' : '';
$query = sprintf('%sbounding_box:[%s %s %s %s]', $join, $westLong, $southLat, $eastLong, $northLat);

return $this->push(new RawOperator($query));
}

private function addPointRadiusOperator(Flags $flags, float $longitude, float $latitude, float $radius): self
{
$join = $flags->has(Operator::OR_FLAG) ? 'OR ' : '';
$query = sprintf('%spoint_radius:[%s %s %s]', $join, $longitude, $latitude, $radius);

return $this->push(new RawOperator($query));
}

private function addNotNullCastOperator(Flags $flags): self
{
$join = $flags->has(Operator::OR_FLAG) ? 'OR ' : '';

return $this->push(new RawOperator($join . '-is:nullcast'));
}
}
7 changes: 3 additions & 4 deletions src/Support/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Felix\TwitterStream\Support;

/** @internal */
class Clock
{
protected static array $queue = [];
Expand All @@ -15,14 +16,12 @@ public static function now(): int
return hrtime()[0];
}

/** @internal */
public static function freeze(int $time, callable $callable)
public static function freeze(int $time, callable $callable): void
{
self::queue([$time], $callable);
}

/** @internal */
public static function queue(array $times, callable $callable)
public static function queue(array $times, callable $callable): void
{
Clock::$queue = [...Clock::$queue, ...$times];

Expand Down
Loading

0 comments on commit d36d023

Please sign in to comment.