/** * 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 NNeonEntity) { 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 = NValidators::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) . ':') . (NStrings::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 NStrings::contains($var, '.') ? $var : $var . '.0'; } else { return json_encode($var); } }
/** * Sets a header. * @param string * @param string|array value or pair email => name * @param bool * @return NMailMimePart provides a fluent interface */ public function setHeader($name, $value, $append = FALSE) { if (!$name || preg_match('#[^a-z0-9-]#i', $name)) { throw new InvalidArgumentException("Header name must be non-empty alphanumeric string, '$name' given."); } if ($value == NULL) { // intentionally == if (!$append) { unset($this->headers[$name]); } } elseif (is_array($value)) { // email $tmp = & $this->headers[$name]; if (!$append || !is_array($tmp)) { $tmp = array(); } foreach ($value as $email => $recipient) { if ($recipient !== NULL && !NStrings::checkEncoding($recipient)) { NValidators::assert($recipient, 'unicode', "header '$name'"); } if (preg_match('#[\r\n]#', $recipient)) { throw new InvalidArgumentException("Name must not contain line separator."); } NValidators::assert($email, 'email', "header '$name'"); $tmp[$email] = $recipient; } } else { $value = (string) $value; if (!NStrings::checkEncoding($value)) { throw new InvalidArgumentException("Header is not valid UTF-8 string."); } $this->headers[$name] = preg_replace('#[\r\n]+#', ' ', $value); } return $this; }
private function getSection(array $data, $key, $file) { NValidators::assertField($data, $key, 'array|null', "section '%' in file '$file'"); $item = $data[$key]; if ($parent = NConfigHelpers::takeParent($item)) { $item = NConfigHelpers::merge($item, $this->getSection($data, $parent, $file)); } return $item; }
/** * Parses single service from configuration file. * @return void */ public static function parseService(NDIServiceDefinition $definition, $config, $shared = TRUE) { if ($config === NULL) { return; } elseif (!is_array($config)) { $config = array('class' => NULL, 'factory' => $config); } $known = $shared ? array('class', 'factory', 'arguments', 'setup', 'autowired', 'run', 'tags') : array('class', 'factory', 'arguments', 'setup', 'autowired', 'tags', 'internal', 'parameters'); if ($error = array_diff(array_keys($config), $known)) { throw new InvalidStateException("Unknown key '" . implode("', '", $error) . "' in definition of service."); } $arguments = array(); if (array_key_exists('arguments', $config)) { NValidators::assertField($config, 'arguments', 'array'); $arguments = self::filterArguments($config['arguments']); $definition->setArguments($arguments); } if (array_key_exists('class', $config) || array_key_exists('factory', $config)) { $definition->class = NULL; $definition->factory = NULL; } if (array_key_exists('class', $config)) { NValidators::assertField($config, 'class', 'string|stdClass|null'); if ($config['class'] instanceof stdClass) { $definition->setClass($config['class']->value, self::filterArguments($config['class']->attributes)); } else { $definition->setClass($config['class'], $arguments); } } if (array_key_exists('factory', $config)) { NValidators::assertField($config, 'factory', 'callable|stdClass|null'); if ($config['factory'] instanceof stdClass) { $definition->setFactory($config['factory']->value, self::filterArguments($config['factory']->attributes)); } else { $definition->setFactory($config['factory'], $arguments); } } if (isset($config['setup'])) { if (NConfigHelpers::takeParent($config['setup'])) { $definition->setup = array(); } NValidators::assertField($config, 'setup', 'list'); foreach ($config['setup'] as $id => $setup) { NValidators::assert($setup, 'callable|stdClass', "setup item #$id"); if ($setup instanceof stdClass) { NValidators::assert($setup->value, 'callable', "setup item #$id"); $definition->addSetup($setup->value, self::filterArguments($setup->attributes)); } else { $definition->addSetup($setup); } } } $definition->setShared($shared); if (isset($config['parameters'])) { NValidators::assertField($config, 'parameters', 'array'); $definition->setParameters($config['parameters']); } if (isset($config['autowired'])) { NValidators::assertField($config, 'autowired', 'bool'); $definition->setAutowired($config['autowired']); } if (isset($config['internal'])) { NValidators::assertField($config, 'internal', 'bool'); $definition->setInternal($config['internal']); } if (isset($config['run'])) { $config['tags']['run'] = (bool) $config['run']; } if (isset($config['tags'])) { NValidators::assertField($config, 'tags', 'array'); if (NConfigHelpers::takeParent($config['tags'])) { $definition->tags = array(); } foreach ($config['tags'] as $tag => $attrs) { if (is_int($tag) && is_string($attrs)) { $definition->addTag($attrs); } else { $definition->addTag($tag, $attrs); } } } }
public function insert($data) { if ($data instanceof Traversable && !$data instanceof NTableSelection) { $data = iterator_to_array($data); } if (NValidators::isList($data)) { foreach (array_keys($data) as $key) { $data[$key][$this->column] = $this->active; } } else { $data[$this->column] = $this->active; } return parent::insert($data); }
public function loadConfiguration() { $container = $this->getContainerBuilder(); $config = $this->getConfig(); // cache $container->addDefinition('cacheJournal') ->setClass('NFileJournal', array('%tempDir%')); $container->addDefinition('cacheStorage') ->setClass('NFileStorage', array('%tempDir%/cache')); $container->addDefinition('templateCacheStorage') ->setClass('NPhpFileStorage', array('%tempDir%/cache')) ->setAutowired(FALSE); // http $container->addDefinition('httpRequestFactory') ->setClass('NHttpRequestFactory') ->addSetup('setEncoding', array('UTF-8')) ->setInternal(TRUE) ->setShared(FALSE); $container->addDefinition('httpRequest') ->setClass('NHttpRequest') ->setFactory('@\NHttpRequestFactory::createHttpRequest'); $container->addDefinition('httpResponse') ->setClass('NHttpResponse'); $container->addDefinition('httpContext') ->setClass('NHttpContext'); $session = $container->addDefinition('session') ->setClass('NSession'); if (isset($config['session']['expiration'])) { $session->addSetup('setExpiration', array($config['session']['expiration'])); unset($config['session']['expiration']); } if (!empty($config['session'])) { NValidators::assertField($config, 'session', 'array'); $session->addSetup('setOptions', array($config['session'])); } $container->addDefinition('user') ->setClass('NUser'); // application $application = $container->addDefinition('application') ->setClass('NApplication') ->addSetup('$catchExceptions', '%productionMode%'); if (empty($config['productionMode'])) { $application->addSetup('NRoutingDebugger::initialize'); // enable routing debugger } $container->addDefinition('router') ->setClass('NRouteList'); $container->addDefinition('presenterFactory') ->setClass('NPresenterFactory', array( isset($container->parameters['appDir']) ? $container->parameters['appDir'] : NULL )); // mailer if (empty($config['mailer']['smtp'])) { $container->addDefinition('mailer') ->setClass('NSendmailMailer'); } else { NValidators::assertField($config, 'mailer', 'array'); $container->addDefinition('mailer') ->setClass('NSmtpMailer', array($config['mailer'])); } }
/** * Rangle validator: is a control's value number in specified range? * @param NTextBase * @param array min and max value pair * @return bool */ public static function validateRange(NTextBase $control, $range) { return NValidators::isInRange($control->getValue(), $range); }
/** * Formats PHP code for class instantiating, function calling or property setting in PHP. * @return string * @internal */ public function formatStatement(NDIStatement $statement, $self = NULL) { $entity = $this->normalizeEntity($statement->entity); $arguments = (array) $statement->arguments; if (is_string($entity) && NStrings::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 NServiceCreationException("Unable to call service '$entity'."); } return $this->formatPhp('$this->?', array($this->sanitizeName($service))); } $params = array(); foreach ($this->definitions[$service]->parameters as $k => $v) { $params[] = preg_replace('#\w+$#', '\$$0', (is_int($k) ? $v : $k)) . (is_int($k) ? '' : ' = ' . NPhpHelpers::dump($v)); } $rm = new ReflectionFunction(create_function(implode(', ', $params), '')); $arguments = NDIHelpers::autowireArguments($rm, $arguments, $this); return $this->formatPhp('$this->?(?*)', array('create' . ucfirst($service), $arguments), $self); } elseif ($entity === 'not') { // operator return $this->formatPhp('!?', array($arguments[0])); } elseif (is_string($entity)) { // class name if ($constructor = NClassReflection::from($entity)->getConstructor()) { $this->addDependency($constructor->getFileName()); $arguments = NDIHelpers::autowireArguments($constructor, $arguments, $this); } elseif ($arguments) { throw new NServiceCreationException("Unable to pass arguments, class $entity has no constructor."); } return $this->formatPhp("new $entity" . ($arguments ? '(?*)' : ''), array($arguments)); } elseif (!NValidators::isList($entity) || count($entity) !== 2) { throw new InvalidStateException("Expected class, method or property, " . NPhpHelpers::dump($entity) . " given."); } elseif ($entity[0] === '') { // globalFunc return $this->formatPhp("$entity[1](?*)", array($arguments), $self); } elseif (NStrings::contains($entity[1], '$')) { // property setter if ($this->getServiceName($entity[0], $self)) { return $this->formatPhp('?->? = ?', array($entity[0], substr($entity[1], 1), $statement->arguments), $self); } else { return $this->formatPhp($entity[0] . '::$? = ?', array(substr($entity[1], 1), $statement->arguments), $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); } }
public function findClanekByUrl($url) { if (NValidators::isNumericInt($url)) { return $this->findClanekById($url); } if (preg_match('~([0-9]*)-(.*)~', htmlspecialchars_decode($url), $tmp)) { return $this->findClanekById($tmp[1]); } return FALSE; }
/** * Adds condition for primary key. * @param mixed * @return self */ public function wherePrimary($key) { if (is_array($this->primary) && NValidators::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; }
static function kontrola_web($web) { return NValidators::isUrl($web); }