getSuggestion() public static method

Finds the best suggestion (for 8-bit encoding).
public static getSuggestion ( array $possibilities, $value ) : string | null
$possibilities array
return string | null
Example #1
0
 /**
  * Checks whether $config contains only $expected items and returns combined array.
  * @return array
  * @throws Nette\InvalidStateException
  */
 public function validateConfig(array $expected, array $config = NULL, $name = NULL)
 {
     if (func_num_args() === 1) {
         return $this->config = $this->validateConfig($expected, $this->config);
     }
     if ($extra = array_diff_key((array) $config, $expected)) {
         $name = $name ?: $this->name;
         $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($expected), key($extra));
         $extra = $hint ? key($extra) : implode(", {$name}.", array_keys($extra));
         throw new Nette\InvalidStateException("Unknown configuration option {$name}.{$extra}" . ($hint ? ", did you mean {$name}.{$hint}?" : '.'));
     }
     return Config\Helpers::merge($config, $expected);
 }
Example #2
0
 /**
  * @param  string
  * @return ActiveRow|mixed
  * @throws Nette\MemberAccessException
  */
 public function &__get($key)
 {
     $this->accessColumn($key);
     if (array_key_exists($key, $this->data)) {
         return $this->data[$key];
     }
     $referenced = $this->table->getReferencedTable($this, $key);
     if ($referenced !== FALSE) {
         $this->accessColumn($key, FALSE);
         return $referenced;
     }
     $this->removeAccessColumn($key);
     $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($this->data), $key);
     throw new Nette\MemberAccessException("Cannot read an undeclared column '{$key}'" . ($hint ? ", did you mean '{$hint}'?" : '.'));
 }
Example #3
0
 private function resolveImplement(ServiceDefinition $def, $name)
 {
     $interface = $def->getImplement();
     if (!interface_exists($interface)) {
         throw new ServiceCreationException("Interface {$interface} used in service '{$name}' not found.");
     }
     self::checkCase($interface);
     $rc = new ReflectionClass($interface);
     $method = $rc->hasMethod('create') ? $rc->getMethod('create') : ($rc->hasMethod('get') ? $rc->getMethod('get') : NULL);
     if (count($rc->getMethods()) !== 1 || !$method || $method->isStatic()) {
         throw new ServiceCreationException("Interface {$interface} used in service '{$name}' must have just one non-static method create() or get().");
     }
     $def->setImplementType($methodName = $rc->hasMethod('create') ? 'create' : 'get');
     if (!$def->getClass() && !$def->getEntity()) {
         $returnType = PhpReflection::getReturnType($method);
         if (!$returnType) {
             throw new ServiceCreationException("Method {$interface}::{$methodName}() used in service '{$name}' has no @return annotation.");
         } elseif (!class_exists($returnType)) {
             throw new ServiceCreationException("Check a @return annotation of the {$interface}::{$methodName}() method used in service '{$name}', class '{$returnType}' cannot be found.");
         }
         $def->setClass($returnType);
     }
     if ($methodName === 'get') {
         if ($method->getParameters()) {
             throw new ServiceCreationException("Method {$interface}::get() used in service '{$name}' must have no arguments.");
         }
         if (!$def->getEntity()) {
             $def->setFactory('@\\' . ltrim($def->getClass(), '\\'));
         } elseif (!$this->getServiceName($def->getFactory()->getEntity())) {
             throw new ServiceCreationException("Invalid factory in service '{$name}' definition.");
         }
     }
     if (!$def->parameters) {
         $ctorParams = array();
         if (!$def->getEntity()) {
             $def->setFactory($def->getClass(), $def->getFactory() ? $def->getFactory()->arguments : array());
         }
         if (($class = $this->resolveEntityClass($def->getFactory(), array($name => 1))) && ($rc = new ReflectionClass($class)) && ($ctor = $rc->getConstructor())) {
             foreach ($ctor->getParameters() as $param) {
                 $ctorParams[$param->getName()] = $param;
             }
         }
         foreach ($method->getParameters() as $param) {
             $hint = PhpReflection::getParameterType($param);
             if (isset($ctorParams[$param->getName()])) {
                 $arg = $ctorParams[$param->getName()];
                 if ($hint !== PhpReflection::getParameterType($arg)) {
                     throw new ServiceCreationException("Type hint for \${$param->getName()} in {$interface}::{$methodName}() doesn't match type hint in {$class} constructor.");
                 }
                 $def->getFactory()->arguments[$arg->getPosition()] = self::literal('$' . $arg->getName());
             } elseif (!$def->getSetup()) {
                 $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($ctorParams), $param->getName());
                 throw new ServiceCreationException("Unused parameter \${$param->getName()} when implementing method {$interface}::{$methodName}()" . ($hint ? ", did you mean \${$hint}?" : '.'));
             }
             $paramDef = $hint . ' ' . $param->getName();
             if ($param->isOptional()) {
                 $def->parameters[$paramDef] = $param->getDefaultValue();
             } else {
                 $def->parameters[] = $paramDef;
             }
         }
     }
 }
