Skip to content

Commit

Permalink
Add has(), isArray() and isScalar() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
otsch committed Jun 1, 2022
1 parent 4f5d964 commit f9a50b4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public function toArray(): array
}

/**
* @param string $key
* @param string|int $key
* @return string|Query|null|mixed
* @throws Exception
*/
public function get(string $key): mixed
public function get(string|int $key): mixed
{
$queryArray = $this->array();

Expand Down Expand Up @@ -152,6 +152,34 @@ public function set(string $key, string|array $value): void
$this->setDirty();
}

/**
* @throws Exception
*/
public function has(int|string $key): bool
{
$this->initArray();

return isset($this->array[$key]);
}

/**
* @throws Exception
*/
public function isArray(int|string $key): bool
{
$this->initArray();

return isset($this->array[$key]) && (is_array($this->array[$key]) || $this->array[$key] instanceof Query);
}

/**
* @throws Exception
*/
public function isScalar(int|string $key): bool
{
return $this->has($key) && !$this->isArray($key);
}

/**
* @param string|mixed[] $value
* @throws Exception
Expand Down
4 changes: 4 additions & 0 deletions tests/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
expect(Query::fromArray(['foo' => 'bar'])->get('foo'))->toBe('bar');
});

it('gets a value by numeric index', function () {
expect(Query::fromArray(['one', 'two'])->get(1))->toBe('two');
});

it('returns null when key doesn\'t exist', function () {
expect(Query::fromString('foo=bar')->get('baz'))->toBeNull();
});
Expand Down
19 changes: 19 additions & 0 deletions tests/HasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Crwlr\QueryString\Query;

it('returns false when it does not have a certain key', function () {
expect(Query::fromArray(['foo' => 'a', 'bar' => 'b'])->has('baz'))->toBeFalse();
});

it('returns true when it has a certain key', function () {
expect(Query::fromArray(['foo' => 'a', 'bar' => 'b'])->has('bar'))->toBeTrue();
});

it('returns true when it does not have a certain numeric index key', function () {
expect(Query::fromArray(['foo', 'bar'])->has(2))->toBeFalse();
});

it('returns false when it has a certain numeric index key', function () {
expect(Query::fromArray(['foo', 'bar'])->has(1))->toBeTrue();
});
47 changes: 47 additions & 0 deletions tests/IsArrayOrScalarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Crwlr\QueryString\Query;

test('isArray returns false when key is not an array', function ($value) {
expect(Query::fromArray(['foo' => $value])->isArray('foo'))->toBeFalse();
})->with([
'string',
true,
false,
1,
1.234,
]);

test('isArray returns false when key does not exist', function () {
expect(Query::fromArray(['foo' => ['test']])->isArray('bar'))->toBeFalse();
});

test('isArray returns true when key is an array', function () {
expect(Query::fromArray(['foo' => ['test']])->isArray('foo'))->toBeTrue();
});

test('isScalar returns false when key is an array', function () {
expect(Query::fromArray(['foo' => ['test']])->isScalar('foo'))->toBeFalse();
});

test('isScalar returns false when key is an instance of Query', function () {
$query = Query::fromArray(['foo' => ['test']]);

$query->get('foo');

expect($query->isScalar('foo'))->toBeFalse();
});

test('isScalar returns false when key does not exist', function () {
expect(Query::fromArray(['foo' => 'test'])->isScalar('bar'))->toBeFalse();
});

test('isScalar returns true when key has a scalar value', function ($value) {
expect(Query::fromArray(['foo' => $value])->isScalar('foo'))->toBeTrue();
})->with([
'string',
true,
false,
1,
1.234,
]);

0 comments on commit f9a50b4

Please sign in to comment.