spatie/laravel-valuestore

轻松存储一些值

1.3.2 2022-12-13 09:04 UTC

This package is auto-updated.

Last update: 2024-09-13 12:47:01 UTC


README

Latest Version on Packagist Software License Tests Total Downloads

此包使存储和检索一些散值变得容易。存储的值以json文件的形式保存。

可以这样使用

use Spatie\Valuestore\Valuestore;

$valuestore = Valuestore::make($pathToFile);

$valuestore->put('key', 'value');

$valuestore->get('key'); // Returns 'value'

$valuestore->has('key'); // Returns true

// Specify a default value for when the specified key does not exist
$valuestore->get('non existing key', 'default') // Returns 'default'

$valuestore->put('anotherKey', 'anotherValue');

// Put multiple items in one go
$valuestore->put(['ringo' => 'drums', 'paul' => 'bass']);

$valuestore->all(); // Returns an array with all items

$valuestore->forget('key'); // Removes the item

$valuestore->flush(); // Empty the entire valuestore

$valuestore->flushStartingWith('somekey'); // remove all items whose keys start with "somekey"

$valuestore->increment('number'); // $valuestore->get('number') will return 1 
$valuestore->increment('number'); // $valuestore->get('number') will return 2
$valuestore->increment('number', 3); // $valuestore->get('number') will return 5

// Valuestore implements ArrayAccess
$valuestore['key'] = 'value';
$valuestore['key']; // Returns 'value'
isset($valuestore['key']); // Return true
unset($valuestore['key']); // Equivalent to removing the value

// Valuestore implements Countable
count($valuestore); // Returns 0
$valuestore->put('key', 'value');
count($valuestore); // Returns 1

阅读此readme中的使用部分,了解其他方法。

Laravel News上的这篇帖子中,Tim MacDonald分享了如何使用此包来实现settings功能。

Spatie是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述这里

支持我们

我们投入了大量资源来创建最佳的开源软件包。您可以通过购买我们的付费产品之一来支持我们。

我们非常感谢您从您家乡寄给我们一张明信片,注明您正在使用我们的哪个包。您可以在我们的联系页面上找到我们的地址。我们将发布收到的所有明信片在我们的虚拟明信片墙上

安装

您可以通过composer安装此包

composer require spatie/valuestore

使用方法

要创建Valuestore,请使用make方法。

$valuestore = Valuestore::make($pathToFile);

您还可以将一些值作为第二个参数传递。这些值将使用put方法添加到valuestore中。

$valuestore = Valuestore::make($pathToFile, ['key' => 'value']);

所有值都将保存为json文件。

如果没有存储值,则将删除文件。

您可以在Valuestore上调用以下方法

put

/**
 * Put a value in the store.
 *
 * @param string|array $name
 * @param string|int|null $value
 * 
 * @return $this
 */
public function put($name, $value = null)

get

/**
 * Get a value from the store.
 *
 * @param string $name
 *
 * @return null|string
 */
public function get(string $name)

has

/*
 * Determine if the store has a value for the given name.
 */
public function has(string $name) : bool

all

/**
 * Get all values from the store.
 *
 * @return array
 */
public function all() : array

allStartingWith

/**
 * Get all values from the store which keys start with the given string.
 *
 * @param string $startingWith
 *
 * @return array
*/
public function allStartingWith(string $startingWith = '') : array

forget

/**
 * Forget a value from the store.
 *
 * @param string $key
 *
 * @return $this
 */
public function forget(string $key)

flush

/**
 * Flush all values from the store.
 *
 * @return $this
 */
 public function flush()

flushStartingWith

/**
 * Flush all values from the store which keys start with the specified value.
 *
 * @param string $startingWith
 *
 * @return $this
 */
 public function flushStartingWith(string $startingWith)

pull

/**
 * Get and forget a value from the store.
 *
 * @param string $name 
 *
 * @return null|string
 */
public function pull(string $name)

increment

/**
 * Increment a value from the store.
 *
 * @param string $name
 * @param int $by
 *
 * @return int|null|string
 */
 public function increment(string $name, int $by = 1)

decrement

/**
 * Decrement a value from the store.
 *
 * @param string $name
 * @param int $by
 *
 * @return int|null|string
 */
 public function decrement(string $name, int $by = 1)

push

/**
 * Push a new value into an array.
 *
 * @param string $name
 * @param $pushValue
 *
 * @return $this
 */
public function push(string $name, $pushValue)

prepend

/**
 * Prepend a new value into an array.
 *
 * @param string $name
 * @param $prependValue
 *
 * @return $this
 */
public function prepend(string $name, $prependValue)

count

/**
 * Count elements.
 *
 * @return int
 */
public function count()

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现了有关安全性的错误,请通过security@spatie.be发送邮件,而不是使用问题跟踪器。

明信片软件

您可以自由使用此软件包,但如果它进入您的生产环境,我们非常感谢您从您家乡寄给我们一张明信片,注明您正在使用我们的哪个包。

我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。

我们将发布收到的所有明信片在我们的公司网站上

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。