sevens/vars

用于编码和操作字符串和数组的简单变量库包。

v1.1.0 2021-09-19 18:36 UTC

This package is auto-updated.

Last update: 2024-09-20 01:01:30 UTC


README

=> Simple Variable library Package For Validating, Encoding and Manipulating Strings and Arrays.

=> Seven Vars is developed by Elisha Temiloluwa a.k.a TemmyScope	

=> Seven Vars is a library that comes with a Validation Class, 
Arrays Manipulation Class and Strings Generation, Encoding, Sanitization & Manipulation Class

=> The Arrays library of the SevenVars Package implements the Countable, 
ArrayAccess & Serializable interface(s) of the Standard PHP Library and provides 
you a plethora of methods to manipulate an array of arrays i.e. a level II deep array.

=> The Strings Class allows developers encode, manipulate, sanitize and generate truly random strings.

=> The Validation Class takes in an array and rules for validating whether the value assigned to each key
in the array is a valid entry. 

安装

composer require sevens/vars

用法:Seven\Vars\Arrays HOW-TO

为了使库生成预期的输出,请使用有效的数组数组进行初始化。 已完全单元测试。

- Valid Input Sample
$data = [
  [
   'name' => 'Random 1',
   'age' => 24, 'password' => 'gHAST_V43SS',
   'nickname' => 'dick & harry'
  ],
  [
   'name' => 'Random 2',
   'age' => 27, 'password' => 'gHAST0SVSS',
   'nickname' => 'harry'
  ],
  [
   'name' => 'Random 3',
   'age' => 24, 'password' => 'gHASTS*VSS',
   'nickname' => 'dick'
  ],
  [
   'name' => 'Random 4',
   'age' => 21, 'password' => 'gHASTSV#SS',
   'nickname' => 'choudharry'
  ]
];

注意:所有Countable、ArrayAccess和Serializable接口的对象可用的方法和操作都按定义实现/声明。

- Initialization
use Seven\Vars\Arrays;

$arrays = new Arrays($data) || Arrays::init($data): Arrays;
- Safe Initialization: From an array of objects E.g. Laravel's Collection

当您不确定数组中包含什么类型的数据时,例如数组数组或对象数组,您始终可以使用安全的初始化。

use Seven\Vars\Arrays;

$arrays = Arrays::safeInit($data): Arrays;
- Adding an array to the existing arrays
$arrays->add([
 'name' => 'Random 5',
 'age' => 23,
 'nickname' => 'newbie'
]): Arrays;
- Merging array of arrays to the existing one
$arrays->addEach([
 [ 'name' => 'Random 6', 'age' => 22, 'nickname' => 'newb1e'],
 [ 'name' => 'Random 7', 'age' => 28, 'nickname' => 'newb6e']
]): Arrays;
- Adding an array to each array in the existing array of arrays
$arrays->addToEach([
 [ 'name' => 'Random 6', 'age' => 22, 'nickname' => 'newb1e'],
 [ 'name' => 'Random 7', 'age' => 28, 'nickname' => 'newb6e']
]): Arrays;
- Pop off and return the last array
$arrays->pop(): array;
- Pop off and return an array containing the last key => value in each array
$arrays->popEach(): array;
- Shift off and return the first array
$arrays->shift(): array;
- Shift off and return an array containing the first key => value in each array
$arrays->shiftEach(): array;
- Sort based on key: sorts the arrays based on the value attached to the passed key
 in ascending order: from smallest to highest
$arrays->sort('name'): Arrays;
- Sort based on key: sorts the arrays based on the value attached to the passed key
 in ascending order: from highest to smallest.
$arrays->sort('name'): Arrays;
- Sort based on key: sorts the arrays based on the value attached to the passed key
 in ascending order: from highest to smallest.
$arrays->downSort('name'): Arrays;
- Last: get the last array from the arrays
$arrays->last(): array;
- First: get the first array from the arrays
$arrays->first(): array;
- apply a callable on all the arrays using their keys: alias => map()

将数组引用传递到您的可调用函数,您的可调用函数必须返回数组,否则所有值都将为NULL。

$arrays->apply(function($array): array{
 $array['age'] = $array['age'] * 2;
 return $array;
}): Arrays;
- sets a new value for each of the arrays using each of the key -> value pair passed;
if an index is passed, only the key at that array index will be changed
$arrays->set(array $param, ?int $index = null): Arrays;
- renames a key in each of the arrays using each of the key -> value pair passed;
* if an index is passed, only the key at that array index will be changed
$arrays->rename(array $k_v, ?int $index = null): Arrays;
- delete an array from a collection if it violates a validation rule
$arrays->validateOrDelete([
 'name'=>[ 'required' => true, 'string' => true], 
 'age' => [ 'required' => true, 'numeric' => true]
]);