Example #4
0
 /** @internal */
 public function processExtensions()
 {
     $this->config = Helpers::expand(array_diff_key($this->config, self::$reserved), $this->builder->parameters) + array_intersect_key($this->config, self::$reserved);
     foreach ($first = $this->getExtensions('Nette\\DI\\Extensions\\ExtensionsExtension') as $name => $extension) {
         $extension->setConfig(isset($this->config[$name]) ? $this->config[$name] : array());
         $extension->loadConfiguration();
     }
     $last = $this->getExtensions('Nette\\DI\\Extensions\\InjectExtension');
     $this->extensions = array_merge(array_diff_key($this->extensions, $last), $last);
     $extensions = array_diff_key($this->extensions, $first);
     foreach (array_intersect_key($extensions, $this->config) as $name => $extension) {
         if (isset($this->config[$name]['services'])) {
             trigger_error("Support for inner section 'services' inside extension was removed (used in '{$name}').", E_USER_DEPRECATED);
         }
         $extension->setConfig($this->config[$name] ?: array());
     }
     foreach ($extensions as $extension) {
         $extension->loadConfiguration();
     }
     if ($extra = array_diff_key($this->extensions, $extensions, $first)) {
         $extra = implode("', '", array_keys($extra));
         throw new Nette\DeprecatedException("Extensions '{$extra}' were added while container was being compiled.");
     } elseif ($extra = key(array_diff_key($this->config, self::$reserved, $this->extensions))) {
         $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys(self::$reserved + $this->extensions), $extra);
         throw new Nette\InvalidStateException("Found section '{$extra}' in configuration, but corresponding extension is missing" . ($hint ? ", did you mean '{$hint}'?" : '.'));
     }
 }
Example #5
0
 public function __get($key)
 {
     $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys((array) $this), $key);
     throw new Nette\MemberAccessException("Cannot read an undeclared column '{$key}'" . ($hint ? ", did you mean '{$hint}'?" : '.'));
 }
Example #6
0
 /**
  * Returns component specified by name or path.
  * @param  string
  * @param  bool   throw exception if component doesn't exist?
  * @return IComponent|NULL
  */
 public function getComponent($name, $need = TRUE)
 {
     if (isset($this->components[$name])) {
         return $this->components[$name];
     }
     if (is_int($name)) {
         $name = (string) $name;
     } elseif (!is_string($name)) {
         throw new Nette\InvalidArgumentException(sprintf('Component name must be integer or string, %s given.', gettype($name)));
     } else {
         $a = strpos($name, self::NAME_SEPARATOR);
         if ($a !== FALSE) {
             $ext = (string) substr($name, $a + 1);
             $name = substr($name, 0, $a);
         }
         if ($name === '') {
             if ($need) {
                 throw new Nette\InvalidArgumentException('Component or subcomponent name must not be empty string.');
             }
             return;
         }
     }
     if (!isset($this->components[$name])) {
         $component = $this->createComponent($name);
         if ($component) {
             if (!$component instanceof IComponent) {
                 throw new Nette\UnexpectedValueException('Method createComponent() did not return Nette\\ComponentModel\\IComponent.');
             } elseif (!isset($this->components[$name])) {
                 $this->addComponent($component, $name);
             }
         }
     }
     if (isset($this->components[$name])) {
         if (!isset($ext)) {
             return $this->components[$name];
         } elseif ($this->components[$name] instanceof IContainer) {
             return $this->components[$name]->getComponent($ext, $need);
         } elseif ($need) {
             throw new Nette\InvalidArgumentException("Component with name '{$name}' is not container and cannot have '{$ext}' component.");
         }
     } elseif ($need) {
         $hint = Nette\Utils\ObjectMixin::getSuggestion(array_merge(array_keys($this->components), array_map('lcfirst', preg_filter('#^createComponent([A-Z0-9].*)#', '$1', get_class_methods($this)))), $name);
         throw new Nette\InvalidArgumentException("Component with name '{$name}' does not exist" . ($hint ? ", did you mean '{$hint}'?" : '.'));
     }
 }
