Skip to content

Commit 6811912

Browse files
committed
Helpers
1 parent a5b4ca9 commit 6811912

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
"description": "Key-Value Storage for Laravel via Database",
44
"type": "library",
55
"require": {
6-
"illuminate/support": "^8.0@dev",
7-
"php": "7.2"
6+
"php": "^7.2",
7+
"illuminate/support": "^7.0",
8+
"illuminate/database": "^7.0"
89
},
910
"require-dev": {
1011
"phpunit/phpunit": "^9.1@dev"
1112
},
1213
"autoload": {
14+
"files": [
15+
"src/Http/helpers.php"
16+
],
1317
"psr-4" : {
1418
"SoftinkLab\\LaravelKeyvalueStorage\\" : "src/"
1519
}

src/Http/helpers.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
if (!function_exists('kvoption')) {
4+
/**
5+
* Get / Set the specified option.
6+
*
7+
* For set option, array should be passed.
8+
*
9+
* @param array|string $key
10+
* @param mixed $default
11+
* @return mixed|\SoftinkLab\LaravelKeyvalueStorage\KVOption
12+
*/
13+
function kvoption($key = null, $default = null)
14+
{
15+
if (is_null($key)) {
16+
return app('kvoption');
17+
}
18+
19+
if (is_array($key)) {
20+
return app('kvoption')->setArray($key);
21+
}
22+
23+
return app('kvoption')->get($key, $default);
24+
}
25+
}
26+
27+
if (!function_exists('kvoption_exists')) {
28+
/**
29+
* Check the specified option exits by key.
30+
*
31+
* @param string $key
32+
* @return mixed
33+
*/
34+
function kvoption_exists($key)
35+
{
36+
return app('option')->exists($key);
37+
}
38+
}

src/KVOption.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ public function exists($key)
4646
* Get the specified option by key.
4747
*
4848
* @param string $key
49+
* @param mixed $default
4950
* @return mixed
5051
*/
51-
public function get($key)
52+
public function get($key, $default = null)
5253
{
5354
if ($option = $this->where('key', $key)->first()) {
5455
return $option->value;
5556
}
5657

57-
return null;
58+
return $default;
5859
}
5960

6061
/**
@@ -81,20 +82,38 @@ public function set($key, $value, $comment)
8182
*/
8283
public function setArray($array)
8384
{
84-
foreach ($array as $option) {
85+
// Multiple elements are present
86+
if (is_array($array[0])){
87+
foreach ($array as $option) {
88+
// Check if comment is available.
89+
if (count($option) == 2) {
90+
$this->updateOrCreate(
91+
['key' => $option[0]],
92+
['value' => $option[1]]
93+
);
94+
} else {
95+
$this->updateOrCreate(
96+
['key' => $option[0]],
97+
['value' => $option[1], 'comment' => $option[2]]
98+
);
99+
}
100+
}
101+
}else{
102+
$option = $array;
85103
// Check if comment is available.
86-
if (count($option) == 2){
104+
if (count($option) == 2) {
87105
$this->updateOrCreate(
88106
['key' => $option[0]],
89107
['value' => $option[1]]
90108
);
91-
}else{
109+
} else {
92110
$this->updateOrCreate(
93111
['key' => $option[0]],
94112
['value' => $option[1], 'comment' => $option[2]]
95113
);
96114
}
97115
}
116+
98117
}
99118

100119
/**

src/KeyValueStorageServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function boot()
1515
$this->publishes([
1616
__DIR__ . '/../config/kvstorage.php' => config_path('kvstorage.php'),
1717
]);
18+
19+
1820
}
1921

2022
/**
@@ -23,5 +25,17 @@ public function boot()
2325
public function register()
2426
{
2527
$this->app->bind('kvoption', KVOption::class);
28+
29+
$this->registerHelpers();
30+
}
31+
32+
/**
33+
* Register helpers file
34+
*/
35+
public function registerHelpers()
36+
{
37+
if (file_exists($file = __DIR__ . '\Http\helpers.php')) {
38+
require_once $file;
39+
}
2640
}
2741
}

0 commit comments

Comments
 (0)