$arrays->get();
- concatenates values of a particular key of multiple arrays using the passed 
separator and saving it on the new name
$arrays->concat(array $keys, string $new_name, string $separator = "_"): Arrays;
- Extract key from the initial array using the key, value pair in the passed argument
$arrays->extractBy(array $k_v): Arrays;
- Extracts all the arrays containing the passed key
$arrays->extractByKey($key): Arrays;
- Extracts from all the arrays, if it contains the passed key
$arrays->extractKey($key): array;
- Excludes all array containing the passed key
$arrays->excludeByKey(string $key): Arrays;
- Exclude keys from the initial array using the key, value pair in the passed argument
$arrays->excludeBy(array $k_v): Arrays;
- Exclude keys from the initial array
$arrays->excludeKey(...$keys): Arrays;
- Returns an array of arrays from the initial array containing only the keys specified in the argument
$arrays->whiteList(array $whitelist): array;
- picks out random arrays
* the set size has to be smaller than the total size of the array being processed
$arrays->random(int $size): array;
- search the value in a key if it contains a certain string
$arrays->searchFor(mixed $search, string $key): array;
- Returns an array of arrays from the initial array containing only arrays that
values matching those of the keys specified in the argument;
$arrays->search(array $k_v): array;
- Returns an array of the size and start position specified
$arrays->trim(int $count, int $start = 0): array;
- checks if the key exists in at least one of the arrays
$arrays->exists(string $k): bool;
- yields a list-like array containing [index, array] like python enumerate method
$arrays->enumerate(): Generator;
- Returns the count of arrays
$arrays->count(): int;
- checks if the array is empty
$arrays->isEmpty(): bool;
- For returning the array at its current state
$arrays->get();
- For serializing and unserializing
$arrays->serialize();

$arrays->unserialize( string $serializedData );
- Return the arrays as JSON ecoded data
$arrays->getJson();
- Return the arrays as an iterable array of objects => equivalent to laravel's collection
$arrays->getObjects();

用法:Seven\Vars\Strings HOW-TO

此类的大多数方法都是静态的,因此不需要初始化。 此类中的非静态方法用于编码、加密和解密。

- Initialization & Usage: If you'd be using the encryption & decryption feature, It's essential
you construct the object passing necessary variables
use Seven\Vars\Strings;

$string = new Strings($alg, $salt, $iv);

$string->encrypt($data);

$string->decrypt($encryptedData);
- Check for the position of a string in another string
/**
 * Checks if a string ends with a specific string
 *
 * @param string $value
 * @param string|string[] $end
 * @param bool $ignoreCase
 * @return bool
*/
public static function endsWith(string $value, $end, bool $ignoreCase = false): bool

/**
* Checks if a string starts with a specific string
*
* @param string $value
* @param string|string[] $start
* @param bool $ignoreCase
* @return bool
*/
public static function startsWith(string $value, $start, bool $ignoreCase = false): bool


/**
* Extracts a string from between two strings, the start string and the stop string
* @param string $full
* @param string $start
* @param string $stop
* @param bool $ignoreCase
* @return string
*/
public function between($full, $start, $stop, bool $ignoreCase = true): string


/**
* Checks if a string contains a specific string
*
* @param string $value
* @param string|array $contain
* @param bool $ignoreCase
* @return bool
*/
public static function contains(string $str, $contain, bool $ignoreCase = false): bool


/**
* Checks if a string matches a pattern
*
* @param string $str
* @param string $pattern => regular expression pattern
* @return bool
*/
public static function matchesPattern(string $str, string $pattern): bool
- For generating truly random strings
/**
* Returns a very random string over 32 characters in length
* @return string
*/
public static function randToken(): string

/**
* Returns a very random string of fixed length
* @param len
* @return string
*/
public static function fixedLengthToken(int $len): string

/**
* trims string to specified length starting from the specified offset
* It is multibyte
* @param string var
* @param int count
* @return reduced string to the specified length
*/
final public static function limit($var, $count = 2225, $offset = 0)

/**
* Returns a random string with very high entropy due to the specified string;
* It will never return two equal strings 
* @param string str
* @return string unique_name
*/
final public static function uniqueId(string $str): string
- Several other methods to format, remove and fix HTML tags; generate time From string etc.

用法:Seven\Vars\Validation HOW-TO

为了使库生成预期的输出,请始终使用有效的数组进行初始化。 当处理数组如$_POST、$_GET等时,此库非常有用。

- Initialization
use Seven\Vars\Validation;

$validation = Validation::init($_POST);

####### OR ########

$data = [
 'name' => 'Random 1',
 'email' => 'random1@mail.com',
 'password' => 'pa33w0rd',
 'site' => 'hybeexchange.com',
 'age' => 24,
 'nickname' => 'dick & harry'
];

$validation = new Validation($data);
- Validation Rules
$validation->rules([

 #checks if a value was provided or not
 'name' => [ 'required' => true ],

 #checks if value is a valid email address
 'email' => [ 'email' => true ],

 #checks for minimum and maximum character length
 'password' => [ 'required' => true, 'min' => 8, 'max' => 32 ],

 #checks if value is a valid website address
 'site' => [ 'url' => true ],

 #check if value is a number, then check if it is greater than 17 and less than 60
 'age' => [ 'numeric' => true, 'gt' => 17, 'lt' => '60'],

]);
- Validation Passed or Failed
$validation->passed(): bool;
- Validation Errors

返回包含首次遇到错误的数组。 如果所有验证规则都成功通过,则返回空数组。

$validation->errors(): array;
- Then - Catch Syntax from JavaScript ES6

如果验证通过,则调用传递到“Then”方法的可调用函数。 如果验证失败,则调用catch方法。

Then和Catch都接受可调用函数 => 闭包、数组可调用、函数

$validation->rules([

])->then(function(){
#This callable is called only when valiidations pass
})->catch(function($errors){
#This callable is called only the validation fails 
});