Example #7
0
 /**
  * Parses single service definition from configuration.
  * @return void
  */
 public static function loadDefinition(ServiceDefinition $definition, $config)
 {
     if ($config === NULL) {
         return;
     } elseif (is_string($config) && interface_exists($config)) {
         $config = ['class' => NULL, 'implement' => $config];
     } elseif ($config instanceof Statement && is_string($config->getEntity()) && interface_exists($config->getEntity())) {
         $config = ['class' => NULL, 'implement' => $config->getEntity(), 'factory' => array_shift($config->arguments)];
     } elseif (!is_array($config) || isset($config[0], $config[1])) {
         $config = ['class' => NULL, 'factory' => $config];
     }
     if (array_key_exists('create', $config)) {
         trigger_error("Key 'create' is deprecated, use 'factory' or 'class' in configuration.", E_USER_DEPRECATED);
         $config['factory'] = $config['create'];
         unset($config['create']);
     }
     $known = ['class', 'factory', 'arguments', 'setup', 'autowired', 'dynamic', 'inject', 'parameters', 'implement', 'run', 'tags'];
     if ($error = array_diff(array_keys($config), $known)) {
         $hints = array_filter(array_map(function ($error) use($known) {
             return Nette\Utils\ObjectMixin::getSuggestion($known, $error);
         }, $error));
         $hint = $hints ? ", did you mean '" . implode("', '", $hints) . "'?" : '.';
         throw new Nette\InvalidStateException(sprintf("Unknown key '%s' in definition of service{$hint}", implode("', '", $error)));
     }
     $config = Helpers::filterArguments($config);
     if (array_key_exists('class', $config) || array_key_exists('factory', $config)) {
         $definition->setClass(NULL);
         $definition->setFactory(NULL);
     }
     if (array_key_exists('class', $config)) {
         Validators::assertField($config, 'class', 'string|Nette\\DI\\Statement|null');
         if (!$config['class'] instanceof Statement) {
             $definition->setClass($config['class']);
         }
         $definition->setFactory($config['class']);
     }
     if (array_key_exists('factory', $config)) {
         Validators::assertField($config, 'factory', 'callable|Nette\\DI\\Statement|null');
         $definition->setFactory($config['factory']);
     }
     if (array_key_exists('arguments', $config)) {
         Validators::assertField($config, 'arguments', 'array');
         $arguments = $config['arguments'];
         if (!Config\Helpers::takeParent($arguments) && !Nette\Utils\Arrays::isList($arguments) && $definition->getFactory()) {
             $arguments += $definition->getFactory()->arguments;
         }
         $definition->setArguments($arguments);
     }
     if (isset($config['setup'])) {
         if (Config\Helpers::takeParent($config['setup'])) {
             $definition->setSetup([]);
         }
         Validators::assertField($config, 'setup', 'list');
         foreach ($config['setup'] as $id => $setup) {
             Validators::assert($setup, 'callable|Nette\\DI\\Statement|array:1', "setup item #{$id}");
             if (is_array($setup)) {
                 $setup = new Statement(key($setup), array_values($setup));
             }
             $definition->addSetup($setup);
         }
     }
     if (isset($config['parameters'])) {
         Validators::assertField($config, 'parameters', 'array');
         $definition->setParameters($config['parameters']);
     }
     if (isset($config['implement'])) {
         Validators::assertField($config, 'implement', 'string');
         $definition->setImplement($config['implement']);
         $definition->setAutowired(TRUE);
     }
     if (isset($config['autowired'])) {
         Validators::assertField($config, 'autowired', 'bool|string|array');
         $definition->setAutowired($config['autowired']);
     }
     if (isset($config['dynamic'])) {
         Validators::assertField($config, 'dynamic', 'bool');
         $definition->setDynamic($config['dynamic']);
     }
     if (isset($config['inject'])) {
         Validators::assertField($config, 'inject', 'bool');
         $definition->addTag(Extensions\InjectExtension::TAG_INJECT, $config['inject']);
     }
     if (isset($config['run'])) {
         trigger_error("Option 'run' is deprecated, use 'run' as tag.", E_USER_DEPRECATED);
         $config['tags']['run'] = (bool) $config['run'];
     }
     if (isset($config['tags'])) {
         Validators::assertField($config, 'tags', 'array');
         if (Config\Helpers::takeParent($config['tags'])) {
             $definition->setTags([]);
         }
         foreach ($config['tags'] as $tag => $attrs) {
             if (is_int($tag) && is_string($attrs)) {
                 $definition->addTag($attrs);
             } else {
                 $definition->addTag($tag, $attrs);
             }
         }
     }
 }
Example #8
0
 /** @internal */
 public function processExtensions()
 {
     $last = $this->getExtensions(Extensions\InjectExtension::class);
     $this->extensions = array_merge(array_diff_key($this->extensions, $last), $last);
     $this->config = Helpers::expand(array_diff_key($this->config, self::$reserved), $this->builder->parameters) + array_intersect_key($this->config, self::$reserved);
     foreach ($first = $this->getExtensions(Extensions\ExtensionsExtension::class) as $name => $extension) {
         $extension->setConfig(isset($this->config[$name]) ? $this->config[$name] : []);
         $extension->loadConfiguration();
     }
     $extensions = array_diff_key($this->extensions, $first);
     foreach (array_intersect_key($extensions, $this->config) as $name => $extension) {
         $extension->setConfig($this->config[$name] ?: []);
     }
     foreach ($extensions as $extension) {
         $extension->loadConfiguration();
     }
     if ($extra = array_diff_key($this->extensions, $extensions, $first)) {
         $extra = implode("', '", array_keys($extra));
         throw new Nette\DeprecatedException("Extensions '{$extra}' were added while container was being compiled.");
     } elseif ($extra = key(array_diff_key($this->config, self::$reserved, $this->extensions))) {
         $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys(self::$reserved + $this->extensions), $extra);
         throw new Nette\InvalidStateException("Found section '{$extra}' in configuration, but corresponding extension is missing" . ($hint ? ", did you mean '{$hint}'?" : '.'));
     }
 }