Skip to content

Commit

Permalink
addElement() and removeElement() added to QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Jan 8, 2018
1 parent e94cf78 commit 1fa7491
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ $array = [

QueryBuilder::create($array);

// to add an element to your array
$element = [
'id' => 4,
'title' => 'Patricia Lebsack',
'email' => '[email protected]',
'rate' => 2,
'company' => [
'name' => 'Robel-Corkery',
'catchPhrase' => 'Multi-tiered zero tolerance productivity',
'bs' => 'transition cutting-edge web services'
]
];
$qb->addElement($element, 4);

// to remove an element from array by his key
$qb->removeElement(3);

```

## Data consistency
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/NotExistingElementException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Exceptions;

class NotExistingElementException extends \Exception
{
}
28 changes: 28 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ArrayQuery\Exceptions\EmptyArrayException;
use ArrayQuery\Exceptions\InvalidArrayException;
use ArrayQuery\Exceptions\NotConsistentDataException;
use ArrayQuery\Exceptions\NotExistingElementException;
use ArrayQuery\Exceptions\NotValidCriterionOperatorException;
use ArrayQuery\Exceptions\NotValidKeyElementInArrayException;
use ArrayQuery\Exceptions\NotValidLimitsOfArrayException;
Expand Down Expand Up @@ -87,6 +88,33 @@ private function setArray(array $array)
$this->array = $array;
}

/**
* @param $element
* @param null $key
* @throws NotConsistentDataException
*/
public function addElement($element, $key = null)
{
if (false === ConsistencyChecker::isElementValid($element, $this->array)) {
throw new NotConsistentDataException('Element provided has no consistent data.');
}

$this->array[$key] = $element;
}

/**
* @param $key
* @throws NotExistingElementException
*/
public function removeElement($key)
{
if(!isset($this->array[$key])){
throw new NotExistingElementException(sprintf('Element with key %s does not exists.', $key));
}

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

/**
* @param $key
* @param $value
Expand Down
92 changes: 92 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,98 @@ public function it_should_get_the_first_result_from_a_query()
}
}

/**
* @test
* @expectedException \ArrayQuery\Exceptions\NotConsistentDataException
* @expectedExceptionMessage Element provided has no consistent data.
*/
public function it_throws_NotConsistentDataException_if_an_element_with_not_consistent_data_is_provided()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array);
$element = [
'id' => 234,
'name' => 'Clementine Bauch 23',
'username' => 'Samantha',
'email' => '[email protected]',
];

$qb->addElement($element, 234);
}
}

/**
* @test
*/
public function it_should_add_an_element_to_array()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array);
$element = [
'id' => 23,
'name' => 'Clementine Bauch 23',
'username' => 'Samantha',
'email' => '[email protected]',
'address' => [
'street' => 'Douglas Extension',
'suite' => 'Suite 847',
'city' => 'McKenziehaven',
'zipcode' => '59590-4157',
'geo' => [
'lat' => '-68.6102',
'lng' => '-47.0653'
]
],
'phone' => '1-463-123-4447',
'website' => 'ramiro.info',
'company' => [
'name' => 'Romaguera-Jacobson',
'catchPhrase' => 'Face to face bifurcated interface',
'bs' => 'e-enable strategic applications'
],
'registration_date' => '01/05/2017',
'update_date' => '2017-05-30',
'tags' => [
'apple',
'pinapple',
'pear',
'apple-pie'
]
];

$qb->addElement($element, 23);

$this->assertCount(11, $qb->getResults());
}
}

/**
* @test
* @expectedException \ArrayQuery\Exceptions\NotExistingElementException
* @expectedExceptionMessage Element with key 232 does not exists.
*/
public function it_throws_NotConsistentDataEfffxception_if_an_wrong_key_element_is_provided()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array);
$qb->removeElement(232);
}
}

/**
* @test
*/
public function it_should_remove_an_element_to_array()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array);
$qb->removeElement(2);
$qb->removeElement(3);

$this->assertCount(8, $qb->getResults());
}
}

/**
* @test
*/
Expand Down

0 comments on commit 1fa7491

Please sign in to comment.