Exemplo n.º 1
0
 /**
  * Recursively validate data using dot notation
  * @param  IField $field 
  * @param  array  $data 
  * @param  string $path
  * @return array
  */
 protected function validateDeeply(IField $field, $data, $path)
 {
     $errors = array();
     if (Validators::isList($data) && count($data)) {
         foreach ($data as $item) {
             $newErrors = $this->validateDeeply($field, $item, $path);
             $errors = array_merge($errors, $newErrors);
         }
     } else {
         $keys = explode(".", $path);
         $last = count($keys) - 1;
         foreach ($keys as $index => $key) {
             $isLast = $index == $last;
             $value = isset($data[$key]) ? $data[$key] : NULL;
             if (is_array($value)) {
                 $newPath = Strings::replace($path, "~^{$key}\\.~");
                 $newErrors = $this->validateDeeply($field, $value, $newPath);
                 $errors = array_merge($errors, $newErrors);
                 break;
                 // because recursion already handled this path validation
             } else {
                 if ($isLast || $value === NULL) {
                     $newErrors = $field->validate($value);
                     $errors = array_merge($errors, $newErrors);
                     break;
                 }
             }
         }
     }
     return $errors;
 }
Exemplo n.º 2
0
 /**
  * @param array|string $setCookies
  */
 public function __construct($setCookies = NULL)
 {
     if (Nette\Utils\Validators::isList($setCookies) || is_scalar($setCookies)) {
         $this->parse(is_array($setCookies) ? $setCookies : (array) $setCookies);
     } else {
         foreach ((array) $setCookies as $name => $value) {
             $this->{$name} = $value;
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Sets control's value.
  *
  * @param  array|Nette\Http\FileUpload
  * @return Nette\Http\FileUpload  provides a fluent interface
  */
 public function setValue($value)
 {
     if (is_array($value)) {
         if (Nette\Utils\Validators::isList($value)) {
             foreach ($value as $i => $file) {
                 $this->value[$i] = $file instanceof Http\FileUpload ? $file : new Http\FileUpload($file);
             }
         } else {
             $this->value = array(new Http\FileUpload($value));
         }
     } elseif ($value instanceof Http\FileUpload) {
         $this->value = array($value);
     } else {
         $this->value = new Http\FileUpload(NULL);
     }
     return $this;
 }
Exemplo n.º 4
0
 /**
  * Returns the NEON representation of a value.
  * @param  mixed
  * @param  int
  * @return string
  */
 public static function encode($var, $options = NULL)
 {
     if ($var instanceof \DateTime) {
         return $var->format('Y-m-d H:i:s O');
     } elseif ($var instanceof NeonEntity) {
         return self::encode($var->value) . '(' . substr(self::encode($var->attributes), 1, -1) . ')';
     }
     if (is_object($var)) {
         $obj = $var;
         $var = array();
         foreach ($obj as $k => $v) {
             $var[$k] = $v;
         }
     }
     if (is_array($var)) {
         $isList = Validators::isList($var);
         $s = '';
         if ($options & self::BLOCK) {
             if (count($var) === 0) {
                 return "[]";
             }
             foreach ($var as $k => $v) {
                 $v = self::encode($v, self::BLOCK);
                 $s .= ($isList ? '-' : self::encode($k) . ':') . (Strings::contains($v, "\n") ? "\n\t" . str_replace("\n", "\n\t", $v) : ' ' . $v) . "\n";
                 continue;
             }
             return $s;
         } else {
             foreach ($var as $k => $v) {
                 $s .= ($isList ? '' : self::encode($k) . ': ') . self::encode($v) . ', ';
             }
             return ($isList ? '[' : '{') . substr($s, 0, -2) . ($isList ? ']' : '}');
         }
     } elseif (is_string($var) && !is_numeric($var) && !preg_match('~[\\x00-\\x1F]|^\\d{4}|^(true|false|yes|no|on|off|null)$~i', $var) && preg_match('~^' . self::$patterns[4] . '$~', $var)) {
         return $var;
     } elseif (is_float($var)) {
         $var = var_export($var, TRUE);
         return Strings::contains($var, '.') ? $var : $var . '.0';
     } else {
         return json_encode($var);
     }
 }
Exemplo n.º 5
0
 /**
  * Formats PHP code for class instantiating, function calling or property setting in PHP.
  * @return string
  * @internal
  */
 public function formatStatement(Statement $statement, $self = NULL)
 {
     $entity = $this->normalizeEntity($statement->entity);
     $arguments = $statement->arguments;
     if (is_string($entity) && Strings::contains($entity, '?')) {
         // PHP literal
         return $this->formatPhp($entity, $arguments, $self);
     } elseif ($service = $this->getServiceName($entity)) {
         // factory calling or service retrieving
         if ($this->definitions[$service]->shared) {
             if ($arguments) {
                 throw new ServiceCreationException("Unable to call service '{$entity}'.");
             }
             return $this->formatPhp('$this->getService(?)', array($service));
         }
         $params = array();
         foreach ($this->definitions[$service]->parameters as $k => $v) {
             $params[] = preg_replace('#\\w+$#', '\\$$0', is_int($k) ? $v : $k) . (is_int($k) ? '' : ' = ' . PhpHelpers::dump($v));
         }
         $rm = new Nette\Reflection\GlobalFunction(create_function(implode(', ', $params), ''));
         $arguments = Helpers::autowireArguments($rm, $arguments, $this);
         return $this->formatPhp('$this->?(?*)', array(Container::getMethodName($service, FALSE), $arguments), $self);
     } elseif ($entity === 'not') {
         // operator
         return $this->formatPhp('!?', array($arguments[0]));
     } elseif (is_string($entity)) {
         // class name
         if ($constructor = Nette\Reflection\ClassType::from($entity)->getConstructor()) {
             $this->addDependency($constructor->getFileName());
             $arguments = Helpers::autowireArguments($constructor, $arguments, $this);
         } elseif ($arguments) {
             throw new ServiceCreationException("Unable to pass arguments, class {$entity} has no constructor.");
         }
         return $this->formatPhp("new {$entity}" . ($arguments ? '(?*)' : ''), array($arguments), $self);
     } elseif (!Validators::isList($entity) || count($entity) !== 2) {
         throw new Nette\InvalidStateException("Expected class, method or property, " . PhpHelpers::dump($entity) . " given.");
     } elseif ($entity[0] === '') {
         // globalFunc
         return $this->formatPhp("{$entity['1']}(?*)", array($arguments), $self);
     } elseif (Strings::contains($entity[1], '$')) {
         // property setter
         Validators::assert($arguments, 'list:1', "setup arguments for '" . Nette\Callback::create($entity) . "'");
         if ($this->getServiceName($entity[0], $self)) {
             return $this->formatPhp('?->? = ?', array($entity[0], substr($entity[1], 1), $arguments[0]), $self);
         } else {
             return $this->formatPhp($entity[0] . '::$? = ?', array(substr($entity[1], 1), $arguments[0]), $self);
         }
     } elseif ($service = $this->getServiceName($entity[0], $self)) {
         // service method
         if ($this->definitions[$service]->class) {
             $arguments = $this->autowireArguments($this->definitions[$service]->class, $entity[1], $arguments);
         }
         return $this->formatPhp('?->?(?*)', array($entity[0], $entity[1], $arguments), $self);
     } else {
         // static method
         $arguments = $this->autowireArguments($entity[0], $entity[1], $arguments);
         return $this->formatPhp("{$entity['0']}::{$entity['1']}(?*)", array($arguments), $self);
     }
 }
Exemplo n.º 6
0
 public function insert($data)
 {
     if ($data instanceof \Traversable && !$data instanceof Selection) {
         $data = iterator_to_array($data);
     }
     if (Nette\Utils\Validators::isList($data)) {
         foreach (array_keys($data) as $key) {
             $data[$key][$this->column] = $this->active;
         }
     } else {
         $data[$this->column] = $this->active;
     }
     return parent::insert($data);
 }
 /**
  * Creates a list of arguments using autowiring.
  *
  * @return array
  */
 public function autowireArguments($class, $method, array $arguments)
 {
     $rc = Nette\Reflection\ClassType::from($class);
     if (!$rc->hasMethod($method)) {
         if (!Nette\Utils\Validators::isList($arguments)) {
             throw new ServiceCreationException("Unable to pass specified arguments to {$class}::{$method}().");
         }
         return $arguments;
     }
     $rm = $rc->getMethod($method);
     if ($rm->isAbstract() || !$rm->isPublic()) {
         throw new ServiceCreationException("{$rm} is not callable.");
     }
     $this->addDependency($rm->getFileName());
     return Helpers::autowireArguments($rm, $arguments, $this);
 }
Exemplo n.º 8
0
 /**
  * Adds condition for primary key.
  * @param  mixed
  * @return self
  */
 public function wherePrimary($key)
 {
     if (is_array($this->primary) && Nette\Utils\Validators::isList($key)) {
         foreach ($this->primary as $i => $primary) {
             $this->where($primary, $key[$i]);
         }
     } elseif (is_array($key)) {
         // key contains column names
         $this->where($key);
     } else {
         $this->where($this->getPrimary(), $key);
     }
     return $this;
 }
Exemplo n.º 9
0
 /**
  * @param string $query
  * @param array $params
  * @param array $types
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  * @throws \Doctrine\DBAL\DBALException
  * @throws \Nette\Utils\RegexpException
  * @return string
  */
 public static function formatQuery($query, $params, array $types = [], AbstractPlatform $platform = NULL)
 {
     if (!$platform) {
         $platform = new Doctrine\DBAL\Platforms\MySqlPlatform();
     }
     if (!$types) {
         foreach ($params as $key => $param) {
             if (is_array($param)) {
                 $types[$key] = Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
             } else {
                 $types[$key] = 'string';
             }
         }
     }
     try {
         list($query, $params, $types) = \Doctrine\DBAL\SQLParserUtils::expandListParameters($query, $params, $types);
     } catch (Doctrine\DBAL\SQLParserUtilsException $e) {
     }
     $formattedParams = [];
     foreach ($params as $key => $param) {
         if (isset($types[$key])) {
             if (is_scalar($types[$key]) && array_key_exists($types[$key], Type::getTypesMap())) {
                 $types[$key] = Type::getType($types[$key]);
             }
             /** @var Type[] $types */
             if ($types[$key] instanceof Type) {
                 $param = $types[$key]->convertToDatabaseValue($param, $platform);
             }
         }
         $formattedParams[] = SimpleParameterFormatter::format($param);
     }
     $params = $formattedParams;
     if (Nette\Utils\Validators::isList($params)) {
         $parts = explode('?', $query);
         if (count($params) > $parts) {
             throw new Kdyby\Doctrine\InvalidStateException("Too mny parameters passed to query.");
         }
         return implode('', Kdyby\Doctrine\Helpers::zipper($parts, $params));
     }
     return Strings::replace($query, '~(\\:[a-z][a-z0-9]*|\\?[0-9]*)~i', function ($m) use(&$params) {
         if (substr($m[0], 0, 1) === '?') {
             if (strlen($m[0]) > 1) {
                 if (isset($params[$k = substr($m[0], 1)])) {
                     return $params[$k];
                 }
             } else {
                 return array_shift($params);
             }
         } else {
             if (isset($params[$k = substr($m[0], 1)])) {
                 return $params[$k];
             }
         }
         return $m[0];
     });
 }
Exemplo n.º 10
0
 /**
  * Validate assets and build list of collection names.
  *
  * @param array $assets
  * @param NULL|string $parentName
  * @throws \Mike227\NAssetic\Exceptions\ConfigurationException
  */
 private function validateAssetsBuildColList(array $assets, $parentName = NULL)
 {
     // Validate assets
     foreach ($assets as $collectionName => $asset) {
         if (is_string($asset)) {
             // file definition
             return;
         }
         // Build list of collection names.
         // It is used to check if the collection exists (if is found reference to a collection).
         $collectionName = $parentName ? $parentName . '.' . $collectionName : $collectionName;
         $this->assetsNames[] = $collectionName;
         // [asset1, asset2, asset3] is equivalent to ['assets' => [asset1, asset2, asset3]]
         if (Validators::isList($asset)) {
             $asset = array('assets' => $asset);
         }
         Validators::assert($asset, 'array');
         foreach ($asset as $key => $value) {
             if (!in_array($key, $this->allowedKeysAssets)) {
                 throw new ConfigurationException("Key \"{$key}\" is not allowed in 'assets' configuration. Allowed keys are: " . implode(" ", $this->allowedKeysAssets));
             }
             if ($key === 'assets') {
                 Validators::assert($value, 'array');
                 $this->validateAssetsBuildColList($value, $collectionName);
             } else {
                 Validators::assert($key, 'string:1..');
             }
         }
     }
 }
Exemplo n.º 11
0
 /**
  * @param array $arr1
  * @param array $arr2
  * @return array
  */
 protected function getRecursiveDiff($arr1, $arr2)
 {
     $isList = Validators::isList($arr1);
     $arr2IsList = Validators::isList($arr2);
     foreach ($arr1 as $key => $item) {
         if (!is_array($arr1[$key])) {
             // if key is numeric, remove the same value
             if (is_numeric($key) && ($pos = array_search($arr1[$key], $arr2)) !== FALSE) {
                 unset($arr1[$key]);
             } else {
                 if (!$isList && isset($arr2[$key]) || $isList && $arr2IsList && array_search($item, $arr2) !== FALSE) {
                     unset($arr1[$key]);
                 }
             }
             //
         } elseif (isset($arr2[$key])) {
             $arr1[$key] = $item = $this->getRecursiveDiff($arr1[$key], $arr2[$key]);
             if (is_array($item) && count($item) === 0) {
                 unset($arr1[$key]);
             }
         }
     }
     if ($isList) {
         $arr1 = array_merge($arr1);
     }
     return $arr1;
 }
Exemplo n.º 12
0
 function formatStatement(Statement $statement, $self = NULL)
 {
     $arguments = (array) $statement->arguments;
     if ($statement->entity instanceof PhpLiteral) {
         return $this->formatPhp('call_user_func_array(?, ?)', array($statement->entity, $arguments));
     }
     $entity = explode('::', $statement->entity);
     if (strpos($statement->entity, '::') === FALSE && ($service = $this->getServiceName($statement->entity))) {
         if ($this->definitions[$service]->shared) {
             throw new ServiceCreationException("Unable to call service '{$statement->entity}'.");
         }
         $params = array();
         foreach ($this->definitions[$service]->parameters as $k => $v) {
             $params[] = preg_replace('#\\w+$#', '\\$$0', is_int($k) ? $v : $k) . (is_int($k) ? '' : ' = ' . PhpHelpers::dump($v));
         }
         $rm = new \ReflectionFunction(create_function(implode(', ', $params), ''));
         $arguments = Helpers::autowireArguments($rm, $arguments, $this);
         return $this->formatPhp('$this->?(?*)', array('create' . ucfirst($service), $arguments), $self);
     } elseif (strpos($statement->entity, '::') === FALSE) {
         if ($constructor = Nette\Reflection\ClassType::from($statement->entity)->getConstructor()) {
             $this->addDependency($constructor->getFileName());
             $arguments = Helpers::autowireArguments($constructor, $arguments, $this);
         } elseif ($arguments) {
             throw new ServiceCreationException("Unable to pass arguments, class {$statement->entity} has no constructor.");
         }
         return $this->formatPhp("new {$statement->entity}" . ($arguments ? '(?*)' : ''), array($arguments));
     } elseif (!Validators::isList($entity) || count($entity) !== 2) {
         throw new Nette\InvalidStateException("Expected class, method or property, {$statement->entity} given.");
     } elseif ($entity[0] === '') {
         return $this->formatPhp("{$entity['1']}(?*)", array($arguments), $self);
     } elseif (strpos($statement->entity, '$') !== FALSE) {
         if ($this->getServiceName($entity[0], $self)) {
             return $this->formatPhp('?->? = ?', array($entity[0], substr($entity[1], 1), reset($arguments)), $self);
         } else {
             return $this->formatPhp($entity[0] . '::$? = ?', array(substr($entity[1], 1), reset($arguments)), $self);
         }
     } elseif ($service = $this->getServiceName($entity[0], $self)) {
         if ($this->definitions[$service]->class) {
             $arguments = $this->autowireArguments($this->expand($this->definitions[$service]->class), $entity[1], $arguments);
         }
         return $this->formatPhp('?->?(?*)', array($entity[0], $entity[1], $arguments), $self);
     } else {
         $arguments = $this->autowireArguments($entity[0], $entity[1], $arguments);
         return $this->formatPhp("{$entity['0']}::{$entity['1']}(?*)", array($arguments), $self);
     }
 }