Skip to content

Commit

Permalink
refactor setOrder and setReference'
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpilloud committed Apr 9, 2019
1 parent 396902b commit a0b9a5e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 53 deletions.
26 changes: 26 additions & 0 deletions docs/sdk/client/setOrder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
### setOrder(int $sortBy, string $sortDirection = null)

Permet de définir le critère de tri et l'ordre.

#### Paramètres

| Param | Type | Description |
| --- | --- | --- |
| $sortBy | int | Critère de tri |
| $sortDirection | string | Optionnel, sens du tri, accepte 'asc' ou 'desc' |


#### Constantes

- Client::SORT_BY_PRICE
- Client::SORT_BY_ROOMS
- Client::SORT_BY_SURFACE
- Client::SORT_BY_CREATED_AT

#### Exemple

```php
$Client->setSort(Client::SORT_BY_PRICE);

$Client->setSort(Client::SORT_BY_PRICE, 'asc');
```
6 changes: 3 additions & 3 deletions docs/sdk/client/setId.md → docs/sdk/client/setReference.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### setId(mixed $ref)
### setReference($ref)

Définit l'identifiant Publimmo ou la référence agence de l'objet à chercher.

Expand All @@ -13,6 +13,6 @@ Définit l'identifiant Publimmo ou la référence agence de l'objet à chercher.
#### Exemple

```php
$Client->setId(54589);
$Client->setId('Villa 34');
$Client->setReference(54589);
$Client->setReference('Villa 34');
```
23 changes: 0 additions & 23 deletions docs/sdk/client/setSort.md

This file was deleted.

8 changes: 2 additions & 6 deletions docs/sdk/recherche.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $PublimmoClient = new Client(99999, 'key');

---

[filename](client/setId.md ':include')
[filename](client/setReference.md ':include')

---

Expand Down Expand Up @@ -109,11 +109,7 @@ $PublimmoClient = new Client(99999, 'key');

---

[filename](client/setSort.md ':include')

---

[filename](client/setSortDirection.md ':include')
[filename](client/setOrder.md ':include')

---

Expand Down
24 changes: 14 additions & 10 deletions src/PublimmoPro/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class Client
/** @var int LOCATION_TYPE_YEAR Internal number for yearly location type */
public const LOCATION_TYPE_YEAR = 3;

/** @var int PROMOTION_TYPE_NOT_PROMOTION Internal number all objects but promotion */
public const PROMOTION_TYPE_NOT_PROMOTION = 0;

/** @var int PROMOTION_TYPE_NEW Internal number for new objects type */
public const PROMOTION_TYPE_NEW = 1;

/** @var int PROMOTION_TYPE_PROMOTION Internal number for promotion type */
public const PROMOTION_TYPE_PROMOTION = 2;


/** @var string API_URL holds the api URL */
private const API_URL = 'http://syn.publimmo.ch/api/v1';
Expand Down Expand Up @@ -259,7 +268,7 @@ public function setRegion(int $region)
return $this;
}

public function setId(mixed $ref)
public function setReference($ref)
{
$this->id = $ref;

Expand Down Expand Up @@ -309,27 +318,22 @@ public function setSelfOnly()
return $this;
}

public function setSort(int $sortBy)
public function setOrder(int $sortBy, string $sortDirection = null)
{
if (!in_array($sortBy, [self::SORT_BY_PRICE, self::SORT_BY_ROOMS, self::SORT_BY_SURFACE, self::SORT_BY_CREATED_AT])) {
throw new \InvalidArgumentException('Sort criterion should be one of '.self::SORT_BY_PRICE. '(Client::SORT_BY_PRICE), '.self::SORT_BY_ROOMS. '(Client::SORT_BY_ROOMS), '.self::SORT_BY_SURFACE. '(Client::SORT_BY_SURFACE), '.self::SORT_BY_CREATED_AT. '(Client::SORT_BY_CREATED_AT)');
}

$this->tri = $sortBy;

return $this;
}

public function setSortDirection(string $sortDirection)
{
if ($sortDirection == 'asc') {
$this->triSens = 2;
} elseif ($sortDirection == 'desc') {
$this->triSens = 1;
} else {
} elseif (!empty($sortDirection)) {
throw new \InvalidArgumentException('Sort direction should be either "asc" or "desc"');
}

$this->tri = $sortBy;

return $this;
}

Expand Down
22 changes: 11 additions & 11 deletions tests/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testRegionCanBeSet(): void

public function testIdCanBeSet(): void
{
$queryUrl = $this->Client->setId(8989)->getQueryURL();
$queryUrl = $this->Client->setReference(8989)->getQueryURL();
$this->assertTrue(strpos($queryUrl, 'id=8989') !== false);
}

Expand Down Expand Up @@ -194,38 +194,38 @@ public function testSelfOnlyCanBeSet(): void

public function testSortCanBeSet(): void
{
$queryUrl = $this->Client->setSort(Client::SORT_BY_PRICE)->getQueryURL();
$queryUrl = $this->Client->setOrder(Client::SORT_BY_PRICE)->getQueryURL();
$this->assertTrue(strpos($queryUrl, 'tri='.Client::SORT_BY_PRICE) !== false);

$queryUrl = $this->Client->setSort(Client::SORT_BY_ROOMS)->getQueryURL();
$queryUrl = $this->Client->setOrder(Client::SORT_BY_ROOMS)->getQueryURL();
$this->assertTrue(strpos($queryUrl, 'tri='.Client::SORT_BY_ROOMS) !== false);

$queryUrl = $this->Client->setSort(Client::SORT_BY_SURFACE)->getQueryURL();
$queryUrl = $this->Client->setOrder(Client::SORT_BY_SURFACE)->getQueryURL();
$this->assertTrue(strpos($queryUrl, 'tri='.Client::SORT_BY_SURFACE) !== false);

$queryUrl = $this->Client->setSort(Client::SORT_BY_CREATED_AT)->getQueryURL();
$queryUrl = $this->Client->setOrder(Client::SORT_BY_CREATED_AT)->getQueryURL();
$this->assertTrue(strpos($queryUrl, 'tri='.Client::SORT_BY_CREATED_AT) !== false);
}

public function testSortCanOnlyBeOfPredefinedTypes(): void
{
$this->expectException(InvalidArgumentException::class);
$queryUrl = $this->Client->setSort(6)->getQueryURL();
$queryUrl = $this->Client->setOrder(6)->getQueryURL();
}

public function testTriSensCanBeSet(): void
public function testSortDirectionCanBeSet(): void
{
$queryUrl = $this->Client->setTriSens('asc')->getQueryURL();
$queryUrl = $this->Client->setOrder(Client::SORT_BY_CREATED_AT, 'asc')->getQueryURL();
$this->assertTrue(strpos($queryUrl, 'triSens=2') !== false);

$queryUrl = $this->Client->setTriSens('desc')->getQueryURL();
$queryUrl = $this->Client->setOrder(Client::SORT_BY_CREATED_AT, 'desc')->getQueryURL();
$this->assertTrue(strpos($queryUrl, 'triSens=1') !== false);
}

public function testTriSensCanOnlyBeOfPredefinedType(): void
public function testSortDirectionCanOnlyBeOfPredefinedType(): void
{
$this->expectException(InvalidArgumentException::class);
$queryUrl = $this->Client->setTriSens('asc,desc')->getQueryURL();
$queryUrl = $this->Client->setOrder(Client::SORT_BY_CREATED_AT, 'asc,desc')->getQueryURL();
}

public function testLanguageCanBeSet(): void
Expand Down

0 comments on commit a0b9a5e

Please sign in to comment.