-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from nspalo/development
BWA-Dev-4-Weather-App-API-Refactor
- Loading branch information
Showing
23 changed files
with
551 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class BaseException extends Exception | ||
{ | ||
private ?array $errors; | ||
|
||
public function __construct($message = '', $code = 0, ?Throwable $previous = null, ?array $errors = null) | ||
{ | ||
parent::__construct($message, $code, $previous); | ||
|
||
$this->errors = $errors; | ||
} | ||
|
||
public function getErrors(): ?array | ||
{ | ||
return $this->errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
abstract class Resource extends JsonResource | ||
{ | ||
/** | ||
* Return this resource as an array | ||
* | ||
* @noinspection PhpMissingParentCallCommonInspection | ||
* | ||
* @param Request $request | ||
* | ||
* @return string[] | ||
*/ | ||
public function toArray($request): array | ||
{ | ||
return $this->getResponse(); | ||
} | ||
|
||
abstract public function getResponse(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\Collector; | ||
|
||
class ServiceCollector | ||
{ | ||
public static function filterByClass(iterable $objects, string $class): array | ||
{ | ||
$objects = $objects instanceof \Traversable ? \iterator_to_array($objects) : (array) $objects; | ||
|
||
return \array_filter($objects, static function ($object) use ($class): bool { | ||
return $object instanceof $class; | ||
}); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/app/Services/ConfigurationMapper/Exceptions/UnknownServiceConfigurationException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\ConfigurationMapper\Exceptions; | ||
|
||
use App\Exceptions\BaseException; | ||
|
||
class UnknownServiceConfigurationException extends BaseException | ||
{ | ||
} |
12 changes: 12 additions & 0 deletions
12
src/app/Services/ConfigurationMapper/Interfaces/ServiceConfigurationMapperInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\ConfigurationMapper\Interfaces; | ||
|
||
interface ServiceConfigurationMapperInterface | ||
{ | ||
public function loadConfig(?string $config = null): void; | ||
public function map(array $keyMapping): array; | ||
public function getByKey(string $key): ?string; | ||
} |
69 changes: 69 additions & 0 deletions
69
src/app/Services/ConfigurationMapper/ServiceConfigurationMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\ConfigurationMapper; | ||
|
||
use App\Services\ConfigurationMapper\Exceptions\UnknownServiceConfigurationException; | ||
use App\Services\ConfigurationMapper\Interfaces\ServiceConfigurationMapperInterface; | ||
|
||
class ServiceConfigurationMapper implements ServiceConfigurationMapperInterface | ||
{ | ||
private ?array $config; | ||
|
||
public function __construct() | ||
{ | ||
$this->config = null; | ||
} | ||
|
||
/** | ||
* @throws UnknownServiceConfigurationException | ||
*/ | ||
public function loadConfig(?string $config = null): void | ||
{ | ||
if ($config === null) { | ||
throw new UnknownServiceConfigurationException(); | ||
} | ||
|
||
$this->config = \config($config); | ||
} | ||
|
||
/** | ||
* @throws UnknownServiceConfigurationException | ||
*/ | ||
public function map(array $keyMapping): array | ||
{ | ||
if ($this->config === null) { | ||
throw new UnknownServiceConfigurationException(); | ||
} | ||
|
||
$resolved = []; | ||
foreach ($this->config as $key => $value) { | ||
$foundKey = array_search($key, $keyMapping, true); | ||
|
||
if($foundKey !== false) { | ||
$resolved[$foundKey] = $value; | ||
} | ||
} | ||
|
||
return $resolved; | ||
} | ||
|
||
/** | ||
* @throws UnknownServiceConfigurationException | ||
*/ | ||
public function getByKey(string $key): ?string | ||
{ | ||
if ($this->config === null) { | ||
throw new UnknownServiceConfigurationException(); | ||
} | ||
|
||
$value = array_search($key, array_flip($this->config), true); | ||
|
||
if ($value === false) { | ||
return null; | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Oops, something went wrong.