Nested variable validator tool

Public

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

Get raw version
php
  1. <?php
  2. /**
  3.  * @file
  4.  * VarCheck class file.
  5.  */
  6.  
  7. /**
  8.  * Class VarCheck
  9.  * Nested variable validator.
  10.  *
  11.  * To avoid multiple level of isset/exist/etc this class provides an easy way to verify nested values in a variable.
  12.  * Typical use case when you have a large variable, and you are not sure if it has the right index, and inside
  13.  * there an object, and an attribute ...
  14.  *
  15.  * Usage:
  16.  * $myComplexVar = array(1 => new stdClass());
  17.  * $myComplexVar[1]->name = 'John Doe';
  18.  * VarCheck::instance($myComplexVar)->key(1)->attr('name')->exist(); // TRUE;
  19.  * VarCheck::instance($myComplexVar)->key(1)->attr('name')->value(); // John Doe;
  20.  * VarCheck::instance($myComplexVar)->key(1)->attr('job')->exist(); // FALSE;
  21.  * VarCheck::instance($myComplexVar)->key(1)->attr('job')->attr('title')->exist(); // FALSE;
  22.  *
  23.  * @see https://github.com/itarato/var-check
  24.  */
  25. class VarCheck {
  26.  
  27. /**
  28.   * The internal variable.
  29.   *
  30.   * @var Mixed
  31.   */
  32. private $value;
  33.  
  34. /**
  35.   * Constructor.
  36.   * Can be called directly, or just simply through: VarCheck::instance($value);
  37.   *
  38.   * @param Mixed $value
  39.   * Variable.
  40.   */
  41. public function __construct($value) {
  42. $this->value = $value;
  43. }
  44.  
  45. /**
  46.   * Getting a quick VarCheck instance.
  47.   *
  48.   * @param Mixed $value
  49.   * Variable.
  50.   * @return VarCheck
  51.   * Instance object.
  52.   */
  53. public static function instance($value) {
  54. return new VarCheck($value);
  55. }
  56.  
  57. /**
  58.   * Check if the value exist.
  59.   *
  60.   * @return bool
  61.   * TRUE if the value exist and different from NULL.
  62.   */
  63. public function exist() {
  64. return isset($this->value);
  65. }
  66.  
  67. /**
  68.   * Calls an attribute of the object value.
  69.   *
  70.   * @param String|Integer $attr
  71.   * Attribute string.
  72.   * @return VarCheck $this
  73.   * Instance.
  74.   */
  75. public function attr($attr) {
  76. if (isset($this->value) && is_object($this->value) && isset($this->value->{$attr})) {
  77. $this->value = $this->value->{$attr};
  78. }
  79. else {
  80. unset($this->value);
  81. }
  82. return $this;
  83. }
  84.  
  85. /**
  86.   * Calls a key of the array value.
  87.   *
  88.   * @param String|Integer $key
  89.   * Key string.
  90.   * @return VarCheck $this
  91.   * Instance.
  92.   */
  93. public function key($key) {
  94. if (isset($this->value) && is_array($this->value) && isset($this->value[$key])) {
  95. $this->value = $this->value[$key];
  96. }
  97. else {
  98. unset($this->value);
  99. }
  100. return $this;
  101. }
  102.  
  103. /**
  104.   * Returns the value.
  105.   *
  106.   * @return Mixed
  107.   * Value.
  108.   */
  109. public function value() {
  110. return $this->value;
  111. }
  112.  
  113. /**
  114.   * Universal validator.
  115.   *
  116.   * @param callable $callback
  117.   * Custom function to for value validation. Has to return a BOOL value.
  118.   * @return Bool
  119.   * Validation success.
  120.   */
  121. public function validateWith(callable $callback) {
  122. return $callback($this->value);
  123. }
  124.  
  125. }