protected function _find($path, $object = null) { $obj = $object === null ? $this->values : $object; return Pie::find($path, $obj); }
public static function findRecursive($find, $scope) { $value = Pie::find($find, $scope); if ($value === '') { $cscope = $scope->getParentScope(); do { if ($cscope === null) { break; } $value = Pie::find($find, $cscope); if ($value !== null) { break; } $cscope = $cscope->getParentScope(); } while (true); } return $value; }
protected function _test(&$errors) { foreach ($this->validations as $key => $val) { $map = array_map('trim', explode('|', $val)); $search = Pie::find($key); foreach ($map as $req) { $req = strtolower($req); // Value is required if ($req == 'required' && $search == '') { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is missing']; } // Value min and max and len if (preg_match('/^(min|max|len):\\d+/i', $req, $matches)) { $length = (int) explode(':', $req)[1]; $action = strtolower($matches[1]); if ($action == 'min' && strlen($search) < $length) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item length is too short']; } elseif ($action == 'max' && strlen($search) > $length) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item length is too long']; } elseif ($action == 'len' && strlen($search) != $length) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item does not equal the length']; } } if (preg_match('/^between:(\\d+),(\\d+)/i', $req, $matches)) { $min = $matches[1]; $max = $matches[2]; $this->minMax($req, $key, $search, $min, $max, $errors); } // Search within a list of items if (preg_match('/^(notin|in):(.+)/i', $req, $matches)) { $action = strtolower($matches[1]); $items = array_map('trim', explode(',', $matches[2])); if ($action == 'in' && !in_array($search, $items)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is not in the list']; } elseif ($action == 'notin' && in_array($search, $items)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is in the list']; } } // Match an item against a regular expression if (preg_match('/match:(.+)/i', $req, $matches)) { if (!preg_match($matches[1], $search)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item does not match']; } } // Validate an ip address if ($req == 'ip' && !filter_var($search, FILTER_VALIDATE_IP)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is not an ip address']; } // Validate an email address if ($req == 'email' && !filter_var($search, FILTER_VALIDATE_EMAIL)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is not an email address']; } // Validate a url if ($req == 'url' && !filter_var($search, FILTER_VALIDATE_URL)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is not a url']; } // Validate a type of data if (preg_match('/typeof:(.+)/i', $req, $matches)) { $types = array_map('trim', explode(',', $matches[1])); if (!in_array(gettype($search), $types)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is not of type']; } } if ($req == 'json') { json_decode($search); if (json_last_error() != JSON_ERROR_NONE) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item is not valid json']; } } // Request key and value must be different if ($req == 'different' && preg_replace('/^\\$.+?\\./', '', $key) == $search) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item key must be different from its value']; } // Request key and value must be the same if ($req == 'same' && preg_replace('/^\\$.+?\\./', '', $key) != $search) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item key must be different from its value']; } // Require a numeric value if ($req == 'numeric' && !is_numeric($search)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item must be numeric']; } // Require a value of numbers only 0 or larger if ($req == 'integer' && !ctype_digit($search)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item must be an integer']; } // Require an alpha value if ($req == 'alpha' && !ctype_alpha($search)) { $errors[] = ['key' => $req, 'value' => $search, 'item' => $key, 'msg' => 'Item must be alpha']; } } } if (count($errors) > 0) { return false; } return true; }
public function functions($value, $operations, Scope $scope) { $operations = array_map('trim', $operations); foreach ($operations as $index => $op) { $items = explode(":", $op); $func = array_shift($items); array_unshift($items, $value); $filter = $this->findFilter($func); if ($filter) { $func = $filter; } else { if (!is_callable($func)) { $call = Pie::find($func, $scope); if (!$call) { $cscope = $scope->getParentScope(); do { if ($cscope === null) { break; } $func = Pie::find($op, $cscope); if ($func !== null) { break; } $cscope = $cscope->getParentScope(); } while (true); } else { $func = $call; } } if (!$call) { $func = Pie::find($func, Pie::$rootScope); } } if ($func instanceof Closure) { $value = call_user_func_array($func, $items); } } return $value; }