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

Feature/global error language setting #13

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Given a version number MAJOR.MINOR.PATCH, increment:
### Added
- Travis CI integration
- Boleto PDF layout option
- Global error language setting
### Changed
- StarkBank\User::setDefault() to StarkBank\Settings::setUser()
### Removed
- PHP 7.0 compatibility

Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,30 @@ $balance = Balance::get($project);
4.2 Set it as a default user in the SDK:

```php
use StarkBank\User;
use StarkBank\Settings;
use StarkBank\Balance;

User::setDefault($project);
Settings::setUser($project);

$balance = Balance::get();
```

Just select the way of passing the project user that is more convenient to you.
On all following examples we will assume a default user has been set.

### 5. Setting up the error language

The error language can also be set in the same way as the default user:

```php
use StarkBank\Settings;

Settings::setLanguage("en-US");
```

Language options are "en-US" for english and "pt-BR" for brazilian portuguese. English is default.


## Testing in Sandbox

Your initial balance is zero. For many operations in Stark Bank, you'll need funds
Expand Down
1 change: 1 addition & 0 deletions src/init.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

require_once("setting.php");
require_once("error.php");
require_once("key.php");
require_once("utils/api.php");
Expand Down
36 changes: 36 additions & 0 deletions src/setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace StarkBank;
use Exception;


class Settings
{
private static $user;
private static $language = "en-US";

public static function getUser()
{
return self::$user;
}

public static function setUser($user)
{
self::$user = $user;
}

public static function getLanguage()
{
return self::$language;
}

public static function setLanguage($language)
{
$acceptedLanguages = ["en-US", "pt-BR"];
if (in_array($language, $acceptedLanguages)) {
self::$language = $language;
return;
}
throw new Exception("language must be one of " . join(", ", $acceptedLanguages));
}
}
14 changes: 1 addition & 13 deletions src/user/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@


class User extends Resource
{
private static $defaultUser;

{
function __construct(&$params)
{
parent::__construct($params);
Expand All @@ -28,16 +26,6 @@ public function privateKey()
{
return PrivateKey::fromPem($this->pem);
}

public static function getDefault()
{
return self::$defaultUser;
}

public static function setDefault($user)
{
self::$defaultUser = $user;
}
}

?>
7 changes: 4 additions & 3 deletions src/utils/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use \Exception;
use EllipticCurve\Ecdsa;
use StarkBank\User;
use StarkBank\Settings;
use StarkBank\Utils\URL;
use StarkBank\Error\InputErrors;
use StarkBank\Error\InternalServerError;
Expand All @@ -31,7 +31,7 @@ class Request
public static function fetch($user, $method, $path, $payload = null, $query = null, $version = "v2")
{
if (is_null($user)) {
$user = User::getDefault();
$user = Settings::getUser();
}
if (is_null($user)) {
throw new Exception("A user is required to access our API. Check our README: https://github.com/starkbank/sdk-php/");
Expand Down Expand Up @@ -60,7 +60,8 @@ public static function fetch($user, $method, $path, $payload = null, $query = nu
"Access-Id" => $user->accessId(),
"Access-Signature" => $signature,
"User-Agent" => "PHP-" . phpversion() . "-SDK-0.4.1",
"Content-Type" => "application/json"
"Content-Type" => "application/json",
"Accept-Language" => Settings::getLanguage()
];

$response = Request::makeRequest($method, $headers, $url, $body);
Expand Down
2 changes: 1 addition & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"id" => $projectId,
"privateKey" => $privateKey
]);
\StarkBank\User::setDefault($project);
\StarkBank\Settings::setUser($project);

echo "\n\nStarting tests\n";

Expand Down