承接 itarato/var-check 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

itarato/var-check

Composer 安装命令:

composer require itarato/var-check

包简介

Accessing properties, keys and instance methods without checking them all the time.

README 文档

README

Build Status

Scrutinizer Code Quality

Codeship Status for itarato/var-check

Changelog

#Version 2

  • Use of PSR-4 autoading:
require_once __DIR__ . '/vendor/autoload.php';
use itarato\VarCheck\VC;
  • Instance creation:
VC::make($variable);
  • VarCheck methods got underscore (avoiding real object properties, functions or array keys):
VC::make($variable)->_value();
VC::make($variable)->_empty();
  • __call magic method now calls the instance function of the current object:
class User {
  function getParent() {
    return $this->parent;
  }
}
$child = new User();
VC::make($child)->getParent()->_value();

Install

  • Through composer: "require": "itarato/var-check": "2.*@dev"
$ echo '{"require": {"itarato/var-check": "2.*@dev"}}' > composer.json ; composer install

VarCheck is a single class to verify nested complex variable without lots of isset() and exist().

To avoid multiple level of isset/exist/etc this class provides an easy way to verify nested values in a variable. Typical use case when you have a large variable, and you are not sure if it has the right index, and inside there an object, and an attribute ...

The complex variable

$myComplexVar = array(1 => new stdClass());
$myComplexVar[1]->name = 'John Doe';

Problem to solve

// Get the value:
$output = isset($myComplexVar[1]) && isset($myComplexVar[1]->name) ? $myComplexVar[1]->name : $otherwise;

Solution

$output = VC::make($myComplexVar)->_key(1)->_attr('name')->_value($otherwise);
// or even simpler:
$output = VC::make($myComplexVar)->{'1'}->name->_value($otherwise);

Checking if the nested value exist

VC::make($myComplexVar)->_key(1)->_attr('name')->_exist(); // TRUE;
// or:
VC::make($myComplexVar)->{'1'}->name->_exist(); // TRUE;

Get the nested value

VC::make($myComplexVar)->_key(1)->_attr('name')->_value(); // John Doe;

Call a function on the value if exist

// Instead of this:
$value = isset($variable['key']['foo']->element) ? my_function($variable['key']['foo']->element) : NULL;
// Do this:
$value = VC::make($variable)->key->foo->element->my_function();
// Or:
$myClassInstance;
$value = arCheck::make($variable)->key->foo->element->call(array($myClassInstance, 'instanceFunction'));

Failsafe check in case it does not exist

VC::make($myComplexVar)->_key(1)->_attr('job')->_exist(); // FALSE;
VC::make($myComplexVar)->_key(1)->_attr('job')->_attr('title')->_exist(); // FALSE;

Check and value at the same time

if ($value = VC::make($form_status)->_key('values')->_key('#node')->_attr('field_image')->_key(LANGUAGE_NONE)->_key(0)->_key('item')->_key('fid')->_value()) {
  // Use $value;
}
// or:
if ($value = VC::make($form_status)->values->{'#node'}->field_image->{LANGUAGE_NONE}->{'0'}->item->fid->_value()) {
  // Use $value;
}

Custom validation

VC::make($myVar)->_key(3)->_attr('title')->_call(function ($v) {
  return $v > 10;
});

统计信息

  • 总下载量: 2.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固