/** * @param string $value */ public static function create($value) { try { Assert::string($value, 'Value must be a string'); Assert::regex($value, '{^(?:.*\\d.*){6}$}', 'Value must contain 6 digits'); } catch (\InvalidArgumentException $e) { throw E::wrap($e); } return new self(preg_replace('{[^0-9]}', '', $value)); }
/** * 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 string|SortCode $sortCode * @param string $accountNumber */ public function __construct($sortCode, $accountNumber) { try { if (!$sortCode instanceof SortCode) { Assert::string($sortCode, 'Sort code must be a string or instance of SortCode'); $sortCode = SortCode::create($sortCode); } Assert::string($accountNumber, 'Account number must be a string'); Assert::regex($accountNumber, '{^(?:.*\\d.*){6}$}', 'Account number must contain at least 6 digits'); } catch (\InvalidArgumentException $e) { throw E::wrap($e); } $this->sortCode = $sortCode; $this->accountNumber = preg_replace('{[^0-9]}', '', $accountNumber); }
/** * Sets the name of the application. * * @param string $name The application name. * * @return static The current instance. * * @see getName() */ public function setName($name) { if (null !== $name) { Assert::string($name, 'The application name must be a string. Got: %s'); Assert::notEmpty($name, 'The application name must not be empty.'); Assert::regex($name, '~^[a-zA-Z0-9\\-]+$~', 'The application name must contain letters, numbers and hyphens only. Did you mean to call setDisplayName()?'); } $this->name = $name; return $this; }
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.'); } }
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); } }
/** * 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; }
/** * Adds an alias name. * * An alias is an alternative name that can be used when calling the * command. Aliases are a useful way for migrating a command from one name * to another. * * Existing alias names are preserved. * * @param string $alias The alias name to add. * * @return static The current instance. * * @see addAliases(), setAliases(), getAlias() */ public function addAlias($alias) { Assert::string($alias, 'The command alias must be a string. Got: %s'); Assert::notEmpty($alias, 'The command alias must not be empty.'); Assert::regex($alias, '~^[a-zA-Z0-9\\-]+$~', 'The command alias should contain letters, digits and hyphens only. Got: %s'); $this->aliases[] = $alias; return $this; }