dechamp/shorty
最新稳定版本:1.3.8
Composer 安装命令:
composer require dechamp/shorty
包简介
Just a few tools to help avoid annoying things such as long logic where it should be simple.
README 文档
README
The goal of this library is to provide easy helpers to shorten repetitive task that are used day in and day out.
Guidelines
- Do not create something that you can get from another supported library.
- K.I.S.S. (Keep it simple stupid), don't make large helpers.
- Keep them as static methods for simplicity
Helps shorten typical logic that can become excessive, such as getting a value from an deep array or returning null.
Methods
grabOrNull((array|object) $haystack, (string) $needle)
Used when dealing with arrays or object, when getting values. check recursively for key, return value or null if key not found
- $haystack - (array|object) it will do the same, for array or object
- $needle - (string) search recursively by seperating the keys with
/, expa/b/c
example usage
use
Shorty::grabOrNull($deepArray, "a/b/c/d");
instead of...
isset($deepArray['a']['b']['c']['d']) ? $deepArray['a']['b']['c']['d'] : null;
grabOr((array|object) $haystack, (string) $needle, (mixed) $default)
Used when dealing with arrays or object, when getting values. check recursively for key, return value or your default if key not found
- $haystack - (array|object) it will do the same, for array or object
- $needle - (string) search recursively by seperating the keys with
/, expa/b/c - $default - (mixed) anything you wish to return by default
example usage
use
Shorty::grabOr($deepArray, "a/b/c/d", "tacos");
instead of...
isset($deepArray['a']['b']['c']['d']) ? $deepArray['a']['b']['c']['d'] : "tacos";
grabOrThrow((array|object) $haystack, (string) $needle, Exception $exception)
Used when dealing with arrays or object, when getting values. check recursively for key, return value or throw your exception.
- $haystack - (array|object) it will do the same, for array or object
- $needle - (string) search recursively by seperating the keys with
/, expa/b/c - $exception - Exception instance
example usage
use
try {
$val = Shorty::grabOrThrow($deepArray, "a/b/c/d", new Exception('No tacos'));
} ...
instead of...
try {
if (! isset($deepArray['a']['b']['c']['d'])) {
throw new Exception('No Tacos');
}
$val = $deepArray['a']['b']['c']['d'];
} ...
guard() this is now deprecated, use https://packagist.org/packages/webmozart/assert instead
Testing
You can use the included Dockerfile for running phpunit.
Setup
1 docker build -t shorty .
2 docker run -it -v $PWD:/var/www/html shorty /bin/bash
3 phpunit and phpunit --coverage-text
统计信息
- 总下载量: 9.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-18