-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #368 from tienvx/add-data-helper
Add data helper
- Loading branch information
Showing
2 changed files
with
116 additions
and
106 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,32 @@ | ||
<?php | ||
|
||
namespace Tienvx\Bundle\MbtBundle\Helper; | ||
|
||
use Exception; | ||
use Tienvx\Bundle\MbtBundle\Entity\Data; | ||
|
||
class DataHelper | ||
{ | ||
/** | ||
* @param Data $data | ||
* @param string $key | ||
* @param callable $miss | ||
* @param callable $validate | ||
* | ||
* @return mixed | ||
* | ||
* @throws Exception | ||
*/ | ||
public static function get(Data $data, string $key, callable $miss, callable $validate) | ||
{ | ||
if ($data->has($key)) { | ||
$value = $data->get($key); | ||
$validate($value); | ||
} else { | ||
$value = $miss(); | ||
$data->set($key, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
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