/** * {@inheritdoc} */ public static function create($body, DescriptionFactory $descriptionFactory = null, FqsenResolver $resolver = null, Context $context = null) { Assert::string($body); Assert::notEmpty($body); $parts = preg_split('/\\s+/Su', $body, 2); return new static($resolver->resolve($parts[0], $context), $descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context)); }
/** * @return Element */ private function getLastOptionValueElement() { $optionValues = $this->getElement('values'); $items = $optionValues->findAll('css', 'div[data-form-collection="item"]'); Assert::notEmpty($items); return end($items); }
/** * @return Element */ private function getLastProvinceElement() { $provinces = $this->getElement('provinces'); $items = $provinces->findAll('css', 'div[data-form-collection="item"]'); Assert::notEmpty($items); return end($items); }
/** * @param RepositoryInterface $repository * * @return \Closure */ public static function randomOne(RepositoryInterface $repository) { return function (Options $options) use($repository) { $objects = $repository->findAll(); Assert::notEmpty($objects); return $objects[array_rand($objects)]; }; }
/** * @Transform /^"([^"]+)" variant of product "([^"]+)"$/ */ public function getProductVariantByNameAndProduct($variantName, $productName) { $products = $this->productRepository->findByName($productName, 'en_US'); Assert::eq(1, count($products), sprintf('%d products has been found with name "%s".', count($products), $productName)); $productVariants = $this->productVariantRepository->findByNameAndProduct($variantName, 'en_US', $products[0]); Assert::notEmpty($productVariants, sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName)); return $productVariants[0]; }
/** * @Transform /^this order made by "([^"]+)"$/ */ public function getOrderByCustomer($email) { $customer = $this->customerRepository->findOneBy(['email' => $email]); Assert::notNull($customer, sprintf('Cannot find customer with email %s.', $email)); $orders = $this->orderRepository->findByCustomer($customer); Assert::notEmpty($orders); return end($orders); }
/** * Creates a new command name. * * @param string $string The command name. * @param string[] $aliases The alias names. */ public function __construct($string, array $aliases = array()) { Assert::string($string, 'The command name must be a string. Got: %s'); Assert::notEmpty($string, 'The command name must not be empty.'); Assert::regex($string, '~^[a-zA-Z0-9\\-]+$~', 'The command name must contain letters, digits and hyphens only. Got: "%s"'); Assert::allString($aliases, 'The command aliases must be strings. Got: %s'); Assert::allNotEmpty($aliases, 'The command aliases must not be empty.'); Assert::allRegex($aliases, '~^[a-zA-Z0-9\\-]+$~', 'The command aliases must contain letters, digits and hyphens only. Got: "%s"'); $this->string = $string; $this->aliases = $aliases; }
/** * @param RepositoryInterface $repository * * @return \Closure */ public static function randomOne(RepositoryInterface $repository) { return function (Options $options) use($repository) { $objects = $repository->findAll(); if ($objects instanceof Collection) { $objects = $objects->toArray(); } Assert::notEmpty($objects); return $objects[array_rand($objects)]; }; }
/** * {@inheritDoc} */ public function getSubscribedEvents() { return ['phpab.participation.variant_run' => function (array $options) { Assert::notEmpty($options, 'Array passed to closure cannot be empty.'); Assert::keyExists($options, 1, 'Second parameter passed to closure must be instance of Bag.'); Assert::isInstanceOf($options[1], 'PhpAb\\Test\\Bag', 'Second parameter passed to closure must be instance of Bag.'); Assert::keyExists($options, 2, 'Third parameter passed to closure must be instance of VariantInterface.'); Assert::isInstanceOf($options[2], 'PhpAb\\Variant\\VariantInterface', 'Third parameter passed to closure must be instance of VariantInterface.'); /** @var TestInterface $test */ $test = $options[1]->getTest(); /** @var VariantInterface $chosenVariant */ $chosenVariant = $options[2]; // Call the add method $this->addParticipation($test->getIdentifier(), $chosenVariant->getIdentifier()); }]; }
/** * {@inheritDoc} */ public function getSubscribedEvents() { return ['phpab.participation.variant_run' => function ($options) { Assert::notEmpty($options, 'Array passed to closure cannot be empty.'); Assert::keyExists($options, 1, 'Second parameter passed to closure must be instance of Bag.'); Assert::isInstanceOf($options[1], 'PhpAb\\Test\\Bag', 'Second parameter passed to closure must be instance of Bag.'); Assert::keyExists($options, 2, 'Third parameter passed to closure must be instance of VariantInterface.'); Assert::isInstanceOf($options[2], 'PhpAb\\Variant\\VariantInterface', 'Third parameter passed to closure must be instance of VariantInterface.'); /** @var TestInterface $test */ $test = $options[1]->getTest(); Assert::keyExists($test->getOptions(), static::EXPERIMENT_ID, 'A Google Analytics Experiment Id must be set as options.'); $experimentId = $test->getOptions()[static::EXPERIMENT_ID]; /** @var VariantInterface $chosenVariant */ $chosenVariant = $options[2]; $variants = $test->getVariants(); // Get the index number of the element $chosenIndex = array_search($chosenVariant->getIdentifier(), array_keys($variants)); // Call the add method $this->addParticipation($experimentId, $chosenIndex); }]; }
/** * @param string $body */ public function setBody($body) { \Webmozart\Assert\Assert::string($body); \Webmozart\Assert\Assert::notEmpty($body); $this->body = $body; }
/** * @param NodeElement $table * @param string $columnName * * @return int * * @throws \Exception If column was not found */ private function getColumnIndex(NodeElement $table, $columnName) { $rows = $table->findAll('css', 'tr'); Assert::notEmpty($rows, 'There are no rows!'); /** @var NodeElement $firstRow */ $firstRow = $rows[0]; $columns = $firstRow->findAll('css', 'th,td'); foreach ($columns as $index => $column) { /** @var NodeElement $column */ if (0 === stripos($column->getText(), $columnName)) { return $index; } } throw new \InvalidArgumentException(sprintf('Column with name "%s" not found!', $columnName)); }
/** * @param string $collection * * @return NodeElement */ private function getLastCollectionItem($collection) { $items = $this->getCollectionItems($collection); Assert::notEmpty($items); return end($items); }
/** * @param PromotionEligibilityCheckerInterface[] $promotionEligibilityCheckers */ public function __construct(array $promotionEligibilityCheckers) { Assert::notEmpty($promotionEligibilityCheckers); Assert::allIsInstanceOf($promotionEligibilityCheckers, PromotionEligibilityCheckerInterface::class); $this->promotionEligibilityCheckers = $promotionEligibilityCheckers; }
/** * {@inheritdoc} */ public function remove($query, $language = 'glob') { if (null === $this->json) { $this->load(); } $this->failUnlessGlob($language); $query = $this->sanitizePath($query); Assert::notEmpty(trim($query, '/'), 'The root directory cannot be removed.'); $removed = $this->removeReferences($query); $this->flush(); return $removed; }
/** * Sets the method of the command handler that should be executed when the * configured command is run. * * The method receives three arguments: * * * {@link Args} `$args`: The console arguments. * * {@link IO} `$io`: The I/O. * * {@link Command} `$command`: The executed command. * * @param string $handlerMethod The method name. * * @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. * * @see getHandlerMethod() */ public function setHandlerMethod($handlerMethod) { Assert::string($handlerMethod, 'The handler method must be a string. Got: %s'); Assert::notEmpty($handlerMethod, 'The handler method must not be empty.'); $this->handlerMethod = $handlerMethod; return $this; }
/** * @return NodeElement */ private function getFirstImageElement() { $imageElements = $this->getImageElements(); Assert::notEmpty($imageElements); return reset($imageElements); }
/** * @param NodeElement $table * @param string $fieldName * * @return int * * @throws \InvalidArgumentException */ private function getColumnIndex(NodeElement $table, $fieldName) { $rows = $table->findAll('css', 'tr'); Assert::notEmpty($rows, 'There are no rows!'); /** @var NodeElement $headerRow */ $headerRow = $rows[0]; $headers = $headerRow->findAll('css', 'th,td'); /** @var NodeElement $column */ foreach ($headers as $index => $column) { $columnFieldName = $this->getColumnFieldName($column); if ($fieldName === $columnFieldName) { return $index; } } throw new \InvalidArgumentException(sprintf('Column with name "%s" not found!', $fieldName)); }
/** * Registers a command handler for the given name. * * @param string $name The handler name. * @param object|callable $handler The handler or a factory callback that * creates the handler. */ public function register($name, $handler) { Assert::string($name, 'The handler name must be a string. Got: %s'); Assert::notEmpty($name, 'The handler name must not be empty.'); if (!is_object($handler)) { Assert::isCallable($handler, 'The handler must be a callable or an object. Got: %s'); } $this->handlers[$name] = $handler; if (!$this->selectedHandler) { $this->selectedHandler = $name; } }
/** * Returns an option by its long or short name. * * @param string $name The long or short option name. * @param bool $includeBase Whether to include options in the base format * in the search. * * @return Option The option. * * @throws NoSuchOptionException If the option with the given name does not * not exist. */ public function getOption($name, $includeBase = true) { Assert::string($name, 'The option name must be a string. Got: %s'); Assert::notEmpty($name, 'The option name must not be empty.'); Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s'); if (isset($this->options[$name])) { return $this->options[$name]; } if (isset($this->optionsByShortName[$name])) { return $this->optionsByShortName[$name]; } if ($includeBase && $this->baseFormat) { return $this->baseFormat->getOption($name); } throw NoSuchOptionException::forOptionName($name); }
private function assertShortAliasValid($alias) { Assert::string($alias, 'An option alias must be a string or null. Got: %s'); Assert::notEmpty($alias, 'An option alias must not be empty.'); Assert::regex($alias, '~^[a-zA-Z]$~', 'A short option alias must be exactly one letter. Got: "%s"'); }
/** * Creates a new argument. * * @param string $name The argument name * @param int $flags A bitwise combination of the flag constants. * @param string $description A human-readable description of the argument. * @param mixed $defaultValue The default value of the argument (must be * null for the flag {@link self::REQUIRED}). */ public function __construct($name, $flags = 0, $description = null, $defaultValue = null) { Assert::string($name, 'The argument name must be a string. Got: %s'); Assert::notEmpty($name, 'The argument name must not be empty.'); Assert::startsWithLetter($name, 'The argument name must start with a letter.'); Assert::regex($name, '~^[a-zA-Z0-9\\-]+$~', 'The argument name must contain letters, digits and hyphens only.'); Assert::nullOrString($description, 'The argument description must be a string or null. Got: %s'); Assert::nullOrNotEmpty($description, 'The argument description must not be empty.'); $this->assertFlagsValid($flags); $this->addDefaultFlags($flags); $this->name = $name; $this->flags = $flags; $this->description = $description; $this->defaultValue = $this->isMultiValued() ? array() : null; if ($this->isOptional() || null !== $defaultValue) { $this->setDefaultValue($defaultValue); } }
/** * {@inheritdoc} */ public function remove($query, $language = 'glob') { $resources = $this->find($query, $language); $nbOfResources = count($this->resources); // Run the assertion after find(), so that we know that $query is valid Assert::notEmpty(trim($query, '/'), 'The root directory cannot be removed.'); foreach ($resources as $resource) { $this->removeResource($resource); } return $nbOfResources - count($this->resources); }
/** * Sets the short option name of the command. * * The short name must consist of a single letter. The short name is * preceded by a single dash "-" when calling the command: * * ``` * $ server -d localhost * ``` * * In the example above, "d" is the short name of the "server --delete" * command. * * @param string $shortName The short option name. * * @return static The current instance. */ public function setShortName($shortName) { if (null !== $shortName) { Assert::string($shortName, 'The short command name must be a string or null. Got: %s'); Assert::notEmpty($shortName, 'The short command name must not be empty.'); Assert::regex($shortName, '~^[a-zA-Z]$~', 'The short command name must contain a single letter. Got: %s'); } // Reset short name preference when unsetting the short name if (null === $shortName && false === $this->longNamePreferred) { $this->longNamePreferred = null; } $this->shortName = $shortName; return $this; }
/** * @return NodeElement */ private function getLastImageElement() { $imageElements = $this->getImageElements(); Assert::notEmpty($imageElements); return end($imageElements); }
/** * @param string $collection * * @return NodeElement */ private function getLastAddedCollectionItem($collection) { $items = $this->getElement($collection)->findAll('css', 'div[data-form-collection="item"]'); Assert::notEmpty($items); return end($items); }
private function assertShortNameValid($shortName, $flags) { if (null !== $shortName) { Assert::string($shortName, 'The short option name must be a string or null. Got: %s'); Assert::notEmpty($shortName, 'The short option name must not be empty.'); Assert::regex($shortName, '~^[a-zA-Z]$~', 'The short option name must be exactly one letter. Got: "%s"'); } if (null === $shortName && $flags & self::PREFER_SHORT_NAME) { throw new InvalidArgumentException('The short option name must be given if the option flag PREFER_SHORT_NAME is selected.'); } }
/** * Returns whether the format contains a specific option. * * You can either pass the long or the short name of the option. * * @param string $name The long or short option name. * @param bool $includeBase Whether to include options in the base format * in the search. * * @return bool Returns `true` if the option with the given name could be * found and `false` otherwise. */ public function hasOption($name, $includeBase = true) { Assert::string($name, 'The option name must be a string or an integer. Got: %s'); Assert::notEmpty($name, 'The option name must not be empty.'); Assert::boolean($includeBase, 'The parameter $includeBase must be a boolean. Got: %s'); if (isset($this->options[$name]) || isset($this->optionsByShortName[$name])) { return true; } if ($includeBase && $this->baseFormat) { return $this->baseFormat->hasOption($name); } return false; }
/** * @param string $recipient * * @throws /InvalidArgumentException */ private function assertRecipientIsValid($recipient) { Assert::notEmpty($recipient, 'The recipient cannot be empty.'); Assert::string($recipient, sprintf('The recipient must be a string, %s given.', gettype($recipient))); Assert::notEq(false, filter_var($recipient, FILTER_VALIDATE_EMAIL), 'Given recipient is not a valid email address.'); }
/** * Sets the help text of the application. * * @param string $help The help text. * * @return static The current instance. */ public function setHelp($help) { if (null !== $help) { Assert::string($help, 'The help text must be a string. Got: %s'); Assert::notEmpty($help, 'The help text must not be empty.'); } $this->help = $help; return $this; }