1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112:
<?php
/*
* This file is part of the webmozart/console package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Console\Api\Args\Format;
use Webmozart\Assert\Assert;
/**
* A command name in the console arguments.
*
* The command name determines which command should be executed. The console
* input may contain one or several command names.
*
* In the example below, the console arguments contain the two command names
* "server" and "add":
*
* ```
* $ console server add localhost
* ```
*
* The last part "localhost" is the argument to the "server add" command.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see CommandOption, ArgsFormat
*/
class CommandName
{
/**
* @var string
*/
private $string;
/**
* @var string[]
*/
private $aliases;
/**
* 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;
}
/**
* Returns the command name as string.
*
* @return string The command name.
*/
public function toString()
{
return $this->string;
}
/**
* Returns the alias names.
*
* @return string[] The aliases of the command name.
*/
public function getAliases()
{
return $this->aliases;
}
/**
* Returns whether a string matches the command name or one of its aliases.
*
* @param string $string The string to test.
*
* @return bool Returns `true` if the given string matches the command name
* or one of its aliases and `false` otherwise.
*/
public function match($string)
{
return $this->string === $string || in_array($string, $this->aliases, true);
}
/**
* Casts the command name to a string.
*
* @return string The command name.
*/
public function __toString()
{
return $this->string;
}
}