Пример #1
0
 protected function remainder(Validate $validate, $input)
 {
     $input = ArrayHelper::diffByKeys($input, array_keys($this->attributes));
     $config = ['remainder' => $this->remainder, 'attributes' => $validate];
     Instance::configure($this, $config);
     $this->validate($input);
 }
Пример #2
0
 protected function getRequest($url, array $params)
 {
     $config = array_merge(parse_url($url), $params);
     $config['method'] = isset($config['method']) ? $config['method'] : Route::GET;
     $config['url'] = isset($config['path']) ? $config['path'] : null;
     unset($config['path']);
     if ($config['method'] !== Route::GET) {
         if (isset($config['query']) && is_array($config['query'])) {
             $config['bodyParams'] = $config['query'];
         }
     } elseif (isset($config['query'])) {
         if (is_array($config['query'])) {
             $config['query'] = http_build_query($config['query']);
         }
         $config['queryString'] = $config['query'];
         if (isset($config['url'])) {
             $config['url'] .= "?{$config['query']}";
         }
     }
     unset($config['query']);
     Instance::configure($this->request, $config);
     return $this->request;
 }
Пример #3
0
 /**
  * @param $snippet
  * @param array $params
  * @return Snippet
  * @throws TemplateException
  * @throws \rock\helpers\InstanceException
  */
 protected function getInstanceSnippet($snippet, array $params = [])
 {
     if ($snippet instanceof Snippet) {
         if (!empty($params)) {
             Instance::configure($snippet, $params);
         }
         return $snippet;
     }
     if (!isset($this->snippets[$snippet])) {
         throw new TemplateException(TemplateException::UNKNOWN_SNIPPET, ['name' => $snippet]);
     }
     $config = $this->snippets[$snippet];
     if (is_callable($config)) {
         if (($config = call_user_func($this->snippets[$snippet], $params)) instanceof Snippet) {
             return $config;
         }
     }
     if (!isset($config['class'])) {
         throw new TemplateException(TemplateException::UNKNOWN_SNIPPET, ['name' => $snippet]);
     }
     $config = array_merge($config, $params);
     /** @var \rock\snippets\Snippet $snippet */
     $snippet = Instance::ensure($config);
     if (!$snippet instanceof Snippet) {
         throw new TemplateException(TemplateException::UNKNOWN_SNIPPET, ['name' => $snippet::className()]);
     }
     return $snippet;
 }
Пример #4
0
 /**
  * Constructor.
  * The default implementation does two things:
  *
  * - Initializes the object with the given configuration `$config`.
  * - Call {@see \rock\base\ObjectInterface::init()}.
  *
  * If this method is overridden in a child class, it is recommended that
  *
  * - the last parameter of the constructor is a configuration array, like `$config` here.
  * - call the parent implementation at the end of the constructor.
  *
  * @param array $config
  */
 public function __construct($config = [])
 {
     Instance::configure($this, $config);
     $this->init();
 }
Пример #5
0
 /**
  * Sanitize value.
  *
  * @param mixed $input
  * @return mixed
  * @throws SanitizeException
  */
 public function sanitize($input)
 {
     foreach ($this->rawRules as $rule) {
         if ($rule instanceof Attributes) {
             $config = ['remainder' => $this->remainder, 'recursive' => $this->recursive];
             Instance::configure($rule, $config);
             return $rule->sanitize($input);
         }
         $input = $rule->sanitize($input);
         if ($rule->recursive && (is_array($input) || is_object($input))) {
             $config['attributes'] = $this;
             return (new Attributes($config))->sanitize($input);
         }
     }
     return $input;
 }
Пример #6
0
 /**
  * Checks a value.
  * @param mixed $input
  * @return bool
  * @throws ValidateException
  */
 public function validate($input)
 {
     $this->errors = [];
     foreach ($this->rawRules as $rules) {
         list($ruleName, $rule) = $rules;
         // notOf or oneOf
         if ($rule instanceof Validate) {
             $rule->validate($input);
             $this->errors = array_merge($this->errors, $rule->getErrors());
             continue;
         }
         if ($rule instanceof When) {
             $rule->invert = $this->invert;
             $rule->validate($input);
             $this->errors = array_merge($this->errors, $rule->getErrors());
             continue;
         }
         if ($rule instanceof Attributes) {
             $config = ['one' => $this->one, 'invert' => $this->invert, 'remainder' => $this->remainder];
             Instance::configure($rule, $config);
             $rule->validate($input);
             $this->errors = $rule->getErrors();
             break;
         }
         if ($this->skipEmpty && $rule->skipEmpty && $this->isEmpty($input, $rule)) {
             continue;
         }
         if ($rule->validate($input) === !$this->invert) {
             continue;
         }
         $this->errors[$ruleName] = $this->error($ruleName, $rule);
         if ($this->one === true) {
             break;
         }
     }
     return empty($this->errors);
 }
Пример #7
0
 protected static function setPropertiesInternal($object, array $data, array $config = [])
 {
     if ($object instanceof ObjectInterface) {
         $config = !empty($config) ? $config : $data['properties'];
         Instance::configure($object, $config);
         $object->init();
     }
 }