-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set accessor trait to validate the key before accessing.
- Loading branch information
1 parent
e48748e
commit 33a4fd4
Showing
3 changed files
with
61 additions
and
56 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
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BomWeather\Trait; | ||
|
||
/** | ||
* Trait to provide general data validation and access methods. | ||
*/ | ||
trait WeatherDataAccessorTrait { | ||
|
||
/** | ||
* Access the weather data attribute from weather data array. | ||
* | ||
* @param array|null $weatherData | ||
* Array of weather data. | ||
* @param string $key | ||
* Array key to fetch the data. | ||
*/ | ||
public function accessWeatherData(?array $weatherData, string $key): mixed { | ||
if (!$weatherData) { | ||
return NULL; | ||
} | ||
if (!\array_key_exists($key, $weatherData)) { | ||
return ''; | ||
} | ||
return $weatherData[$key] ?? ''; | ||
} | ||
|
||
} |
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