Removed unused dependencies
This commit is contained in:
parent
ee00f549be
commit
1f802a3fa0
73 changed files with 0 additions and 13396 deletions
85
src/ncc/ThirdParty/beberlei/assert/Assert.php
vendored
85
src/ncc/ThirdParty/beberlei/assert/Assert.php
vendored
|
@ -1,85 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to kontakt@beberlei.de so I can send you a copy immediately.
|
||||
*/
|
||||
|
||||
namespace ncc\Assert;
|
||||
|
||||
/**
|
||||
* AssertionChain factory.
|
||||
*/
|
||||
abstract class Assert
|
||||
{
|
||||
/** @var string */
|
||||
protected static $lazyAssertionExceptionClass = LazyAssertionException::class;
|
||||
|
||||
/** @var string */
|
||||
protected static $assertionClass = Assertion::class;
|
||||
|
||||
/**
|
||||
* Start validation on a value, returns {@link AssertionChain}.
|
||||
*
|
||||
* The invocation of this method starts an assertion chain
|
||||
* that is happening on the passed value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param string|callable|null $defaultMessage
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* Assert::that($value)->notEmpty()->integer();
|
||||
* Assert::that($value)->nullOr()->string()->startsWith("Foo");
|
||||
*
|
||||
* The assertion chain can be stateful, that means be careful when you reuse
|
||||
* it. You should never pass around the chain.
|
||||
*/
|
||||
public static function that($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
|
||||
{
|
||||
$assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath);
|
||||
|
||||
return $assertionChain->setAssertionClassName(static::$assertionClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start validation on a set of values, returns {@link AssertionChain}.
|
||||
*
|
||||
* @param mixed $values
|
||||
* @param string|callable|null $defaultMessage
|
||||
*/
|
||||
public static function thatAll($values, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
|
||||
{
|
||||
return static::that($values, $defaultMessage, $defaultPropertyPath)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start validation and allow NULL, returns {@link AssertionChain}.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param string|callable|null $defaultMessage
|
||||
*/
|
||||
public static function thatNullOr($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
|
||||
{
|
||||
return static::that($value, $defaultMessage, $defaultPropertyPath)->nullOr();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a lazy assertion object.
|
||||
*/
|
||||
public static function lazy(): LazyAssertion
|
||||
{
|
||||
$lazyAssertion = new LazyAssertion();
|
||||
|
||||
return $lazyAssertion
|
||||
->setAssertClass(\get_called_class())
|
||||
->setExceptionClass(static::$lazyAssertionExceptionClass);
|
||||
}
|
||||
}
|
2797
src/ncc/ThirdParty/beberlei/assert/Assertion.php
vendored
2797
src/ncc/ThirdParty/beberlei/assert/Assertion.php
vendored
File diff suppressed because it is too large
Load diff
|
@ -1,247 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to kontakt@beberlei.de so I can send you a copy immediately.
|
||||
*/
|
||||
|
||||
namespace ncc\Assert;
|
||||
|
||||
use LogicException;
|
||||
|
||||
/**
|
||||
* Chaining builder for assertions.
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*
|
||||
* @method AssertionChain alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric.
|
||||
* @method AssertionChain base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
|
||||
* @method AssertionChain between(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
|
||||
* @method AssertionChain betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit.
|
||||
* @method AssertionChain betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths.
|
||||
* @method AssertionChain boolean(string|callable $message = null, string $propertyPath = null) Assert that value is php boolean.
|
||||
* @method AssertionChain choice(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices.
|
||||
* @method AssertionChain choicesNotEmpty(array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content.
|
||||
* @method AssertionChain classExists(string|callable $message = null, string $propertyPath = null) Assert that the class exists.
|
||||
* @method AssertionChain contains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars.
|
||||
* @method AssertionChain count(int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count.
|
||||
* @method AssertionChain date(string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format.
|
||||
* @method AssertionChain defined(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
|
||||
* @method AssertionChain digit(string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit.
|
||||
* @method AssertionChain directory(string|callable $message = null, string $propertyPath = null) Assert that a directory exists.
|
||||
* @method AssertionChain e164(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number.
|
||||
* @method AssertionChain email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
|
||||
* @method AssertionChain endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
|
||||
* @method AssertionChain eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==).
|
||||
* @method AssertionChain eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset.
|
||||
* @method AssertionChain extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
|
||||
* @method AssertionChain extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed.
|
||||
* @method AssertionChain false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.
|
||||
* @method AssertionChain file(string|callable $message = null, string $propertyPath = null) Assert that a file exists.
|
||||
* @method AssertionChain float(string|callable $message = null, string $propertyPath = null) Assert that value is a php float.
|
||||
* @method AssertionChain greaterOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit.
|
||||
* @method AssertionChain greaterThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit.
|
||||
* @method AssertionChain implementsInterface(string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface.
|
||||
* @method AssertionChain inArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice().
|
||||
* @method AssertionChain integer(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer.
|
||||
* @method AssertionChain integerish(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish.
|
||||
* @method AssertionChain interfaceExists(string|callable $message = null, string $propertyPath = null) Assert that the interface exists.
|
||||
* @method AssertionChain ip(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address.
|
||||
* @method AssertionChain ipv4(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address.
|
||||
* @method AssertionChain ipv6(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address.
|
||||
* @method AssertionChain isArray(string|callable $message = null, string $propertyPath = null) Assert that value is an array.
|
||||
* @method AssertionChain isArrayAccessible(string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object.
|
||||
* @method AssertionChain isCallable(string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable.
|
||||
* @method AssertionChain isCountable(string|callable $message = null, string $propertyPath = null) Assert that value is countable.
|
||||
* @method AssertionChain isInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name.
|
||||
* @method AssertionChain isJsonString(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string.
|
||||
* @method AssertionChain isObject(string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object.
|
||||
* @method AssertionChain isResource(string|callable $message = null, string $propertyPath = null) Assert that value is a resource.
|
||||
* @method AssertionChain isTraversable(string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object.
|
||||
* @method AssertionChain keyExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array.
|
||||
* @method AssertionChain keyIsset(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset().
|
||||
* @method AssertionChain keyNotExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array.
|
||||
* @method AssertionChain length(int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length.
|
||||
* @method AssertionChain lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit.
|
||||
* @method AssertionChain lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit.
|
||||
* @method AssertionChain max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit.
|
||||
* @method AssertionChain maxCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements.
|
||||
* @method AssertionChain maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars.
|
||||
* @method AssertionChain methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object.
|
||||
* @method AssertionChain min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit.
|
||||
* @method AssertionChain minCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements.
|
||||
* @method AssertionChain minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long.
|
||||
* @method AssertionChain noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty.
|
||||
* @method AssertionChain notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank.
|
||||
* @method AssertionChain notContains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars.
|
||||
* @method AssertionChain notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty.
|
||||
* @method AssertionChain notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty.
|
||||
* @method AssertionChain notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==).
|
||||
* @method AssertionChain notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices.
|
||||
* @method AssertionChain notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name.
|
||||
* @method AssertionChain notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null.
|
||||
* @method AssertionChain notRegex(string $pattern, string|callable $message = null, string $propertyPath = null) # Assert that value does not match a regex.
|
||||
* @method AssertionChain notSame(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===).
|
||||
* @method AssertionChain null(string|callable $message = null, string $propertyPath = null) Assert that value is null.
|
||||
* @method AssertionChain numeric(string|callable $message = null, string $propertyPath = null) Assert that value is numeric.
|
||||
* @method AssertionChain objectOrClass(string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists.
|
||||
* @method AssertionChain phpVersion(mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version.
|
||||
* @method AssertionChain propertiesExist(array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist.
|
||||
* @method AssertionChain propertyExists(string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists.
|
||||
* @method AssertionChain range(mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers.
|
||||
* @method AssertionChain readable(string|callable $message = null, string $propertyPath = null) Assert that the value is something readable.
|
||||
* @method AssertionChain regex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex.
|
||||
* @method AssertionChain same(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===).
|
||||
* @method AssertionChain satisfy(callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback.
|
||||
* @method AssertionChain scalar(string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar.
|
||||
* @method AssertionChain startsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars.
|
||||
* @method AssertionChain string(string|callable $message = null, string $propertyPath = null) Assert that value is a string.
|
||||
* @method AssertionChain subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name.
|
||||
* @method AssertionChain true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True.
|
||||
* @method AssertionChain uniqueValues(string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality).
|
||||
* @method AssertionChain url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL.
|
||||
* @method AssertionChain uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID.
|
||||
* @method AssertionChain version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions.
|
||||
* @method AssertionChain writeable(string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable.
|
||||
*/
|
||||
class AssertionChain
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @var string|callable|null
|
||||
*/
|
||||
private $defaultMessage;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $defaultPropertyPath;
|
||||
|
||||
/**
|
||||
* Return each assertion as always valid.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $alwaysValid = false;
|
||||
|
||||
/**
|
||||
* Perform assertion on every element of array or traversable.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $all = false;
|
||||
|
||||
/** @var string|Assertion Class to use for assertion calls */
|
||||
private $assertionClassName = 'Assert\Assertion';
|
||||
|
||||
/**
|
||||
* AssertionChain constructor.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param string|callable|null $defaultMessage
|
||||
*/
|
||||
public function __construct($value, $defaultMessage = null, string $defaultPropertyPath = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->defaultMessage = $defaultMessage;
|
||||
$this->defaultPropertyPath = $defaultPropertyPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call assertion on the current value in the chain.
|
||||
*
|
||||
* @param string $methodName
|
||||
* @param array $args
|
||||
*/
|
||||
public function __call($methodName, $args): AssertionChain
|
||||
{
|
||||
if (true === $this->alwaysValid) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
$method = new \ReflectionMethod($this->assertionClassName, $methodName);
|
||||
} catch (\ReflectionException $exception) {
|
||||
throw new \RuntimeException("Assertion '".$methodName."' does not exist.");
|
||||
}
|
||||
|
||||
\array_unshift($args, $this->value);
|
||||
$params = $method->getParameters();
|
||||
|
||||
foreach ($params as $idx => $param) {
|
||||
if (isset($args[$idx])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($param->getName()) {
|
||||
case 'message':
|
||||
$args[$idx] = $this->defaultMessage;
|
||||
break;
|
||||
case 'propertyPath':
|
||||
$args[$idx] = $this->defaultPropertyPath;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->all) {
|
||||
$methodName = 'all'.$methodName;
|
||||
}
|
||||
|
||||
\call_user_func_array([$this->assertionClassName, $methodName], $args);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch chain into validation mode for an array of values.
|
||||
*/
|
||||
public function all(): AssertionChain
|
||||
{
|
||||
$this->all = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch chain into mode allowing nulls, ignoring further assertions.
|
||||
*/
|
||||
public function nullOr(): AssertionChain
|
||||
{
|
||||
if (null === $this->value) {
|
||||
$this->alwaysValid = true;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setAssertionClassName($className): AssertionChain
|
||||
{
|
||||
if (!\is_string($className)) {
|
||||
throw new LogicException('Exception class name must be passed as a string');
|
||||
}
|
||||
|
||||
if (Assertion::class !== $className && !\is_subclass_of($className, Assertion::class)) {
|
||||
throw new LogicException($className.' is not (a subclass of) '.Assertion::class);
|
||||
}
|
||||
|
||||
$this->assertionClassName = $className;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to kontakt@beberlei.de so I can send you a copy immediately.
|
||||
*/
|
||||
|
||||
namespace ncc\Assert;
|
||||
|
||||
use Throwable;
|
||||
|
||||
interface AssertionFailedException extends Throwable
|
||||
{
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPropertyPath();
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue();
|
||||
|
||||
public function getConstraints(): array;
|
||||
}
|
455
src/ncc/ThirdParty/beberlei/assert/CHANGELOG.md
vendored
455
src/ncc/ThirdParty/beberlei/assert/CHANGELOG.md
vendored
|
@ -1,455 +0,0 @@
|
|||
# Change Log
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## 3.2.7 - 2019-12-19
|
||||
|
||||
### Fixes
|
||||
- Reinstated the `@method` return type for `Assert\LazyAssertion` methods to show that the return type is `LazyAssertion`.
|
||||
|
||||
## 3.2.6 - 2019-10-10
|
||||
|
||||
### Fixes
|
||||
- Make `Assert\Assertion::stringify()` UTF-8 safe (Thanks to [Pedram Azimaei](https://github.com/beberlei/assert/pull/290))
|
||||
|
||||
## 3.2.5 - 2019-10-10 - Fix the broken things release.
|
||||
|
||||
### Notice
|
||||
- Sigh!
|
||||
- Richard Quadling
|
||||
|
||||
### Fixes
|
||||
- REALLY Removed dependency of the intl extension.
|
||||
- Updated the Docblocks for `Assert\Assertion::all()` helper to show that the value is expected to be an array.
|
||||
|
||||
## 3.2.4 - 2019-10-10 - Fix the broken things release.
|
||||
|
||||
### Notice
|
||||
- It seems I've been slightly lax in verifying the signature changes and expected extensions.
|
||||
Hopefully, both of these have been fixed in this version.
|
||||
Truly sorry for breaking the internet!
|
||||
- Richard Quadling
|
||||
|
||||
### Fixes
|
||||
- Restored `\Assert\Assertion::createException()` signature to 3.2.2.
|
||||
- Removed dependency of the intl extension. If the extension is available, then `Assert\Assertion::count()`,
|
||||
`Assert\Assertion::isCountable()`, `Assert\Assertion::minCount()`, and `Assert\Assertion::maxCount()` will operate on
|
||||
`ResourceBundles`.
|
||||
- Fixed the `@method` return type for `Assert\LazyAssertion` methods to show that the return type is `static` for
|
||||
extensions of `Assert\LazyAssertion`.
|
||||
*NOTE :* Docblock does not have the ability to differentiate between a non static `@method` whose returns type is of
|
||||
the subclass and a `@method` that is called statically ([PSR-5#899](https://github.com/php-fig/fig-standards/pull/899)).
|
||||
So the use of `static static` is a fudge that sort of works for IDEs that need to know about the method that MAY be
|
||||
overridden in a subclass.
|
||||
|
||||
## 3.2.3 - 2019-08-23
|
||||
|
||||
### Other changes
|
||||
- Added type hints and documentation consistency (Thanks to [Andru Cherny](https://github.com/beberlei/assert/pull/286))
|
||||
|
||||
## 3.2.2 - 2019-08-23
|
||||
|
||||
### Added assertions
|
||||
- `Assertion::eqArraySubset()` (Thanks to [Anna Filina](https://github.com/beberlei/assert/pull/283))
|
||||
|
||||
## 3.2.1 - 2019-05-28
|
||||
|
||||
### Fixes
|
||||
- Updated regex for `Assert\Assertion::url()` (Thanks to [Christophe Histaesse](https://github.com/beberlei/assert/pull/281))
|
||||
- Fixed broken regex for `Assert\Assertion::url()` (Thanks to [Menno Holtkamp](https://github.com/beberlei/assert/issues/275))
|
||||
|
||||
### Other changes
|
||||
- Added PHP 7.3.0, PHP 7.3.1, and PHP 7.3.2 to Travis pipeline as there are differences in PCRE
|
||||
- Updated docblocks for `Assert\Assertion::NullOrXxxx()` to show that the first parameter can be null.
|
||||
- Updated docblocks for `Assert\LazyAssertion` to show that the return type is `$this` to aid IDE's static analysis.
|
||||
|
||||
## 3.2.0 - 2018-12-24
|
||||
|
||||
### Added assertions
|
||||
- `Assertion::isCountable()` (Thanks to [Baptiste Clavié](https://github.com/beberlei/assert/pull/268))
|
||||
- `Assertion::maxCount()` (Thanks to [Baptiste Clavié](https://github.com/beberlei/assert/pull/269))
|
||||
- `Assertion::minCount()` (Thanks to [Baptiste Clavié](https://github.com/beberlei/assert/pull/269))
|
||||
- `Assertion::nonContains()` (Thanks to [Baptiste Clavié](https://github.com/beberlei/assert/pull/270))
|
||||
|
||||
### Other changes
|
||||
- Added PHP 7.3 to Travis pipeline
|
||||
- Added support for `\ResourceBundle` and `\SimpleXMLElement` to `Assertion::count()`.
|
||||
|
||||
## 3.1.0 - 2018-10-29
|
||||
|
||||
### Other changes
|
||||
- Refactor assertion messages for `Assert\Assertion::notEq()`, `Assert\Assertion::notSame()`, and `Assert\Assertion::notInArray()` (Thanks to [Rick Kuipers](https://github.com/beberlei/assert/pull/259))
|
||||
|
||||
## 3.0.1 - 2018-07-04
|
||||
|
||||
### Added assertions
|
||||
- `Assertion::notRegex()` (Thanks to [Thomas Müller](https://github.com/beberlei/assert/pull/261))
|
||||
|
||||
### Other changes
|
||||
- Upgraded regex and unit tests for `Assert\Assertion::url()` to latest from Symfony/Validator
|
||||
|
||||
## 3.0.0 - 2018-07-04
|
||||
|
||||
### Changes
|
||||
- Deprecate support for PHP < 7.0
|
||||
|
||||
### Fixes
|
||||
- An `AssertionFailedException` must be a `Throwable` (Thanks to [Marco Pivetta](https://github.com/beberlei/assert/pull/256))
|
||||
|
||||
## 2.9.8 - 2019-05-28
|
||||
|
||||
### Fixes
|
||||
- Updated regex for `Assert\Assertion::url()` (Thanks to [Christophe Histaesse](https://github.com/beberlei/assert/pull/281))
|
||||
|
||||
## 2.9.7 - 2019-02-19
|
||||
|
||||
### Fixes
|
||||
- Fixed broken regex for `Assert\Assertion::url()` (Thanks to [Menno Holtkamp](https://github.com/beberlei/assert/issues/275))
|
||||
|
||||
## 2.9.6 - 2018-04-16
|
||||
|
||||
### Fixes
|
||||
- Made constraints in exceptions consistent for all assertions (Thanks to [Peter Kruithof](https://github.com/beberlei/assert/pull/255))
|
||||
|
||||
## 2.9.5 - 2018-04-16
|
||||
|
||||
### Fixes
|
||||
- Remove DocBlock entry causing exception in third party DocBlock parser (Thanks to [Koutsoumpos Valantis](https://github.com/beberlei/assert/issues/251))
|
||||
|
||||
## 2.9.4 - 2018-04-09
|
||||
|
||||
### Fixes
|
||||
- Prevent date overflow in Assertion::date() by reset preset date value (Thanks to [Nobuhiro Nakamura](https://github.com/beberlei/assert/issues/250))
|
||||
|
||||
## 2.9.3 - 2018-03-16
|
||||
|
||||
### Changes
|
||||
- Expand error for `\Assert\Assertion::count()` to include the supplied count (Thanks to [Yoann Blot](https://github.com/beberlei/assert/issues/247))
|
||||
|
||||
## 2.9.2 - 2018-01-25
|
||||
|
||||
### Fixes
|
||||
- Usage of custom extended Assertion class in LazyAssertion (Thanks to [Marek Štípek](https://github.com/beberlei/assert/pull/245))
|
||||
|
||||
## 2.9.1 - 2018-01-25
|
||||
|
||||
### Deprecation notice
|
||||
- Support for PHP 5 will be dropped at the end of 2018, in line with PHP's [supported versions](http://php.net/supported-versions.php).
|
||||
|
||||
### Fixes
|
||||
- `\Assert\Assertion::generateMessage()` will now receive the default message for an assertion if one is not supplied (Thanks to [Romans Malinovskis](https://github.com/beberlei/assert/issues/225))
|
||||
|
||||
## 2.8.1 - 2017-11-30
|
||||
|
||||
### Fixes
|
||||
- `Assertion::integerish()` has had several issues in the last couple of versions.
|
||||
Hopefully these are now fixed.
|
||||
Thanks to [Erik Roelofs](https://github.com/beberlei/assert/issues/243) and [Michał Mleczko](https://github.com/beberlei/assert/issues/240)
|
||||
|
||||
### Deprecation notice
|
||||
- The functions `\Assert\that()`, `\Assert\thatAll()`, `\Assert\thatNullOr()`, and `\Assert\lazy()` are no longer marked as deprecated.
|
||||
Both the functional and static constructors work together. Whichever you wish to use is a personal preference.
|
||||
|
||||
## 2.7.11 - 2017-11-13
|
||||
|
||||
### Fixes
|
||||
- `Assertion::integerish(0)` and `Assertion::integerish('0')` now assert correctly.
|
||||
|
||||
## 2.7.10 - 2017-11-13
|
||||
|
||||
### Added assertions
|
||||
- `Assertion::base64()` (Thanks to [Pablo Kowalczyk](https://github.com/beberlei/assert/pull/232))
|
||||
|
||||
## 2.7.9 - 2017-11-13
|
||||
|
||||
### Fixes
|
||||
- `Assertion::integerish()` now correctly asserts integers with leading zeros in strings (Thanks to [Albert Casademont](https://github.com/beberlei/assert/pull/227#issuecomment-343961009))
|
||||
|
||||
## 2.7.8 - 2017-10-20
|
||||
|
||||
### Fixes
|
||||
- `Assertion::integerish()` now throws exception as expected (Thanks to [Thomas Flack](https://github.com/beberlei/assert/issues/235))
|
||||
|
||||
## 2.7.7 - 2017-10-18
|
||||
|
||||
### Fixes
|
||||
- Basic Auth usernames and passwords can contain '.' (Thanks to [Fede Isas](https://github.com/beberlei/assert/issues/234))
|
||||
|
||||
## 2.7.6 - 2017-05-04
|
||||
|
||||
### Fixes
|
||||
- Fixed stringification of booleans (Thanks to [Philipp Rieber](https://github.com/beberlei/assert/issues/226))
|
||||
|
||||
## 2.7.5 - 2017-04-29
|
||||
### Added assertions
|
||||
- `Assert\Assertion:isResource()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/222))
|
||||
|
||||
### Other changes
|
||||
- Corrected doc-block for `Assert\Assertion::propertiesExist()`.
|
||||
|
||||
## 2.7.4 - 2017-03-14
|
||||
### Added assertions
|
||||
- `Assert\Assertion::objectOrClass()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/218))
|
||||
- `Assert\Assertion::propertyExists()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/218))
|
||||
- `Assert\Assertion::propertiesExist()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/218))
|
||||
|
||||
### Other changes
|
||||
- Unit tests no longer using deprecated exception methods (Thanks to [Richard Quadling](https://github.com/beberlei/assert/pull/217))
|
||||
- All global namespaced functions have been optimised (Thanks to [Andreas Möller](https://github.com/beberlei/assert/pull/211))
|
||||
|
||||
## 2.7.3 - 2017-01-24
|
||||
|
||||
### Fixes
|
||||
- Fix `Assert\Assertion::integerish()` when used with a resource (Thanks to [manuxi](https://github.com/beberlei/assert/issues/206))
|
||||
|
||||
## 2.7.2 - 2017-01-09
|
||||
|
||||
### Fixes
|
||||
- Backward compatibility fixes for PHP 5.3
|
||||
|
||||
## 2.7.1 - 2017-01-06
|
||||
### Added assertions
|
||||
- `Assert\Assertion::extensionVersion()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/205))
|
||||
- `Assert\Assertion::phpVersion()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/203))
|
||||
- `Assert\Assertion::version()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/203))
|
||||
|
||||
### Other changes
|
||||
- Exception messages can now be constructed via a callback.
|
||||
- Documentation now includes types.
|
||||
|
||||
## 2.6.9 - 2017-01-04
|
||||
### Added assertions
|
||||
- `Assert\Assertion::defined()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/193))
|
||||
- `Assert\Assertion::extensionLoaded()` (Thanks to [Timothy Younger](https://github.com/beberlei/assert/pull/201))
|
||||
|
||||
### Other changes
|
||||
- Added types to generated documentation.
|
||||
- Added PHPStan analysis for PHP 7+
|
||||
|
||||
## 2.6.8 - 2016-12-05
|
||||
|
||||
### Fixes
|
||||
- All exceptions thrown by this library extend `\Assert\InvalidArgumentException` (Thanks to [Richard Quadling](https://github.com/beberlei/assert/pull/187))
|
||||
|
||||
### Other changes
|
||||
- Update to php-cs-fixer ^2.0 release (Thanks to [Raphael Stolt](https://github.com/beberlei/assert/pull/188))
|
||||
- Simplify XDebug disabling for Travis (Thanks to [Raphael Stolt](https://github.com/beberlei/assert/pull/189))
|
||||
- Use PSR-4 autoloading (Thanks to [Andreas Möller](https://github.com/beberlei/assert/pull/190))
|
||||
- Enable Composer package sorting (Thanks to [Raphael Stolt](https://github.com/beberlei/assert/pull/191))
|
||||
- Fix grammar in documentation (Thanks to [Adrian Föder](https://github.com/beberlei/assert/pull/192))
|
||||
|
||||
## 2.6.7 - 2016-11-14
|
||||
|
||||
### Fixes
|
||||
- [Fix the interfaceExists assertion](https://github.com/beberlei/assert/pull/182)
|
||||
- Fixed issue in document generator (Thanks to [Taco van den Broek](https://github.com/tacovandenbroek))
|
||||
|
||||
### Other changes
|
||||
- [Added ability to capture multiple errors on a single value in a chain](https://github.com/beberlei/assert/pull/186) (Thanks to [Alec Carpenter](https://github.com/alecgunnar))
|
||||
- [Use static factory methods instead of functions in the Assert namespace](https://github.com/beberlei/assert/pull/184) (Thanks to [Taco van den Broek](https://github.com/tacovandenbroek))
|
||||
|
||||
### Deprecation notice
|
||||
- The functions in the Assert namespace (`\Assert\that()`, `\Assert\thatAll()`, `\Assert\thatNullOr()` and `\Assert\lazy()`) are now marked as deprecated.
|
||||
They will be removed in the next major release.
|
||||
They have been replaced with the static methods `\Assert\Assert::that()`, `\Assert\Assert::thatAll()`, `\Assert\Assert::thatNullOr()` and `\Assert\Assert::lazy()`
|
||||
|
||||
## 2.6.6 - 2016-10-31
|
||||
|
||||
### Other changes
|
||||
- [Make all assertions return true on success, so that it can be used inside PHP 7 assert()](https://github.com/beberlei/assert/issues/136)
|
||||
|
||||
## 2.6.5 - 2016-10-11
|
||||
### Added assertions
|
||||
- `Assert\Assertion::between()`
|
||||
- `Assert\Assertion::betweenExclusive()`
|
||||
|
||||
### Fixes
|
||||
- Allow `http://localhost` as a valid URL - fixes [Assertion::url('http://localhost') not a valid url?](https://github.com/beberlei/assert/issues/133)
|
||||
|
||||
### Other changes
|
||||
- Upgraded regex and unit tests for `Assert\Assertion::url()` to latest from Symfony/Validator
|
||||
- Added PHP-CS
|
||||
- Speed up of builds for Travis
|
||||
|
||||
## 2.6.4 - 2016-10-03
|
||||
### Added assertions
|
||||
- `Assert\Assertion::e164()` - The international public telecommunication numbering plan
|
||||
- `Assert\Assertion::interfaceExists()`
|
||||
- `Assert\Assertion::ip()` / `Assert\Assertion::ipv4()` / `Assert\Assertion::ipv6()`
|
||||
- `Assert\Assertion::keyNotExists()`
|
||||
- `Assert\Assertion::null()`
|
||||
- `Assert\Assertion::satisfy()` - Allows for a bespoke assertion, rather than a predefined one
|
||||
|
||||
### Fixes
|
||||
- Improved the reporting of the value for min and max assertions
|
||||
|
||||
### Other changes
|
||||
- Removed `composer.lock` file from library
|
||||
- Improved travis build to detect incorrect documentation changes
|
||||
|
||||
## 2.6.3 - 2016-07-28
|
||||
### Added assertions
|
||||
- `Assert\Assertion::notInArray()`
|
||||
|
||||
### Fixes
|
||||
- Made `Assert\Assertion::INVALID_GREATER_OR_EQUAL` unique
|
||||
|
||||
### Other changes
|
||||
- Introduced [CONTRIBUTING.md](https://github.com/beberlei/assert/blob/v2.6.3/CONTRIBUTING.md) to get contributors to generate the docblocks when a new assertion is added
|
||||
- Introduced [.editorconfig](https://github.com/beberlei/assert/blob/v2.6.3/.editorconfig) to allow IDEs that support EditorConfig to provide a consistent code style.
|
||||
See [EditorConfig](http://editorconfig.org/) for further details
|
||||
- Additional tests and updated documentation.
|
||||
- Travis updates.
|
||||
|
||||
## 2.6.2 - 2016-07-26
|
||||
### Fixes
|
||||
- Fixed unit test to work with PHP 5.3
|
||||
|
||||
## 2.6.1 - 2016-07-26
|
||||
### Fixes
|
||||
- Fixed `Assertion::isCallable()` to with with PHP 5.3
|
||||
|
||||
## 2.6 - 2016-07-26
|
||||
### Added assertions
|
||||
- `Assert\Assertion::isCallable()`
|
||||
|
||||
## 2.5.2 - 2016-07-26
|
||||
### Other changes
|
||||
- Updated tests
|
||||
- Updated `generate_method_docs.php` and regenerated all documentation
|
||||
- Added Richard Quadling as collaborator
|
||||
|
||||
## 2.5.1 - 2016-05-20
|
||||
### Other changes
|
||||
- Updated missing assertions from documentation
|
||||
|
||||
## 2.5 - 2016-03-22
|
||||
### Added assertions
|
||||
- `Assert\Assertion::date()`
|
||||
|
||||
### Other changes
|
||||
- Added appropriate guards to the additional assert functions to stop them from being defined twice
|
||||
|
||||
## 2.4 - 2015-08-21
|
||||
### Added assertions
|
||||
- `Assert\Assertion::lessThan()`
|
||||
- `Assert\Assertion::lessOrEqualThan()`
|
||||
- `Assert\Assertion::greaterThan()`
|
||||
- `Assert\Assertion::greaterOrEqualThan()`
|
||||
|
||||
### Other changes
|
||||
- Added support for PHP 5.6 and PHP 7.0 to Travis
|
||||
|
||||
## 2.3 - 2015-12-18
|
||||
### Added assertions
|
||||
- `Assert\Assertion::isTraversable()`
|
||||
- `Assert\Assertion::isArrayAccessible()`
|
||||
- `Assert\Assertion::keyIsset()`
|
||||
|
||||
## 2.2 - 2015-12-18
|
||||
### Other changes
|
||||
- Used parameterised `sprintf()` for messages
|
||||
|
||||
## 2.1 - 2015-11-06
|
||||
### Added assertions
|
||||
- `Assert\Assertion::notEq()`
|
||||
- `Assert\Assertion::notSame()`
|
||||
- `Assert\Assertion::scalar()`
|
||||
- `Assert\Assertion::choicesNotEmpty()`
|
||||
- `Assert\Assertion::methodExists()`
|
||||
- `Assert\Assertion::isObject()`
|
||||
|
||||
## 2.0.1 - 2014-01-26
|
||||
### Other changes
|
||||
- Pass constraints and values to `Assert\AssertionFailedException`
|
||||
|
||||
## 2.0 - 2014-01-26
|
||||
### Other changes
|
||||
- Introduce AssertionChaining and LazyAssertions
|
||||
- Introduce `Assert\Assertion::stringify()` to make a string version of a value
|
||||
|
||||
## 1.7 - 2014-01-25
|
||||
### Added assertions
|
||||
- `Assert\Assertion::float()`
|
||||
|
||||
### Other changes
|
||||
- Added support for HHVM to Travis
|
||||
|
||||
## 1.6 - 2013-11-05
|
||||
### Added assertions
|
||||
- `Assert\Assertion::count()`
|
||||
|
||||
### Other changes
|
||||
- Added support for PHP 5.5 to Travis
|
||||
|
||||
## 1.5 - 2013-10-01
|
||||
### Added assertions
|
||||
- `Assert\Assertion::notEmptyKey()`
|
||||
- `Assert\Assertion::all....()`
|
||||
|
||||
|
||||
## 1.4 - 2013-07-07
|
||||
### Added assertions
|
||||
- `Assert\Assertion::noContent()`
|
||||
- `Assert\Assertion::endsWith()`
|
||||
- `Assert\Assertion::notIsInstanceOf()`
|
||||
- `Assert\Assertion::isJsonString()`
|
||||
- `Assert\Assertion::uuid()`
|
||||
|
||||
### Other changes
|
||||
- Added BSD-2 License
|
||||
|
||||
## 1.3 - 2013-03-02
|
||||
### Added assertions
|
||||
- `Assert\Assertion::length()`
|
||||
- `Assert\Assertion::url()`
|
||||
- `Assert\Assertion::false()`
|
||||
- `Assert\Assertion::implementsInterface()`
|
||||
|
||||
### Other changes
|
||||
- Travis now runs PHP Unit tests
|
||||
- Added `Assert\InvalidArgumentException`
|
||||
- Added `$encoding = 'UTF-8'` parameter to appropriate assertions
|
||||
|
||||
## 1.2 - 2012-07-23
|
||||
### Added assertions
|
||||
- `Assert\Assertion::nullOr....()`
|
||||
|
||||
## 1.1 - 2012-07-23
|
||||
### Added assertions
|
||||
- `Assert\Assertion::eq()`
|
||||
- `Assert\Assertion::same()`
|
||||
- `Assert\Assertion::inArray()`
|
||||
- `Assert\Assertion::min()`
|
||||
- `Assert\Assertion::max()`
|
||||
- `Assert\Assertion::true()`
|
||||
- `Assert\Assertion::classExists()`
|
||||
|
||||
### Other changes
|
||||
- Added `$propertyPath = null` parameter to assertions
|
||||
|
||||
## 1.0 - 2012-05-20
|
||||
### Added assertions
|
||||
- `Assert\Assertion::integer()`
|
||||
- `Assert\Assertion::digit()`
|
||||
- `Assert\Assertion::integerish()`
|
||||
- `Assert\Assertion::boolean()`
|
||||
- `Assert\Assertion::notEmpty()`
|
||||
- `Assert\Assertion::string()`
|
||||
- `Assert\Assertion::regex()`
|
||||
- `Assert\Assertion::minLength()`
|
||||
- `Assert\Assertion::maxLength()`
|
||||
- `Assert\Assertion::betweenLength()`
|
||||
- `Assert\Assertion::startsWith()`
|
||||
- `Assert\Assertion::contains()`
|
||||
- `Assert\Assertion::choice()`
|
||||
- `Assert\Assertion::isArray()`
|
||||
- `Assert\Assertion::keyExists()`
|
||||
- `Assert\Assertion::notBlank()`
|
||||
- `Assert\Assertion::isInstanceOf()`
|
||||
- `Assert\Assertion::subclassOf()`
|
||||
- `Assert\Assertion::range()`
|
||||
- `Assert\Assertion::file()`
|
||||
- `Assert\Assertion::readable()`
|
||||
- `Assert\Assertion::writeable()`
|
||||
- `Assert\Assertion::email()`
|
||||
- `Assert\Assertion::alnum()`
|
|
@ -1,9 +0,0 @@
|
|||
# How to contribute
|
||||
|
||||
Thanks for contributing to assert! Just follow these single guidelines:
|
||||
|
||||
- You must use [feature / topic branches](https://git-scm.com/book/en/v2/Git-Branching-Branching-Workflows) to ease the merge of contributions.
|
||||
- Coding standard compliance must be ensured before committing or opening pull requests by running `composer assert:cs-fix` or `composer assert:cs-lint` in the root directory of this repository.
|
||||
- After adding new assertions regenerate the [README.md](README.md) and the docblocks by running `composer assert:generate-docs` on the command line.
|
||||
- After adding new non release relevant artifacts you must ensure they are export ignored in the [.gitattributes](.gitattributes) file.
|
||||
|
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to kontakt@beberlei.de so I can send you a copy immediately.
|
||||
*/
|
||||
|
||||
namespace ncc\Assert;
|
||||
|
||||
class InvalidArgumentException extends \InvalidArgumentException implements AssertionFailedException
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $propertyPath;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $constraints;
|
||||
|
||||
public function __construct($message, $code, string $propertyPath = null, $value = null, array $constraints = [])
|
||||
{
|
||||
parent::__construct($message, $code);
|
||||
|
||||
$this->propertyPath = $propertyPath;
|
||||
$this->value = $value;
|
||||
$this->constraints = $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* User controlled way to define a sub-property causing
|
||||
* the failure of a currently asserted objects.
|
||||
*
|
||||
* Useful to transport information about the nature of the error
|
||||
* back to higher layers.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPropertyPath()
|
||||
{
|
||||
return $this->propertyPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value that caused the assertion to fail.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the constraints that applied to the failed assertion.
|
||||
*/
|
||||
public function getConstraints(): array
|
||||
{
|
||||
return $this->constraints;
|
||||
}
|
||||
}
|
11
src/ncc/ThirdParty/beberlei/assert/LICENSE
vendored
11
src/ncc/ThirdParty/beberlei/assert/LICENSE
vendored
|
@ -1,11 +0,0 @@
|
|||
Copyright (c) 2011-2013, Benjamin Eberlei
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
- Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
228
src/ncc/ThirdParty/beberlei/assert/LazyAssertion.php
vendored
228
src/ncc/ThirdParty/beberlei/assert/LazyAssertion.php
vendored
|
@ -1,228 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to kontakt@beberlei.de so I can send you a copy immediately.
|
||||
*/
|
||||
|
||||
namespace ncc\Assert;
|
||||
|
||||
use LogicException;
|
||||
|
||||
/**
|
||||
* Chaining builder for lazy assertions.
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*
|
||||
* @method LazyAssertion alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric.
|
||||
* @method LazyAssertion base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
|
||||
* @method LazyAssertion between(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
|
||||
* @method LazyAssertion betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit.
|
||||
* @method LazyAssertion betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths.
|
||||
* @method LazyAssertion boolean(string|callable $message = null, string $propertyPath = null) Assert that value is php boolean.
|
||||
* @method LazyAssertion choice(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices.
|
||||
* @method LazyAssertion choicesNotEmpty(array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content.
|
||||
* @method LazyAssertion classExists(string|callable $message = null, string $propertyPath = null) Assert that the class exists.
|
||||
* @method LazyAssertion contains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars.
|
||||
* @method LazyAssertion count(int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count.
|
||||
* @method LazyAssertion date(string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format.
|
||||
* @method LazyAssertion defined(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined.
|
||||
* @method LazyAssertion digit(string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit.
|
||||
* @method LazyAssertion directory(string|callable $message = null, string $propertyPath = null) Assert that a directory exists.
|
||||
* @method LazyAssertion e164(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number.
|
||||
* @method LazyAssertion email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
|
||||
* @method LazyAssertion endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
|
||||
* @method LazyAssertion eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==).
|
||||
* @method LazyAssertion eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset.
|
||||
* @method LazyAssertion extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
|
||||
* @method LazyAssertion extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed.
|
||||
* @method LazyAssertion false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.
|
||||
* @method LazyAssertion file(string|callable $message = null, string $propertyPath = null) Assert that a file exists.
|
||||
* @method LazyAssertion float(string|callable $message = null, string $propertyPath = null) Assert that value is a php float.
|
||||
* @method LazyAssertion greaterOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit.
|
||||
* @method LazyAssertion greaterThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit.
|
||||
* @method LazyAssertion implementsInterface(string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface.
|
||||
* @method LazyAssertion inArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice().
|
||||
* @method LazyAssertion integer(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer.
|
||||
* @method LazyAssertion integerish(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish.
|
||||
* @method LazyAssertion interfaceExists(string|callable $message = null, string $propertyPath = null) Assert that the interface exists.
|
||||
* @method LazyAssertion ip(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address.
|
||||
* @method LazyAssertion ipv4(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address.
|
||||
* @method LazyAssertion ipv6(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address.
|
||||
* @method LazyAssertion isArray(string|callable $message = null, string $propertyPath = null) Assert that value is an array.
|
||||
* @method LazyAssertion isArrayAccessible(string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object.
|
||||
* @method LazyAssertion isCallable(string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable.
|
||||
* @method LazyAssertion isCountable(string|callable $message = null, string $propertyPath = null) Assert that value is countable.
|
||||
* @method LazyAssertion isInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name.
|
||||
* @method LazyAssertion isJsonString(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string.
|
||||
* @method LazyAssertion isObject(string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object.
|
||||
* @method LazyAssertion isResource(string|callable $message = null, string $propertyPath = null) Assert that value is a resource.
|
||||
* @method LazyAssertion isTraversable(string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object.
|
||||
* @method LazyAssertion keyExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array.
|
||||
* @method LazyAssertion keyIsset(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset().
|
||||
* @method LazyAssertion keyNotExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array.
|
||||
* @method LazyAssertion length(int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length.
|
||||
* @method LazyAssertion lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit.
|
||||
* @method LazyAssertion lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit.
|
||||
* @method LazyAssertion max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit.
|
||||
* @method LazyAssertion maxCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements.
|
||||
* @method LazyAssertion maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars.
|
||||
* @method LazyAssertion methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object.
|
||||
* @method LazyAssertion min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit.
|
||||
* @method LazyAssertion minCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements.
|
||||
* @method LazyAssertion minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long.
|
||||
* @method LazyAssertion noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty.
|
||||
* @method LazyAssertion notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank.
|
||||
* @method LazyAssertion notContains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars.
|
||||
* @method LazyAssertion notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty.
|
||||
* @method LazyAssertion notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty.
|
||||
* @method LazyAssertion notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==).
|
||||
* @method LazyAssertion notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices.
|
||||
* @method LazyAssertion notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name.
|
||||
* @method LazyAssertion notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null.
|
||||
* @method LazyAssertion notRegex(string $pattern, string|callable $message = null, string $propertyPath = null) # Assert that value does not match a regex.
|
||||
* @method LazyAssertion notSame(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===).
|
||||
* @method LazyAssertion null(string|callable $message = null, string $propertyPath = null) Assert that value is null.
|
||||
* @method LazyAssertion numeric(string|callable $message = null, string $propertyPath = null) Assert that value is numeric.
|
||||
* @method LazyAssertion objectOrClass(string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists.
|
||||
* @method LazyAssertion phpVersion(mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version.
|
||||
* @method LazyAssertion propertiesExist(array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist.
|
||||
* @method LazyAssertion propertyExists(string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists.
|
||||
* @method LazyAssertion range(mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers.
|
||||
* @method LazyAssertion readable(string|callable $message = null, string $propertyPath = null) Assert that the value is something readable.
|
||||
* @method LazyAssertion regex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex.
|
||||
* @method LazyAssertion same(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===).
|
||||
* @method LazyAssertion satisfy(callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback.
|
||||
* @method LazyAssertion scalar(string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar.
|
||||
* @method LazyAssertion startsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars.
|
||||
* @method LazyAssertion string(string|callable $message = null, string $propertyPath = null) Assert that value is a string.
|
||||
* @method LazyAssertion subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name.
|
||||
* @method LazyAssertion true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True.
|
||||
* @method LazyAssertion uniqueValues(string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality).
|
||||
* @method LazyAssertion url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL.
|
||||
* @method LazyAssertion uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID.
|
||||
* @method LazyAssertion version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions.
|
||||
* @method LazyAssertion writeable(string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable.
|
||||
* @method LazyAssertion all() Switch chain into validation mode for an array of values.
|
||||
* @method LazyAssertion nullOr() Switch chain into mode allowing nulls, ignoring further assertions.
|
||||
*/
|
||||
class LazyAssertion
|
||||
{
|
||||
private $currentChainFailed = false;
|
||||
private $alwaysTryAll = false;
|
||||
private $thisChainTryAll = false;
|
||||
private $currentChain;
|
||||
private $errors = [];
|
||||
|
||||
/** @var string The class to use as AssertionChain factory */
|
||||
private $assertClass = Assert::class;
|
||||
|
||||
/** @var string|LazyAssertionException The class to use for exceptions */
|
||||
private $exceptionClass = LazyAssertionException::class;
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param string|callable|null $defaultMessage
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function that($value, string $propertyPath = null, $defaultMessage = null)
|
||||
{
|
||||
$this->currentChainFailed = false;
|
||||
$this->thisChainTryAll = false;
|
||||
$assertClass = $this->assertClass;
|
||||
$this->currentChain = $assertClass::that($value, $defaultMessage, $propertyPath);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return static
|
||||
*/
|
||||
public function tryAll()
|
||||
{
|
||||
if (!$this->currentChain) {
|
||||
$this->alwaysTryAll = true;
|
||||
}
|
||||
|
||||
$this->thisChainTryAll = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function __call($method, $args)
|
||||
{
|
||||
if (false === $this->alwaysTryAll
|
||||
&& false === $this->thisChainTryAll
|
||||
&& true === $this->currentChainFailed
|
||||
) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
try {
|
||||
\call_user_func_array([$this->currentChain, $method], $args);
|
||||
} catch (AssertionFailedException $e) {
|
||||
$this->errors[] = $e;
|
||||
$this->currentChainFailed = true;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LazyAssertionException
|
||||
*/
|
||||
public function verifyNow(): bool
|
||||
{
|
||||
if ($this->errors) {
|
||||
throw \call_user_func([$this->exceptionClass, 'fromErrors'], $this->errors);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setAssertClass(string $className): LazyAssertion
|
||||
{
|
||||
if (Assert::class !== $className && !\is_subclass_of($className, Assert::class)) {
|
||||
throw new LogicException($className.' is not (a subclass of) '.Assert::class);
|
||||
}
|
||||
|
||||
$this->assertClass = $className;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setExceptionClass(string $className): LazyAssertion
|
||||
{
|
||||
if (LazyAssertionException::class !== $className && !\is_subclass_of($className, LazyAssertionException::class)) {
|
||||
throw new LogicException($className.' is not (a subclass of) '.LazyAssertionException::class);
|
||||
}
|
||||
|
||||
$this->exceptionClass = $className;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to kontakt@beberlei.de so I can send you a copy immediately.
|
||||
*/
|
||||
|
||||
namespace ncc\Assert;
|
||||
|
||||
class LazyAssertionException extends InvalidArgumentException
|
||||
{
|
||||
/**
|
||||
* @var InvalidArgumentException[]
|
||||
*/
|
||||
private $errors = [];
|
||||
|
||||
/**
|
||||
* @param InvalidArgumentException[] $errors
|
||||
*/
|
||||
public static function fromErrors(array $errors): self
|
||||
{
|
||||
$message = \sprintf('The following %d assertions failed:', \count($errors))."\n";
|
||||
|
||||
$i = 1;
|
||||
foreach ($errors as $error) {
|
||||
$message .= \sprintf("%d) %s: %s\n", $i++, $error->getPropertyPath(), $error->getMessage());
|
||||
}
|
||||
|
||||
return new static($message, $errors);
|
||||
}
|
||||
|
||||
public function __construct($message, array $errors)
|
||||
{
|
||||
parent::__construct($message, 0, null, null);
|
||||
|
||||
$this->errors = $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return InvalidArgumentException[]
|
||||
*/
|
||||
public function getErrorExceptions(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
}
|
346
src/ncc/ThirdParty/beberlei/assert/README.md
vendored
346
src/ncc/ThirdParty/beberlei/assert/README.md
vendored
|
@ -1,346 +0,0 @@
|
|||
# Assert
|
||||
|
||||
[](https://travis-ci.org/beberlei/assert)
|
||||
[](https://scrutinizer-ci.com/g/beberlei/assert/)
|
||||
[](https://github.com/beberlei/assert/issues)
|
||||
|
||||
[](https://github.com/beberlei/assert)
|
||||
[](https://packagist.org/packages/beberlei/assert)
|
||||
|
||||
[](https://packagist.org/packages/beberlei/assert)
|
||||
[](https://packagist.org/packages/beberlei/assert)
|
||||
[](https://packagist.org/packages/beberlei/assert)
|
||||
|
||||
A simple php library which contains assertions and guard methods for input validation (not filtering!) in business-model, libraries and application low-level code.
|
||||
The library can be used to implement pre-/post-conditions on input data.
|
||||
|
||||
Idea is to reduce the amount of code for implementing assertions in your model and also simplify the code paths to implement assertions. When assertions fail, an exception is thrown, removing the
|
||||
necessity for if-clauses in your code.
|
||||
|
||||
The library is not using Symfony or Zend Validators for a reason: The checks have to be low-level, fast, non-object-oriented code to be used everywhere necessary. Using any of the two libraries
|
||||
requires instantiation of several objects, using a locale component, translations, you name it. Its too much bloat.
|
||||
|
||||
## Installation
|
||||
|
||||
Using Composer:
|
||||
|
||||
```sh
|
||||
composer require beberlei/assert
|
||||
```
|
||||
|
||||
## Example usages
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Assert\Assertion;
|
||||
|
||||
function duplicateFile($file, $times)
|
||||
{
|
||||
Assertion::file($file);
|
||||
Assertion::digit($times);
|
||||
|
||||
for ($i = 0; $i < $times; $i++) {
|
||||
copy($file, $file . $i);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Real time usage with [Azure Blob Storage](https://github.com/beberlei/azure-blob-storage/blob/master/lib/Beberlei/AzureBlobStorage/BlobClient.php#L571):
|
||||
|
||||
```php
|
||||
<?php
|
||||
public function putBlob($containerName = '', $blobName = '', $localFileName = '', $metadata = array(), $leaseId = null, $additionalHeaders = array())
|
||||
{
|
||||
Assertion::notEmpty($containerName, 'Container name is not specified');
|
||||
self::assertValidContainerName($containerName);
|
||||
Assertion::notEmpty($blobName, 'Blob name is not specified.');
|
||||
Assertion::notEmpty($localFileName, 'Local file name is not specified.');
|
||||
Assertion::file($localFileName, 'Local file name is not specified.');
|
||||
self::assertValidRootContainerBlobName($containerName, $blobName);
|
||||
|
||||
// Check file size
|
||||
if (filesize($localFileName) >= self::MAX_BLOB_SIZE) {
|
||||
return $this->putLargeBlob($containerName, $blobName, $localFileName, $metadata, $leaseId, $additionalHeaders);
|
||||
}
|
||||
|
||||
// Put the data to Windows Azure Storage
|
||||
return $this->putBlobData($containerName, $blobName, file_get_contents($localFileName), $metadata, $leaseId, $additionalHeaders);
|
||||
}
|
||||
```
|
||||
|
||||
### NullOr helper
|
||||
|
||||
A helper method (`Assertion::nullOr*`) is provided to check if a value is null OR holds for the assertion:
|
||||
|
||||
```php
|
||||
<?php
|
||||
Assertion::nullOrMax(null, 42); // success
|
||||
Assertion::nullOrMax(1, 42); // success
|
||||
Assertion::nullOrMax(1337, 42); // exception
|
||||
```
|
||||
|
||||
### All helper
|
||||
|
||||
The `Assertion::all*` method checks if all provided values hold for the
|
||||
assertion. It will throw an exception of the assertion does not hold for one of
|
||||
the values:
|
||||
|
||||
```php
|
||||
<?php
|
||||
Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'stdClass'); // success
|
||||
Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'PDO'); // exception
|
||||
```
|
||||
|
||||
### Assert::that() Chaining
|
||||
|
||||
Using the static API on values is very verbose when checking values against multiple assertions.
|
||||
Starting with 2.6.7 of Assert the `Assert` class provides a much nicer fluent API for assertions, starting
|
||||
with `Assert::that($value)` and then receiving the assertions you want to call
|
||||
on the fluent interface. You only have to specify the `$value` once.
|
||||
|
||||
```php
|
||||
<?php
|
||||
Assert::that($value)->notEmpty()->integer();
|
||||
Assert::that($value)->nullOr()->string()->startsWith("Foo");
|
||||
Assert::that($values)->all()->float();
|
||||
```
|
||||
|
||||
There are also two shortcut function `Assert::thatNullOr()` and `Assert::thatAll()` enabling
|
||||
the "nullOr" or "all" helper respectively.
|
||||
|
||||
### Lazy Assertions
|
||||
|
||||
There are many cases in web development, especially when involving forms, you want to collect several errors
|
||||
instead of aborting directly on the first error. This is what lazy assertions are for. Their API
|
||||
works exactly like the fluent ``Assert::that()`` API, but instead of throwing an Exception directly,
|
||||
they collect all errors and only trigger the exception when the method
|
||||
``verifyNow()`` is called on the ``Assert\SoftAssertion`` object.
|
||||
|
||||
```php
|
||||
<?php
|
||||
Assert::lazy()
|
||||
->that(10, 'foo')->string()
|
||||
->that(null, 'bar')->notEmpty()
|
||||
->that('string', 'baz')->isArray()
|
||||
->verifyNow();
|
||||
```
|
||||
|
||||
The method ``that($value, $propertyPath)`` requires a property path (name), so that you know how to differentiate
|
||||
the errors afterwards.
|
||||
|
||||
On failure ``verifyNow()`` will throw an exception
|
||||
``Assert\\LazyAssertionException`` with a combined message:
|
||||
|
||||
The following 3 assertions failed:
|
||||
1) foo: Value "10" expected to be string, type integer given.
|
||||
2) bar: Value "<NULL>" is empty, but non empty value was expected.
|
||||
3) baz: Value "string" is not an array.
|
||||
|
||||
You can also retrieve all the ``AssertionFailedException``s by calling ``getErrorExceptions()``.
|
||||
This can be useful for example to build a failure response for the user.
|
||||
|
||||
For those looking to capture multiple errors on a single value when using a lazy assertion chain,
|
||||
you may follow your call to ``that`` with ``tryAll`` to run all assertions against the value, and
|
||||
capture all of the resulting failed assertion error messages. Here's an example:
|
||||
|
||||
```php
|
||||
Assert::lazy()
|
||||
->that(10, 'foo')->tryAll()->integer()->between(5, 15)
|
||||
->that(null, 'foo')->tryAll()->notEmpty()->string()
|
||||
->verifyNow();
|
||||
```
|
||||
|
||||
The above shows how to use this functionality to finely tune the behavior of reporting failures, but to make
|
||||
catching all failures even easier, you may also call ``tryAll`` before making any assertions like below. This
|
||||
helps to reduce method calls, and has the same behavior as above.
|
||||
|
||||
```php
|
||||
Assert::lazy()->tryAll()
|
||||
->that(10, 'foo')->integer()->between(5, 15)
|
||||
->that(null, 'foo')->notEmpty()->string()
|
||||
->verifyNow();
|
||||
```
|
||||
|
||||
### Functional Constructors
|
||||
|
||||
The following functions exist as aliases to `Assert` static constructors:
|
||||
|
||||
- `Assert\that()`
|
||||
- `Assert\thatAll()`
|
||||
- `Assert\thatNullOr()`
|
||||
- `Assert\lazy()`
|
||||
|
||||
Using the functional or static constructors is entirely personal preference.
|
||||
|
||||
**Note:** The functional constructors will not work with an [`Assertion` extension](#your-own-assertion-class).
|
||||
However it is trivial to recreate these functions in a way that point to the extended class.
|
||||
|
||||
## List of assertions
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Assert\Assertion;
|
||||
|
||||
Assertion::alnum(mixed $value);
|
||||
Assertion::base64(string $value);
|
||||
Assertion::between(mixed $value, mixed $lowerLimit, mixed $upperLimit);
|
||||
Assertion::betweenExclusive(mixed $value, mixed $lowerLimit, mixed $upperLimit);
|
||||
Assertion::betweenLength(mixed $value, int $minLength, int $maxLength);
|
||||
Assertion::boolean(mixed $value);
|
||||
Assertion::choice(mixed $value, array $choices);
|
||||
Assertion::choicesNotEmpty(array $values, array $choices);
|
||||
Assertion::classExists(mixed $value);
|
||||
Assertion::contains(mixed $string, string $needle);
|
||||
Assertion::count(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
|
||||
Assertion::date(string $value, string $format);
|
||||
Assertion::defined(mixed $constant);
|
||||
Assertion::digit(mixed $value);
|
||||
Assertion::directory(string $value);
|
||||
Assertion::e164(string $value);
|
||||
Assertion::email(mixed $value);
|
||||
Assertion::endsWith(mixed $string, string $needle);
|
||||
Assertion::eq(mixed $value, mixed $value2);
|
||||
Assertion::eqArraySubset(mixed $value, mixed $value2);
|
||||
Assertion::extensionLoaded(mixed $value);
|
||||
Assertion::extensionVersion(string $extension, string $operator, mixed $version);
|
||||
Assertion::false(mixed $value);
|
||||
Assertion::file(string $value);
|
||||
Assertion::float(mixed $value);
|
||||
Assertion::greaterOrEqualThan(mixed $value, mixed $limit);
|
||||
Assertion::greaterThan(mixed $value, mixed $limit);
|
||||
Assertion::implementsInterface(mixed $class, string $interfaceName);
|
||||
Assertion::inArray(mixed $value, array $choices);
|
||||
Assertion::integer(mixed $value);
|
||||
Assertion::integerish(mixed $value);
|
||||
Assertion::interfaceExists(mixed $value);
|
||||
Assertion::ip(string $value, int $flag = null);
|
||||
Assertion::ipv4(string $value, int $flag = null);
|
||||
Assertion::ipv6(string $value, int $flag = null);
|
||||
Assertion::isArray(mixed $value);
|
||||
Assertion::isArrayAccessible(mixed $value);
|
||||
Assertion::isCallable(mixed $value);
|
||||
Assertion::isCountable(array|Countable|ResourceBundle|SimpleXMLElement $value);
|
||||
Assertion::isInstanceOf(mixed $value, string $className);
|
||||
Assertion::isJsonString(mixed $value);
|
||||
Assertion::isObject(mixed $value);
|
||||
Assertion::isResource(mixed $value);
|
||||
Assertion::isTraversable(mixed $value);
|
||||
Assertion::keyExists(mixed $value, string|int $key);
|
||||
Assertion::keyIsset(mixed $value, string|int $key);
|
||||
Assertion::keyNotExists(mixed $value, string|int $key);
|
||||
Assertion::length(mixed $value, int $length);
|
||||
Assertion::lessOrEqualThan(mixed $value, mixed $limit);
|
||||
Assertion::lessThan(mixed $value, mixed $limit);
|
||||
Assertion::max(mixed $value, mixed $maxValue);
|
||||
Assertion::maxCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
|
||||
Assertion::maxLength(mixed $value, int $maxLength);
|
||||
Assertion::methodExists(string $value, mixed $object);
|
||||
Assertion::min(mixed $value, mixed $minValue);
|
||||
Assertion::minCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count);
|
||||
Assertion::minLength(mixed $value, int $minLength);
|
||||
Assertion::noContent(mixed $value);
|
||||
Assertion::notBlank(mixed $value);
|
||||
Assertion::notContains(mixed $string, string $needle);
|
||||
Assertion::notEmpty(mixed $value);
|
||||
Assertion::notEmptyKey(mixed $value, string|int $key);
|
||||
Assertion::notEq(mixed $value1, mixed $value2);
|
||||
Assertion::notInArray(mixed $value, array $choices);
|
||||
Assertion::notIsInstanceOf(mixed $value, string $className);
|
||||
Assertion::notNull(mixed $value);
|
||||
Assertion::notRegex(mixed $value, string $pattern);
|
||||
Assertion::notSame(mixed $value1, mixed $value2);
|
||||
Assertion::null(mixed $value);
|
||||
Assertion::numeric(mixed $value);
|
||||
Assertion::objectOrClass(mixed $value);
|
||||
Assertion::phpVersion(string $operator, mixed $version);
|
||||
Assertion::propertiesExist(mixed $value, array $properties);
|
||||
Assertion::propertyExists(mixed $value, string $property);
|
||||
Assertion::range(mixed $value, mixed $minValue, mixed $maxValue);
|
||||
Assertion::readable(string $value);
|
||||
Assertion::regex(mixed $value, string $pattern);
|
||||
Assertion::same(mixed $value, mixed $value2);
|
||||
Assertion::satisfy(mixed $value, callable $callback);
|
||||
Assertion::scalar(mixed $value);
|
||||
Assertion::startsWith(mixed $string, string $needle);
|
||||
Assertion::string(mixed $value);
|
||||
Assertion::subclassOf(mixed $value, string $className);
|
||||
Assertion::true(mixed $value);
|
||||
Assertion::uniqueValues(array $values);
|
||||
Assertion::url(mixed $value);
|
||||
Assertion::uuid(string $value);
|
||||
Assertion::version(string $version1, string $operator, string $version2);
|
||||
Assertion::writeable(string $value);
|
||||
|
||||
```
|
||||
|
||||
Remember: When a configuration parameter is necessary, it is always passed AFTER the value. The value is always the first parameter.
|
||||
|
||||
## Exception & Error Handling
|
||||
|
||||
If any of the assertions fails a `Assert\AssertionFailedException` is thrown.
|
||||
You can pass an argument called ```$message``` to any assertion to control the
|
||||
exception message. Every exception contains a default message and unique message code
|
||||
by default.
|
||||
|
||||
```php
|
||||
<?php
|
||||
use Assert\Assertion;
|
||||
use Assert\AssertionFailedException;
|
||||
|
||||
try {
|
||||
Assertion::integer($value, "The pressure of gas is measured in integers.");
|
||||
} catch(AssertionFailedException $e) {
|
||||
// error handling
|
||||
$e->getValue(); // the value that caused the failure
|
||||
$e->getConstraints(); // the additional constraints of the assertion.
|
||||
}
|
||||
```
|
||||
|
||||
``Assert\AssertionFailedException`` is just an interface and the default
|
||||
implementation is ``Assert\InvalidArgumentException`` which extends the SPL
|
||||
``InvalidArgumentException``. You can change the exception being used on a
|
||||
package based level.
|
||||
|
||||
### Customised exception messages
|
||||
|
||||
You can pass a callback as the message parameter, allowing you to construct your own
|
||||
message only if an assertion fails, rather than every time you run the test.
|
||||
|
||||
The callback will be supplied with an array of parameters that are for the assertion.
|
||||
|
||||
As some assertions call other assertions, your callback will need to example the array
|
||||
to determine what assertion failed.
|
||||
|
||||
The array will contain a key called `::assertion` that indicates which assertion
|
||||
failed.
|
||||
|
||||
The callback should return the string that will be used as the exception
|
||||
message.
|
||||
|
||||
## Your own Assertion class
|
||||
|
||||
To shield your library from possible bugs, misinterpretations or BC breaks
|
||||
inside Assert you should introduce a library/project based assertion subclass,
|
||||
where you can override the exception thrown as well.
|
||||
|
||||
In addition, you can override the ``Assert\Assertion::stringify()`` method to
|
||||
provide your own interpretations of the types during error handling.
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace MyProject;
|
||||
|
||||
use Assert\Assertion as BaseAssertion;
|
||||
|
||||
class Assertion extends BaseAssertion
|
||||
{
|
||||
protected static $exceptionClass = 'MyProject\AssertionFailedException';
|
||||
}
|
||||
```
|
||||
|
||||
As of V2.9.2, [Lazy Assertions](#lazy-assertions) now have access to any additional
|
||||
assertions present in your own assertion classes.
|
||||
|
||||
## Contributing
|
||||
Please see [CONTRIBUTING](CONTRIBUTING.md) for more details.
|
||||
|
10
src/ncc/ThirdParty/beberlei/assert/TODO.md
vendored
10
src/ncc/ThirdParty/beberlei/assert/TODO.md
vendored
|
@ -1,10 +0,0 @@
|
|||
# TODO
|
||||
|
||||
- Refactor unit tests into sets of related assertions.
|
||||
- Refactor all unit tests to use the new recommend exception testing pattern as the current `setExpectedException()` method is deprecated.
|
||||
- Separate assertions into sets that deal with related themes:
|
||||
- Variable type (isInt, isString isBoolean, isArray, etc.)
|
||||
- Variable content (min, max, between, etc.)
|
||||
- Scalar structures (keyExists, keyIsSet etc),
|
||||
- Class/interface definition (classExists, subClassOf, etc).
|
||||
- Class/interface content (methodExists, propertyExists, propertyIsSettable, etc.)
|
72
src/ncc/ThirdParty/beberlei/assert/functions.php
vendored
72
src/ncc/ThirdParty/beberlei/assert/functions.php
vendored
|
@ -1,72 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Assert
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to kontakt@beberlei.de so I can send you a copy immediately.
|
||||
*/
|
||||
|
||||
namespace ncc\Assert;
|
||||
|
||||
/**
|
||||
* Start validation on a value, returns {@link AssertionChain}.
|
||||
*
|
||||
* The invocation of this method starts an assertion chain
|
||||
* that is happening on the passed value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param string|callable|null $defaultMessage
|
||||
* @param string $defaultPropertyPath
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* \Assert\that($value)->notEmpty()->integer();
|
||||
* \Assert\that($value)->nullOr()->string()->startsWith("Foo");
|
||||
*
|
||||
* The assertion chain can be stateful, that means be careful when you reuse
|
||||
* it. You should never pass around the chain.
|
||||
*/
|
||||
function that($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
|
||||
{
|
||||
return Assert::that($value, $defaultMessage, $defaultPropertyPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start validation on a set of values, returns {@link AssertionChain}.
|
||||
*
|
||||
* @param mixed $values
|
||||
* @param string|callable|null $defaultMessage
|
||||
* @param string $defaultPropertyPath
|
||||
*/
|
||||
function thatAll($values, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
|
||||
{
|
||||
return Assert::thatAll($values, $defaultMessage, $defaultPropertyPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start validation and allow NULL, returns {@link AssertionChain}.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param string|callable|null $defaultMessage
|
||||
* @param string $defaultPropertyPath
|
||||
*
|
||||
* @deprecated In favour of Assert::thatNullOr($value, $defaultMessage = null, $defaultPropertyPath = null)
|
||||
*/
|
||||
function thatNullOr($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain
|
||||
{
|
||||
return Assert::thatNullOr($value, $defaultMessage, $defaultPropertyPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a lazy assertion object.
|
||||
*/
|
||||
function lazy(): LazyAssertion
|
||||
{
|
||||
return Assert::lazy();
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Action;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class ExitAction
|
||||
{
|
||||
public function __invoke(CliMenu $menu) : void
|
||||
{
|
||||
$menu->close();
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Action;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class GoBackAction
|
||||
{
|
||||
public function __invoke(CliMenu $menu) : void
|
||||
{
|
||||
if ($parent = $menu->getParent()) {
|
||||
$menu->closeThis();
|
||||
$parent->open();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,659 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Builder;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\Action\ExitAction;
|
||||
use ncc\PhpSchool\CliMenu\Action\GoBackAction;
|
||||
use ncc\PhpSchool\CliMenu\Exception\InvalidShortcutException;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\AsciiArtItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\CheckboxItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\LineBreakItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuMenuItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\RadioItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\SelectableItem;
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\SplitItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\StaticItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\CheckboxStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\DefaultStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\RadioStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\SelectableStyle;
|
||||
use ncc\PhpSchool\CliMenu\Terminal\TerminalFactory;
|
||||
use ncc\PhpSchool\Terminal\Terminal;
|
||||
use function ncc\PhpSchool\CliMenu\Util\each;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
* @author Aydin Hassan <aydin@hotmail.com>
|
||||
*/
|
||||
class CliMenuBuilder
|
||||
{
|
||||
/**
|
||||
* @var CliMenu
|
||||
*/
|
||||
private $menu;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $goBackButtonText = 'Go Back';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $exitButtonText = 'Exit';
|
||||
|
||||
/**
|
||||
* @var MenuStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
/**
|
||||
* @var Terminal
|
||||
*/
|
||||
private $terminal;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $disableDefaultItems = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $disabled = false;
|
||||
|
||||
/**
|
||||
* Whether or not to auto create keyboard shortcuts for items
|
||||
* when they contain square brackets. Eg: [M]y item
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $autoShortcuts = false;
|
||||
|
||||
/**
|
||||
* Regex to auto match for shortcuts defaults to looking
|
||||
* for a single character encased in square brackets
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $autoShortcutsRegex = '/\[(.)\]/';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $extraItemStyles = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $subMenu = false;
|
||||
|
||||
public function __construct(Terminal $terminal = null)
|
||||
{
|
||||
$this->terminal = $terminal ?? TerminalFactory::fromSystem();
|
||||
$this->style = new MenuStyle($this->terminal);
|
||||
$this->menu = new CliMenu(null, [], $this->terminal, $this->style);
|
||||
}
|
||||
|
||||
public static function newSubMenu(Terminal $terminal) : self
|
||||
{
|
||||
$instance = new self($terminal);
|
||||
$instance->subMenu = true;
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function setTitle(string $title) : self
|
||||
{
|
||||
$this->menu->setTitle($title);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addMenuItem(MenuItemInterface $item) : self
|
||||
{
|
||||
$this->menu->addItem($item);
|
||||
|
||||
$this->processItemShortcut($item);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addItem(
|
||||
string $text,
|
||||
callable $itemCallable,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) : self {
|
||||
$this->addMenuItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addItems(array $items) : self
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->addItem(...$item);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCheckboxItem(
|
||||
string $text,
|
||||
callable $itemCallable,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) : self {
|
||||
$this->addMenuItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCheckboxItems(array $items): self
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->addCheckboxItem(...$item);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRadioItem(
|
||||
string $text,
|
||||
callable $itemCallable,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) : self {
|
||||
$this->addMenuItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRadioItems(array $items): self
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->addRadioItem(...$item);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addStaticItem(string $text) : self
|
||||
{
|
||||
$this->addMenuItem(new StaticItem($text));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
|
||||
{
|
||||
$this->addMenuItem(new LineBreakItem($breakChar, $lines));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self
|
||||
{
|
||||
$this->addMenuItem(new AsciiArtItem($art, $position, $alt));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSubMenu(string $text, \Closure $callback) : self
|
||||
{
|
||||
$builder = self::newSubMenu($this->terminal);
|
||||
|
||||
if ($this->autoShortcuts) {
|
||||
$builder->enableAutoShortcuts($this->autoShortcutsRegex);
|
||||
}
|
||||
|
||||
each($this->extraItemStyles, function (int $i, array $extraItemStyle) use ($builder) {
|
||||
$builder->registerItemStyle($extraItemStyle['class'], $extraItemStyle['style']);
|
||||
});
|
||||
|
||||
$callback($builder);
|
||||
|
||||
$menu = $builder->build();
|
||||
$menu->setParent($this->menu);
|
||||
|
||||
$this->menu->addItem($item = new MenuMenuItem(
|
||||
$text,
|
||||
$menu,
|
||||
$builder->isMenuDisabled()
|
||||
));
|
||||
|
||||
$this->processItemShortcut($item);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : self
|
||||
{
|
||||
$menu = $builder->build();
|
||||
$menu->setParent($this->menu);
|
||||
|
||||
$this->menu->addItem($item = new MenuMenuItem(
|
||||
$text,
|
||||
$menu,
|
||||
$builder->isMenuDisabled()
|
||||
));
|
||||
|
||||
$this->processItemShortcut($item);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function enableAutoShortcuts(string $regex = null) : self
|
||||
{
|
||||
$this->autoShortcuts = true;
|
||||
|
||||
if (null !== $regex) {
|
||||
$this->autoShortcutsRegex = $regex;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function extractShortcut(string $title) : ?string
|
||||
{
|
||||
preg_match($this->autoShortcutsRegex, $title, $match);
|
||||
|
||||
if (!isset($match[1])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mb_strlen($match[1]) > 1) {
|
||||
throw InvalidShortcutException::fromShortcut($match[1]);
|
||||
}
|
||||
|
||||
return isset($match[1]) ? strtolower($match[1]) : null;
|
||||
}
|
||||
|
||||
private function processItemShortcut(MenuItemInterface $item) : void
|
||||
{
|
||||
$this->processIndividualShortcut($item, function (CliMenu $menu) use ($item) {
|
||||
$menu->executeAsSelected($item);
|
||||
});
|
||||
}
|
||||
|
||||
private function processSplitItemShortcuts(SplitItem $splitItem) : void
|
||||
{
|
||||
foreach ($splitItem->getItems() as $item) {
|
||||
$this->processIndividualShortcut($item, function (CliMenu $menu) use ($splitItem, $item) {
|
||||
$current = $splitItem->getSelectedItemIndex();
|
||||
|
||||
$splitItem->setSelectedItemIndex(
|
||||
(int) array_search($item, $splitItem->getItems(), true)
|
||||
);
|
||||
|
||||
$menu->executeAsSelected($splitItem);
|
||||
|
||||
if ($current !== null) {
|
||||
$splitItem->setSelectedItemIndex($current);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private function processIndividualShortcut(MenuItemInterface $item, callable $callback) : void
|
||||
{
|
||||
if (!$this->autoShortcuts) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($shortcut = $this->extractShortcut($item->getText())) {
|
||||
$this->menu->addCustomControlMapping(
|
||||
$shortcut,
|
||||
$callback
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function addSplitItem(\Closure $callback) : self
|
||||
{
|
||||
$builder = new SplitItemBuilder($this->menu);
|
||||
|
||||
if ($this->autoShortcuts) {
|
||||
$builder->enableAutoShortcuts($this->autoShortcutsRegex);
|
||||
}
|
||||
|
||||
each($this->extraItemStyles, function (int $i, array $extraItemStyle) use ($builder) {
|
||||
$builder->registerItemStyle($extraItemStyle['class'], $extraItemStyle['style']);
|
||||
});
|
||||
|
||||
$callback($builder);
|
||||
|
||||
$this->menu->addItem($splitItem = $builder->build());
|
||||
|
||||
$this->processSplitItemShortcuts($splitItem);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable a submenu
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function disableMenu() : self
|
||||
{
|
||||
if (!$this->subMenu) {
|
||||
throw new \InvalidArgumentException(
|
||||
'You can\'t disable the root menu'
|
||||
);
|
||||
}
|
||||
|
||||
$this->disabled = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isMenuDisabled() : bool
|
||||
{
|
||||
return $this->disabled;
|
||||
}
|
||||
|
||||
public function setGoBackButtonText(string $goBackButtonTest) : self
|
||||
{
|
||||
$this->goBackButtonText = $goBackButtonTest;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setExitButtonText(string $exitButtonText) : self
|
||||
{
|
||||
$this->exitButtonText = $exitButtonText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBackgroundColour(string $colour, string $fallback = null) : self
|
||||
{
|
||||
$this->style->setBg($colour, $fallback);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setForegroundColour(string $colour, string $fallback = null) : self
|
||||
{
|
||||
$this->style->setFg($colour, $fallback);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setWidth(int $width) : self
|
||||
{
|
||||
$this->style->setWidth($width);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPadding(int $topBottom, int $leftRight = null) : self
|
||||
{
|
||||
$this->style->setPadding($topBottom, $leftRight);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPaddingTopBottom(int $topBottom) : self
|
||||
{
|
||||
$this->style->setPaddingTopBottom($topBottom);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPaddingLeftRight(int $leftRight) : self
|
||||
{
|
||||
$this->style->setPaddingLeftRight($leftRight);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMarginAuto() : self
|
||||
{
|
||||
$this->style->setMarginAuto();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMargin(int $margin) : self
|
||||
{
|
||||
$this->style->setMargin($margin);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setItemExtra(string $extra) : self
|
||||
{
|
||||
$this->style->setItemExtra($extra);
|
||||
$this->getSelectableStyle()->setItemExtra($extra);
|
||||
|
||||
// if we customise item extra, it means we most likely want to display it
|
||||
$this->displayExtra();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTitleSeparator(string $separator) : self
|
||||
{
|
||||
$this->style->setTitleSeparator($separator);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string|null $right
|
||||
* @param int|string|null $bottom
|
||||
* @param int|string|null $left
|
||||
*/
|
||||
public function setBorder(int $top, $right = null, $bottom = null, $left = null, string $colour = null) : self
|
||||
{
|
||||
$this->style->setBorder($top, $right, $bottom, $left, $colour);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderTopWidth(int $width) : self
|
||||
{
|
||||
$this->style->setBorderTopWidth($width);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderRightWidth(int $width) : self
|
||||
{
|
||||
$this->style->setBorderRightWidth($width);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderBottomWidth(int $width) : self
|
||||
{
|
||||
$this->style->setBorderBottomWidth($width);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderLeftWidth(int $width) : self
|
||||
{
|
||||
$this->style->setBorderLeftWidth($width);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderColour(string $colour, string $fallback = null) : self
|
||||
{
|
||||
$this->style->setBorderColour($colour, $fallback);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStyle() : MenuStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function getTerminal() : Terminal
|
||||
{
|
||||
return $this->terminal;
|
||||
}
|
||||
|
||||
private function getDefaultItems() : array
|
||||
{
|
||||
$actions = [];
|
||||
if ($this->subMenu) {
|
||||
$actions[] = new SelectableItem($this->goBackButtonText, new GoBackAction);
|
||||
}
|
||||
|
||||
$actions[] = new SelectableItem($this->exitButtonText, new ExitAction);
|
||||
return $actions;
|
||||
}
|
||||
|
||||
public function disableDefaultItems() : self
|
||||
{
|
||||
$this->disableDefaultItems = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function displayExtra() : self
|
||||
{
|
||||
$this->style->setDisplaysExtra(true);
|
||||
$this->getSelectableStyle()->setDisplaysExtra(true);
|
||||
$this->getCheckboxStyle()->setDisplaysExtra(true);
|
||||
$this->getRadioStyle()->setDisplaysExtra(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function itemsHaveExtra(array $items) : bool
|
||||
{
|
||||
return !empty(array_filter($items, function (MenuItemInterface $item) {
|
||||
return $item->showsItemExtra();
|
||||
}));
|
||||
}
|
||||
|
||||
public function build() : CliMenu
|
||||
{
|
||||
if (!$this->disableDefaultItems) {
|
||||
$this->menu->addItems($this->getDefaultItems());
|
||||
}
|
||||
|
||||
if (!$this->style->getDisplaysExtra()) {
|
||||
$this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems()));
|
||||
}
|
||||
|
||||
if (!$this->subMenu) {
|
||||
$this->menu->propagateStyles();
|
||||
}
|
||||
|
||||
return $this->menu;
|
||||
}
|
||||
|
||||
public function getDefaultStyle() : DefaultStyle
|
||||
{
|
||||
$style = $this->menu->getItemStyle(DefaultStyle::class);
|
||||
assert($style instanceof DefaultStyle);
|
||||
return $style;
|
||||
}
|
||||
|
||||
public function setDefaultStyle(DefaultStyle $style) : self
|
||||
{
|
||||
$this->menu->setItemStyle($style, DefaultStyle::class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function modifyDefaultStyle(callable $itemCallable) : self
|
||||
{
|
||||
$itemCallable($this->getDefaultStyle());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSelectableStyle() : SelectableStyle
|
||||
{
|
||||
$style = $this->menu->getItemStyle(SelectableStyle::class);
|
||||
assert($style instanceof SelectableStyle);
|
||||
return $style;
|
||||
}
|
||||
|
||||
public function setSelectableStyle(SelectableStyle $style) : self
|
||||
{
|
||||
$this->menu->setItemStyle($style, SelectableStyle::class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function modifySelectableStyle(callable $itemCallable) : self
|
||||
{
|
||||
$itemCallable($this->getSelectableStyle());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCheckboxStyle() : CheckboxStyle
|
||||
{
|
||||
$style = $this->menu->getItemStyle(CheckboxStyle::class);
|
||||
assert($style instanceof CheckboxStyle);
|
||||
return $style;
|
||||
}
|
||||
|
||||
public function setCheckboxStyle(CheckboxStyle $style) : self
|
||||
{
|
||||
$this->menu->setItemStyle($style, CheckboxStyle::class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function modifyCheckboxStyle(callable $itemCallable) : self
|
||||
{
|
||||
$itemCallable($this->getCheckboxStyle());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRadioStyle() : RadioStyle
|
||||
{
|
||||
$style = $this->menu->getItemStyle(RadioStyle::class);
|
||||
assert($style instanceof RadioStyle);
|
||||
return $style;
|
||||
}
|
||||
|
||||
public function setRadioStyle(RadioStyle $style) : self
|
||||
{
|
||||
$this->menu->setItemStyle($style, RadioItem::class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function modifyRadioStyle(callable $itemCallable) : self
|
||||
{
|
||||
$itemCallable($this->getRadioStyle());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function modifyStyle(string $styleClass, callable $itemCallable) : self
|
||||
{
|
||||
$itemCallable($this->menu->getItemStyle($styleClass));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : self
|
||||
{
|
||||
$this->menu->getStyleLocator()
|
||||
->registerItemStyle($itemClass, $itemStyle);
|
||||
|
||||
$this->extraItemStyles[] = ['class' => $itemClass, 'style' => $itemStyle];
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,169 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Builder;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\CheckboxItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\LineBreakItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuMenuItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\RadioItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\SelectableItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\SplitItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\StaticItem;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use function \ncc\PhpSchool\CliMenu\Util\each;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class SplitItemBuilder
|
||||
{
|
||||
/**
|
||||
* @var CliMenu
|
||||
*/
|
||||
private $menu;
|
||||
|
||||
/**
|
||||
* @var SplitItem
|
||||
*/
|
||||
private $splitItem;
|
||||
|
||||
/**
|
||||
* Whether or not to auto create keyboard shortcuts for items
|
||||
* when they contain square brackets. Eg: [M]y item
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $autoShortcuts = false;
|
||||
|
||||
/**
|
||||
* Regex to auto match for shortcuts defaults to looking
|
||||
* for a single character encased in square brackets
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $autoShortcutsRegex = '/\[(.)\]/';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $extraItemStyles = [];
|
||||
|
||||
public function __construct(CliMenu $menu)
|
||||
{
|
||||
$this->menu = $menu;
|
||||
$this->splitItem = new SplitItem();
|
||||
}
|
||||
|
||||
public function addItem(
|
||||
string $text,
|
||||
callable $itemCallable,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) : self {
|
||||
$this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addCheckboxItem(
|
||||
string $text,
|
||||
callable $itemCallable,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) : self {
|
||||
$this->splitItem->addItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRadioItem(
|
||||
string $text,
|
||||
callable $itemCallable,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) : self {
|
||||
$this->splitItem->addItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addStaticItem(string $text) : self
|
||||
{
|
||||
$this->splitItem->addItem(new StaticItem($text));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
|
||||
{
|
||||
$this->splitItem->addItem(new LineBreakItem($breakChar, $lines));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSubMenu(string $text, \Closure $callback) : self
|
||||
{
|
||||
$builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal());
|
||||
|
||||
if ($this->autoShortcuts) {
|
||||
$builder->enableAutoShortcuts($this->autoShortcutsRegex);
|
||||
}
|
||||
|
||||
each($this->extraItemStyles, function (int $i, array $extraItemStyle) use ($builder) {
|
||||
$builder->registerItemStyle($extraItemStyle['class'], $extraItemStyle['style']);
|
||||
});
|
||||
|
||||
$callback($builder);
|
||||
|
||||
$menu = $builder->build();
|
||||
$menu->setParent($this->menu);
|
||||
|
||||
$this->splitItem->addItem(new MenuMenuItem(
|
||||
$text,
|
||||
$menu,
|
||||
$builder->isMenuDisabled()
|
||||
));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addMenuItem(MenuItemInterface $item) : self
|
||||
{
|
||||
$this->splitItem->addItem($item);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setGutter(int $gutter) : self
|
||||
{
|
||||
$this->splitItem->setGutter($gutter);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function enableAutoShortcuts(string $regex = null) : self
|
||||
{
|
||||
$this->autoShortcuts = true;
|
||||
|
||||
if (null !== $regex) {
|
||||
$this->autoShortcutsRegex = $regex;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : self
|
||||
{
|
||||
$this->extraItemStyles[] = ['class' => $itemClass, 'style' => $itemStyle];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function build() : SplitItem
|
||||
{
|
||||
return $this->splitItem;
|
||||
}
|
||||
}
|
161
src/ncc/ThirdParty/php-school/cli-menu/CHANGELOG.md
vendored
161
src/ncc/ThirdParty/php-school/cli-menu/CHANGELOG.md
vendored
|
@ -1,161 +0,0 @@
|
|||
# Change Log
|
||||
All notable changes to this project will be documented in this file.
|
||||
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
|
||||
|
||||
## [Unreleased][unreleased]
|
||||
### Added
|
||||
|
||||
### Changed
|
||||
|
||||
### Fixed
|
||||
|
||||
### Removed
|
||||
|
||||
## [4.3.0]
|
||||
### Fixed
|
||||
- PHP 8.1 Support (#252, #249)
|
||||
|
||||
### Added
|
||||
- declare(strict_types=1) everywhere
|
||||
|
||||
## [4.2.0]
|
||||
### Added
|
||||
- Yes/no confirmation dialogue (#248)
|
||||
- Ability to add multiple checkbox and radio items (#241)
|
||||
|
||||
## [4.1.0]
|
||||
### Added
|
||||
- Ability to modify password length for password input (#235)
|
||||
- Improve the formatting of disabled menu items in different terminals (#236)
|
||||
- Support for PHP8 (#240)
|
||||
|
||||
## [4.0.0]
|
||||
### Added
|
||||
- Add PHP 7.4 support (#183)
|
||||
- CheckboxItem & RadioItem (#186, #189, #193, #194, #226)
|
||||
- Ability to force display extra (#187)
|
||||
- Individual style objects for each item type (#211, #212, #213, #214, #216, #230)
|
||||
- Method getStyle() to interface ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface
|
||||
|
||||
### Fixed
|
||||
- Fixed item extra rendering outside of menu (#66, £184, #187)
|
||||
- Fix unresponsive menu upon closing and reopening (#198)
|
||||
- Menu styles incorrectly propagating to submenus (#201, #210)
|
||||
- Various issues with the menu width, when the terminal was too small (#223, #220, #219)
|
||||
|
||||
### Removed
|
||||
- Remove rebinding $this in builder closures so we can access the real $this (#191, #192, #196)
|
||||
- Marker methods from ncc\PhpSchool\CliMenu\MenuStyle:
|
||||
#getSelectedMarker()
|
||||
#setSelectedMarker()
|
||||
#getUnselectedMarker()
|
||||
#setUnselectedMarker()
|
||||
#getMarker()
|
||||
- ncc\PhpSchool\CliMenu\MenuItem\SelectableTrait
|
||||
- Marker methods from ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder:
|
||||
#setUnselectedMarker()
|
||||
#setSelectedMarker()
|
||||
|
||||
## [3.2.0]
|
||||
### Added
|
||||
- Allow ESC key to "cancel" editing an input (#174)
|
||||
- Methods for disabling the default VIM mappings and setting your own (#172)
|
||||
- Ability to set custom validator on Text and Number inputs (#177)
|
||||
- Ability to turn on automatic item shortcuts (#176)
|
||||
|
||||
## [3.1.0]
|
||||
### Changed
|
||||
- Update dependencies + fix some static analysis issues
|
||||
|
||||
## [3.0.0]
|
||||
### Changed
|
||||
- Optimise redrawing to reduce flickering (#83)
|
||||
- Use parent menu terminal when creating sub menus to reduce object graph (#94)
|
||||
- Do not print right margin. Causes menu to wrap even when row fits in terminal (#116)
|
||||
- CliMenu throws a \RuntimeException if it is opened with no items added (#146, #130)
|
||||
- Sub Menus are configured via closures (#155)
|
||||
- Remove restriction of 1 character length for markers (#141)
|
||||
- Remove the mandatory space after markers for now they can be of any length (#154)
|
||||
|
||||
### Added
|
||||
- Added type hints everywhere (#79)
|
||||
- Added phpstan to the travis build (#79)
|
||||
- Input dialogue system for prompting users. Comes with text, number and password inputs (#81)
|
||||
- Added ability to pass already prepared CliMenuBuilder instance to CliMenuBuilder#addSubMenuFromBuilder (#85, 155)
|
||||
- Added CliMenu#addItems & CliMenu#setItems to add multiple items and replace them (#86)
|
||||
- Added custom control mapping - link any key to a callable to immediately execute it (#87)
|
||||
- Added MenuMenuItem#getSubMenu (#92)
|
||||
- Added alternate text to AsciiArtItem to display if the ascii art is too large for the current terminal (#93)
|
||||
- Added the ability to pass existing MenuStyle instance to dialogues and inputs for consistent themes and reduced object graph (#99)
|
||||
- Added CSS like borders (#100)
|
||||
- Added option to auto center menu with CliMenuBuilder#setMarginAuto (#103)
|
||||
- Added option to auto center menu with CliMenuBuilder#setMarginAuto (#103)
|
||||
- Added support for 256 colours with automatic and manual fallback to 8 colours (#104)
|
||||
- Added clear option to CliMenu#redraw useful for when reducing the terminal width (#117)
|
||||
- Added ability to set top/bottom and left/right padding independently (#121)
|
||||
- Added a new Split Item item type which allows displaying multiple items on one line (#127)
|
||||
- Added setText methods to various items so they can be modified at runtime (#153)
|
||||
- Added MenuStyle#hasChangedFromDefaults to check if a MenuStyle has been modified (#149)
|
||||
- Added CliMenu#setTitle and CliMenu#setStyle (#155)
|
||||
- Added CliMenuBuilder#getStyle to get the current style object for the menu
|
||||
|
||||
### Fixed
|
||||
- Fixed sub menu go back button freezing menu (#88)
|
||||
- Fixed centering ascii art items with trailing white space (#102)
|
||||
- Enable cursor when exiting menu (#110)
|
||||
- Fixed (#71) - changed padding calculation when row too long to stop php notices (#112)
|
||||
- Fixed wordwrap helper (#134)
|
||||
- Fixed selected item issues when adding/setting/removing items (#156)
|
||||
- Fix infinite loop when no selectable items in menu (#159, #144)
|
||||
|
||||
### Removed
|
||||
- Dropped PHP 5.x and PHP 7.0 support (#79)
|
||||
- Removed the Terminal namespace which has been migrated to php-school/terminal (#81)
|
||||
- Removed MenuStyle::getDefaultStyleValues (#149)
|
||||
- Removed CliMenuBuilder#setTerminal (#149)
|
||||
- Removed CliMenuBuilder#getSubMenu (#155)
|
||||
- Removed CliMenuBuilder#getMenuStyle (#155)
|
||||
- Removed CliMenuBuilder#end (#155)
|
||||
|
||||
## [2.1.0]
|
||||
### Changed
|
||||
- Use new static for submenu to allow subclassing (#68)
|
||||
|
||||
### Added
|
||||
- Add emacs style up/down shortcuts ctrl+n and ctrl+p (#67)
|
||||
|
||||
## [2.0.2]
|
||||
### Fixed
|
||||
- Don't output ascii art if the terminal width is too small (#63)
|
||||
|
||||
## [2.0.1]
|
||||
### Fixed
|
||||
- Reset array keys after removing an item from the menu (#61)
|
||||
|
||||
## [2.0.0]
|
||||
### Fixed
|
||||
- PHPUnit deprecations - updated to createMock()
|
||||
|
||||
### Changed
|
||||
- Require ext-posix (#50)
|
||||
- Make MenuStyle easier to construct by only allowing changes to be made via setters (#45)
|
||||
|
||||
### Added
|
||||
- Added getStyle() to CliMenu to get access to the style object from the menu itself (#42)
|
||||
- Added redraw method to CliMenu which can redraw the menu immediately with any style changes. See
|
||||
examples/crazy-redraw.php for an example (#43)
|
||||
- Added tests for child menu style inheritance (#44)
|
||||
- Add getter getItems() to get all items from the menu (#46)
|
||||
- Add method removeItem(ItemInterface $item) to remove an item from the menu (#46)
|
||||
- Ability to toggle item extra while the menu is open - see examples/toggle-item-extra.php (#46)
|
||||
- Added dialogues flash and confirm - they both display some text on top of the menu, flash is dismissed with
|
||||
any key press where the confirm requires enter to be pressed on the provided button.
|
||||
See examples/confirm.php and examples/flash.php (#49)
|
||||
|
||||
### Removed
|
||||
- Removed windows terminal - many required terminal features are unavailable (#50)
|
||||
- Individual component instantiation restrictions (#41)
|
||||
|
||||
## [1.2.0]
|
||||
### Added
|
||||
- Added ability to disable menu items and sub-menus, they will appear dimmed and will be un-selectable (#40)
|
786
src/ncc/ThirdParty/php-school/cli-menu/CliMenu.php
vendored
786
src/ncc/ThirdParty/php-school/cli-menu/CliMenu.php
vendored
|
@ -1,786 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\Dialogue\CancellableConfirm;
|
||||
use ncc\PhpSchool\CliMenu\Exception\InvalidTerminalException;
|
||||
use ncc\PhpSchool\CliMenu\Exception\MenuNotOpenException;
|
||||
use ncc\PhpSchool\CliMenu\Input\InputIO;
|
||||
use ncc\PhpSchool\CliMenu\Input\Number;
|
||||
use ncc\PhpSchool\CliMenu\Input\Password;
|
||||
use ncc\PhpSchool\CliMenu\Input\Text;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\LineBreakItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\PropagatesStyles;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\SplitItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\StaticItem;
|
||||
use ncc\PhpSchool\CliMenu\Dialogue\Confirm;
|
||||
use ncc\PhpSchool\CliMenu\Dialogue\Flash;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\Locator;
|
||||
use ncc\PhpSchool\CliMenu\Terminal\TerminalFactory;
|
||||
use ncc\PhpSchool\CliMenu\Util\StringUtil as s;
|
||||
use ncc\PhpSchool\Terminal\InputCharacter;
|
||||
use ncc\PhpSchool\Terminal\NonCanonicalReader;
|
||||
use ncc\PhpSchool\Terminal\Terminal;
|
||||
use function ncc\PhpSchool\CliMenu\Util\collect;
|
||||
use function ncc\PhpSchool\CliMenu\Util\each;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class CliMenu
|
||||
{
|
||||
/**
|
||||
* @var Terminal
|
||||
*/
|
||||
protected $terminal;
|
||||
|
||||
/**
|
||||
* @var MenuStyle
|
||||
*/
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
* @var Locator
|
||||
*/
|
||||
private $itemStyleLocator;
|
||||
|
||||
/**
|
||||
* @var ?string
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* @var MenuItemInterface[]
|
||||
*/
|
||||
protected $items = [];
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected $selectedItem;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $open = false;
|
||||
|
||||
/**
|
||||
* @var CliMenu|null
|
||||
*/
|
||||
protected $parent;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultControlMappings = [
|
||||
'^P' => InputCharacter::UP,
|
||||
'k' => InputCharacter::UP,
|
||||
'^K' => InputCharacter::DOWN,
|
||||
'j' => InputCharacter::DOWN,
|
||||
"\r" => InputCharacter::ENTER,
|
||||
' ' => InputCharacter::ENTER,
|
||||
'l' => InputCharacter::LEFT,
|
||||
'm' => InputCharacter::RIGHT,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $customControlMappings = [];
|
||||
|
||||
/**
|
||||
* @var Frame
|
||||
*/
|
||||
private $currentFrame;
|
||||
|
||||
public function __construct(
|
||||
?string $title,
|
||||
array $items,
|
||||
Terminal $terminal = null,
|
||||
MenuStyle $style = null
|
||||
) {
|
||||
$this->title = $title;
|
||||
$this->items = $items;
|
||||
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
|
||||
$this->style = $style ?: new MenuStyle($this->terminal);
|
||||
|
||||
$this->itemStyleLocator = new Locator();
|
||||
|
||||
$this->selectFirstItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the terminal to work with CliMenu
|
||||
*/
|
||||
protected function configureTerminal() : void
|
||||
{
|
||||
$this->assertTerminalIsValidTTY();
|
||||
|
||||
$this->terminal->disableCanonicalMode();
|
||||
$this->terminal->disableEchoBack();
|
||||
$this->terminal->disableCursor();
|
||||
$this->terminal->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert changes made to the terminal
|
||||
*/
|
||||
protected function tearDownTerminal() : void
|
||||
{
|
||||
$this->terminal->restoreOriginalConfiguration();
|
||||
$this->terminal->enableCanonicalMode();
|
||||
$this->terminal->enableEchoBack();
|
||||
$this->terminal->enableCursor();
|
||||
}
|
||||
|
||||
private function assertTerminalIsValidTTY() : void
|
||||
{
|
||||
if (!$this->terminal->isInteractive()) {
|
||||
throw new InvalidTerminalException('Terminal is not interactive (TTY)');
|
||||
}
|
||||
}
|
||||
|
||||
public function setTitle(string $title) : void
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function getTitle() : ?string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setParent(CliMenu $parent) : void
|
||||
{
|
||||
$this->parent = $parent;
|
||||
}
|
||||
|
||||
public function getParent() : ?CliMenu
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
public function getTerminal() : Terminal
|
||||
{
|
||||
return $this->terminal;
|
||||
}
|
||||
|
||||
public function isOpen() : bool
|
||||
{
|
||||
return $this->open;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new Item to the menu
|
||||
*/
|
||||
public function addItem(MenuItemInterface $item) : void
|
||||
{
|
||||
$this->items[] = $item;
|
||||
|
||||
$this->selectFirstItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add multiple Items to the menu
|
||||
*/
|
||||
public function addItems(array $items) : void
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->items[] = $item;
|
||||
}
|
||||
|
||||
$this->selectFirstItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Items of the menu
|
||||
*/
|
||||
public function setItems(array $items) : void
|
||||
{
|
||||
$this->selectedItem = null;
|
||||
$this->items = $items;
|
||||
|
||||
$this->selectFirstItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the selected pointer to the first selectable item
|
||||
*/
|
||||
private function selectFirstItem() : void
|
||||
{
|
||||
if (null === $this->selectedItem) {
|
||||
foreach ($this->items as $key => $item) {
|
||||
if ($item->canSelect()) {
|
||||
$this->selectedItem = $key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the built-in VIM control mappings
|
||||
*/
|
||||
public function disableDefaultControlMappings() : void
|
||||
{
|
||||
$this->defaultControlMappings = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default control mappings
|
||||
*/
|
||||
public function setDefaultControlMappings(array $defaultControlMappings) : void
|
||||
{
|
||||
$this->defaultControlMappings = $defaultControlMappings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom control mapping
|
||||
*/
|
||||
public function addCustomControlMapping(string $input, callable $callable) : void
|
||||
{
|
||||
if (isset($this->defaultControlMappings[$input]) || isset($this->customControlMappings[$input])) {
|
||||
throw new \InvalidArgumentException('Cannot rebind this input');
|
||||
}
|
||||
|
||||
$this->customControlMappings[$input] = $callable;
|
||||
}
|
||||
|
||||
public function getCustomControlMappings() : array
|
||||
{
|
||||
return $this->customControlMappings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand function to add multiple custom control mapping at once
|
||||
*/
|
||||
public function addCustomControlMappings(array $map) : void
|
||||
{
|
||||
foreach ($map as $input => $callable) {
|
||||
$this->addCustomControlMapping($input, $callable);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a custom control mapping
|
||||
*/
|
||||
public function removeCustomControlMapping(string $input) : void
|
||||
{
|
||||
if (!isset($this->customControlMappings[$input])) {
|
||||
throw new \InvalidArgumentException('This input is not registered');
|
||||
}
|
||||
|
||||
unset($this->customControlMappings[$input]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display menu and capture input
|
||||
*/
|
||||
private function display() : void
|
||||
{
|
||||
$this->draw();
|
||||
|
||||
$reader = new NonCanonicalReader($this->terminal);
|
||||
$reader->addControlMappings($this->defaultControlMappings);
|
||||
|
||||
while ($this->isOpen()) {
|
||||
$char = $reader->readCharacter();
|
||||
if (!$char->isHandledControl()) {
|
||||
$rawChar = $char->get();
|
||||
if (isset($this->customControlMappings[$rawChar])) {
|
||||
$this->customControlMappings[$rawChar]($this);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($char->getControl()) {
|
||||
case InputCharacter::UP:
|
||||
case InputCharacter::DOWN:
|
||||
$this->moveSelectionVertically($char->getControl());
|
||||
$this->draw();
|
||||
break;
|
||||
case InputCharacter::LEFT:
|
||||
case InputCharacter::RIGHT:
|
||||
$this->moveSelectionHorizontally($char->getControl());
|
||||
$this->draw();
|
||||
break;
|
||||
case InputCharacter::ENTER:
|
||||
$this->executeCurrentItem();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the selection in a given direction, up / down
|
||||
*/
|
||||
protected function moveSelectionVertically(string $direction) : void
|
||||
{
|
||||
$itemKeys = array_keys($this->items);
|
||||
|
||||
$increments = 0;
|
||||
|
||||
do {
|
||||
$increments++;
|
||||
|
||||
if ($increments > count($itemKeys)) {
|
||||
//full cycle detected, there must be no selected items
|
||||
//in the menu, so stop trying to select one.
|
||||
return;
|
||||
}
|
||||
|
||||
$direction === 'UP'
|
||||
? $this->selectedItem--
|
||||
: $this->selectedItem++;
|
||||
|
||||
if ($this->selectedItem !== null && !array_key_exists($this->selectedItem, $this->items)) {
|
||||
$this->selectedItem = $direction === 'UP'
|
||||
? (int) end($itemKeys)
|
||||
: (int) reset($itemKeys);
|
||||
}
|
||||
} while (!$this->canSelect());
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the selection in a given direction, left / right
|
||||
*/
|
||||
protected function moveSelectionHorizontally(string $direction) : void
|
||||
{
|
||||
if (!$this->items[$this->selectedItem] instanceof SplitItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var SplitItem $item */
|
||||
$item = $this->items[$this->selectedItem];
|
||||
$itemKeys = array_keys($item->getItems());
|
||||
$selectedItemIndex = $item->getSelectedItemIndex();
|
||||
|
||||
if (null === $selectedItemIndex) {
|
||||
$selectedItemIndex = 0;
|
||||
}
|
||||
|
||||
do {
|
||||
$direction === 'LEFT'
|
||||
? $selectedItemIndex--
|
||||
: $selectedItemIndex++;
|
||||
|
||||
if (!array_key_exists($selectedItemIndex, $item->getItems())) {
|
||||
$selectedItemIndex = $direction === 'LEFT'
|
||||
? (int) end($itemKeys)
|
||||
: (int) reset($itemKeys);
|
||||
}
|
||||
} while (!$item->canSelectIndex($selectedItemIndex));
|
||||
|
||||
$item->setSelectedItemIndex($selectedItemIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the currently selected item actually be selected?
|
||||
*
|
||||
* For example:
|
||||
* selectable item -> yes
|
||||
* static item -> no
|
||||
* split item with only static items -> no
|
||||
* split item with at least one selectable item -> yes
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function canSelect() : bool
|
||||
{
|
||||
return $this->items[$this->selectedItem]->canSelect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the item the user actually selected
|
||||
*
|
||||
*/
|
||||
public function getSelectedItem() : MenuItemInterface
|
||||
{
|
||||
if (null === $this->selectedItem) {
|
||||
throw new \RuntimeException('No selected item');
|
||||
}
|
||||
|
||||
$item = $this->items[$this->selectedItem];
|
||||
return $item instanceof SplitItem
|
||||
? $item->getSelectedItem()
|
||||
: $item;
|
||||
}
|
||||
|
||||
public function setSelectedItem(MenuItemInterface $item) : void
|
||||
{
|
||||
$key = array_search($item, $this->items, true);
|
||||
|
||||
if (false === $key) {
|
||||
throw new \InvalidArgumentException('Item does not exist in menu');
|
||||
}
|
||||
|
||||
$this->selectedItem = (int) $key;
|
||||
}
|
||||
|
||||
public function getSelectedItemIndex() : int
|
||||
{
|
||||
if (null === $this->selectedItem) {
|
||||
throw new \RuntimeException('No selected item');
|
||||
}
|
||||
|
||||
return $this->selectedItem;
|
||||
}
|
||||
|
||||
public function getItemByIndex(int $index) : MenuItemInterface
|
||||
{
|
||||
if (!isset($this->items[$index])) {
|
||||
throw new \RuntimeException('Item with index does not exist');
|
||||
}
|
||||
|
||||
return $this->items[$index];
|
||||
}
|
||||
|
||||
public function executeAsSelected(MenuItemInterface $item) : void
|
||||
{
|
||||
$current = $this->items[$this->selectedItem];
|
||||
$this->setSelectedItem($item);
|
||||
$this->executeCurrentItem();
|
||||
$this->setSelectedItem($current);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the current item
|
||||
*/
|
||||
protected function executeCurrentItem() : void
|
||||
{
|
||||
$item = $this->getSelectedItem();
|
||||
|
||||
if ($item->canSelect()) {
|
||||
$callable = $item->getSelectAction();
|
||||
if ($callable) {
|
||||
$callable($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If true we clear the whole terminal screen, useful
|
||||
* for example when reducing the width of the menu, to not
|
||||
* leave leftovers of the previous wider menu.
|
||||
*
|
||||
* Redraw the menu
|
||||
*/
|
||||
public function redraw(bool $clear = false) : void
|
||||
{
|
||||
if ($clear) {
|
||||
$this->terminal->clear();
|
||||
}
|
||||
|
||||
$this->assertOpen();
|
||||
$this->draw();
|
||||
}
|
||||
|
||||
private function assertOpen() : void
|
||||
{
|
||||
if (!$this->isOpen()) {
|
||||
throw new MenuNotOpenException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the menu to STDOUT
|
||||
*/
|
||||
protected function draw() : void
|
||||
{
|
||||
$frame = new Frame;
|
||||
|
||||
$frame->newLine(2);
|
||||
|
||||
if ($this->style->getBorderTopWidth() > 0) {
|
||||
$frame->addRows($this->style->getBorderTopRows());
|
||||
}
|
||||
|
||||
if ($this->style->getPaddingTopBottom() > 0) {
|
||||
$frame->addRows($this->style->getPaddingTopBottomRows());
|
||||
}
|
||||
|
||||
if ($this->title) {
|
||||
$frame->addRows($this->drawMenuItem(new StaticItem($this->title)));
|
||||
$frame->addRows($this->drawMenuItem(new LineBreakItem($this->style->getTitleSeparator())));
|
||||
}
|
||||
|
||||
array_map(function ($item, $index) use ($frame) {
|
||||
$frame->addRows($this->drawMenuItem($item, $index === $this->selectedItem));
|
||||
}, $this->items, array_keys($this->items));
|
||||
|
||||
|
||||
if ($this->style->getPaddingTopBottom() > 0) {
|
||||
$frame->addRows($this->style->getPaddingTopBottomRows());
|
||||
}
|
||||
|
||||
if ($this->style->getBorderBottomWidth() > 0) {
|
||||
$frame->addRows($this->style->getBorderBottomRows());
|
||||
}
|
||||
|
||||
$frame->newLine(2);
|
||||
|
||||
$this->terminal->moveCursorToTop();
|
||||
foreach ($frame->getRows() as $row) {
|
||||
if ($row == "\n") {
|
||||
$this->terminal->clearLine();
|
||||
}
|
||||
$this->terminal->write($row);
|
||||
}
|
||||
$this->terminal->clearDown();
|
||||
|
||||
$this->currentFrame = $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a menu item
|
||||
*/
|
||||
protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) : array
|
||||
{
|
||||
$rows = $item->getRows($this->style, $selected);
|
||||
|
||||
if ($item instanceof SplitItem) {
|
||||
$selected = false;
|
||||
}
|
||||
|
||||
$invertedColoursSetCode = $selected
|
||||
? $this->style->getInvertedColoursSetCode()
|
||||
: '';
|
||||
$invertedColoursUnsetCode = $selected
|
||||
? $this->style->getInvertedColoursUnsetCode()
|
||||
: '';
|
||||
|
||||
if ($this->style->getBorderLeftWidth() || $this->style->getBorderRightWidth()) {
|
||||
$borderColour = $this->style->getBorderColourCode();
|
||||
} else {
|
||||
$borderColour = '';
|
||||
}
|
||||
|
||||
return array_map(function ($row) use ($invertedColoursSetCode, $invertedColoursUnsetCode, $borderColour) {
|
||||
return sprintf(
|
||||
"%s%s%s%s%s%s%s%s%s%s%s%s\n",
|
||||
str_repeat(' ', $this->style->getMargin()),
|
||||
$borderColour,
|
||||
str_repeat(' ', $this->style->getBorderLeftWidth()),
|
||||
$this->style->getColoursSetCode(),
|
||||
$invertedColoursSetCode,
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$row,
|
||||
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
|
||||
$invertedColoursUnsetCode,
|
||||
$borderColour,
|
||||
str_repeat(' ', $this->style->getBorderRightWidth()),
|
||||
$this->style->getColoursResetCode()
|
||||
);
|
||||
}, $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidTerminalException
|
||||
*/
|
||||
public function open() : void
|
||||
{
|
||||
if ($this->isOpen()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (count($this->items) === 0) {
|
||||
throw new \RuntimeException('Menu must have at least 1 item before it can be opened');
|
||||
}
|
||||
|
||||
$this->configureTerminal();
|
||||
$this->open = true;
|
||||
$this->display();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the menu
|
||||
*
|
||||
* @throws InvalidTerminalException
|
||||
*/
|
||||
public function close() : void
|
||||
{
|
||||
$menu = $this;
|
||||
|
||||
do {
|
||||
$menu->closeThis();
|
||||
$menu = $menu->getParent();
|
||||
} while (null !== $menu);
|
||||
|
||||
$this->tearDownTerminal();
|
||||
}
|
||||
|
||||
public function closeThis() : void
|
||||
{
|
||||
$this->terminal->clean();
|
||||
$this->terminal->moveCursorToTop();
|
||||
$this->open = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MenuItemInterface[]
|
||||
*/
|
||||
public function getItems() : array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function removeItem(MenuItemInterface $item) : void
|
||||
{
|
||||
$key = array_search($item, $this->items, true);
|
||||
|
||||
if (false === $key) {
|
||||
throw new \InvalidArgumentException('Item does not exist in menu');
|
||||
}
|
||||
|
||||
unset($this->items[$key]);
|
||||
$this->items = array_values($this->items);
|
||||
|
||||
if ($this->selectedItem === $key) {
|
||||
$this->selectedItem = null;
|
||||
$this->selectFirstItem();
|
||||
}
|
||||
}
|
||||
|
||||
public function getStyle() : MenuStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(MenuStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
public function setItemStyle(ItemStyle $style, string $styleClass) : void
|
||||
{
|
||||
$this->itemStyleLocator->setStyle($style, $styleClass);
|
||||
}
|
||||
|
||||
public function getItemStyle(string $styleClass) : ItemStyle
|
||||
{
|
||||
return $this->itemStyleLocator->getStyle($styleClass);
|
||||
}
|
||||
|
||||
public function getItemStyleForItem(MenuItemInterface $item) : ItemStyle
|
||||
{
|
||||
return $this->itemStyleLocator->getStyleForMenuItem($item);
|
||||
}
|
||||
|
||||
public function getStyleLocator() : Locator
|
||||
{
|
||||
return $this->itemStyleLocator;
|
||||
}
|
||||
|
||||
public function importStyles(CliMenu $menu) : void
|
||||
{
|
||||
if (!$this->style->hasChangedFromDefaults()) {
|
||||
$this->style = $menu->style;
|
||||
}
|
||||
|
||||
$this->itemStyleLocator->importFrom($menu->itemStyleLocator);
|
||||
}
|
||||
|
||||
public function getCurrentFrame() : Frame
|
||||
{
|
||||
return $this->currentFrame;
|
||||
}
|
||||
|
||||
public function flash(string $text, MenuStyle $style = null) : Flash
|
||||
{
|
||||
$this->guardSingleLine($text);
|
||||
|
||||
$style = $style ?? (new MenuStyle($this->terminal))
|
||||
->setBg('yellow')
|
||||
->setFg('red');
|
||||
|
||||
return new Flash($this, $style, $this->terminal, $text);
|
||||
}
|
||||
|
||||
public function confirm(string $text, MenuStyle $style = null) : Confirm
|
||||
{
|
||||
$this->guardSingleLine($text);
|
||||
|
||||
$style = $style ?? (new MenuStyle($this->terminal))
|
||||
->setBg('yellow')
|
||||
->setFg('red');
|
||||
|
||||
return new Confirm($this, $style, $this->terminal, $text);
|
||||
}
|
||||
|
||||
public function cancellableConfirm(string $text, MenuStyle $style = null) : CancellableConfirm
|
||||
{
|
||||
$this->guardSingleLine($text);
|
||||
|
||||
$style = $style ?? (new MenuStyle($this->terminal))
|
||||
->setBg('yellow')
|
||||
->setFg('red');
|
||||
|
||||
return new CancellableConfirm($this, $style, $this->terminal, $text);
|
||||
}
|
||||
|
||||
public function askNumber(MenuStyle $style = null) : Number
|
||||
{
|
||||
$this->assertOpen();
|
||||
|
||||
$style = $style ?? (new MenuStyle($this->terminal))
|
||||
->setBg('yellow')
|
||||
->setFg('red');
|
||||
|
||||
return new Number(new InputIO($this, $this->terminal), $style);
|
||||
}
|
||||
|
||||
public function askText(MenuStyle $style = null) : Text
|
||||
{
|
||||
$this->assertOpen();
|
||||
|
||||
$style = $style ?? (new MenuStyle($this->terminal))
|
||||
->setBg('yellow')
|
||||
->setFg('red');
|
||||
|
||||
return new Text(new InputIO($this, $this->terminal), $style);
|
||||
}
|
||||
|
||||
public function askPassword(MenuStyle $style = null) : Password
|
||||
{
|
||||
$this->assertOpen();
|
||||
|
||||
$style = $style ?? (new MenuStyle($this->terminal))
|
||||
->setBg('yellow')
|
||||
->setFg('red');
|
||||
|
||||
return new Password(new InputIO($this, $this->terminal), $style);
|
||||
}
|
||||
|
||||
private function guardSingleLine(string $text) : void
|
||||
{
|
||||
if (strpos($text, "\n") !== false) {
|
||||
throw new \InvalidArgumentException;
|
||||
}
|
||||
}
|
||||
|
||||
public function propagateStyles() : void
|
||||
{
|
||||
collect($this->items)
|
||||
->filter(function (int $k, MenuItemInterface $item) {
|
||||
return $this->itemStyleLocator->hasStyleForMenuItem($item);
|
||||
})
|
||||
->filter(function (int $k, MenuItemInterface $item) {
|
||||
return !$item->getStyle()->hasChangedFromDefaults();
|
||||
})
|
||||
->each(function (int $k, $item) {
|
||||
$item->setStyle(clone $this->getItemStyleForItem($item));
|
||||
});
|
||||
|
||||
|
||||
collect($this->items)
|
||||
->filter(function (int $k, MenuItemInterface $item) {
|
||||
return $item instanceof PropagatesStyles;
|
||||
})
|
||||
->each(function (int $k, $item) {
|
||||
$item->propagateStyles($this);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,114 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Dialogue;
|
||||
|
||||
use ncc\PhpSchool\Terminal\InputCharacter;
|
||||
use ncc\PhpSchool\Terminal\NonCanonicalReader;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class CancellableConfirm extends Dialogue
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $confirm = true;
|
||||
|
||||
/**
|
||||
* Display confirmation with a button with the given text
|
||||
*/
|
||||
public function display(string $confirmText = 'OK', string $cancelText = 'Cancel') : bool
|
||||
{
|
||||
$this->drawDialog($confirmText, $cancelText);
|
||||
|
||||
$reader = new NonCanonicalReader($this->terminal);
|
||||
|
||||
while ($char = $reader->readCharacter()) {
|
||||
if ($char->isControl() && $char->getControl() === InputCharacter::ENTER) {
|
||||
$this->parentMenu->redraw();
|
||||
return $this->confirm;
|
||||
} elseif ($char->isControl() && $char->getControl() === InputCharacter::TAB ||
|
||||
($char->isControl() && $char->getControl() === InputCharacter::RIGHT && $this->confirm) ||
|
||||
($char->isControl() && $char->getControl() === InputCharacter::LEFT && !$this->confirm)
|
||||
) {
|
||||
$this->confirm = !$this->confirm;
|
||||
$this->drawDialog($confirmText, $cancelText);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function drawDialog(string $confirmText = 'OK', string $cancelText = 'Cancel'): void
|
||||
{
|
||||
$this->assertMenuOpen();
|
||||
|
||||
$this->terminal->moveCursorToRow($this->y);
|
||||
|
||||
$promptWidth = mb_strlen($this->text) + 4;
|
||||
|
||||
$buttonLength = mb_strlen($confirmText) + 6;
|
||||
$buttonLength += mb_strlen($cancelText) + 7;
|
||||
|
||||
$confirmButton = sprintf(
|
||||
'%s%s < %s > %s%s',
|
||||
$this->style->getOptionCode($this->confirm ? 'bold' : 'dim'),
|
||||
$this->style->getInvertedColoursSetCode(),
|
||||
$confirmText,
|
||||
$this->style->getInvertedColoursUnsetCode(),
|
||||
$this->style->getOptionCode($this->confirm ? 'bold' : 'dim', false)
|
||||
);
|
||||
|
||||
$cancelButton = sprintf(
|
||||
'%s%s < %s > %s%s',
|
||||
$this->style->getOptionCode($this->confirm ? 'dim' : 'bold'),
|
||||
$this->style->getInvertedColoursSetCode(),
|
||||
$cancelText,
|
||||
$this->style->getInvertedColoursUnsetCode(),
|
||||
$this->style->getOptionCode($this->confirm ? 'dim' : 'bold', false)
|
||||
);
|
||||
|
||||
$buttonRow = $confirmButton . " " . $cancelButton;
|
||||
|
||||
if ($promptWidth < $buttonLength) {
|
||||
$pad = ($buttonLength - $promptWidth) / 2;
|
||||
$this->text = sprintf(
|
||||
'%s%s%s',
|
||||
str_repeat(' ', intval(round($pad, 0, 2) + $this->style->getPaddingLeftRight())),
|
||||
$this->text,
|
||||
str_repeat(' ', intval(round($pad, 0, 1) + $this->style->getPaddingLeftRight()))
|
||||
);
|
||||
$promptWidth = mb_strlen($this->text) + 4;
|
||||
}
|
||||
|
||||
$leftFill = (int) (($promptWidth / 2) - ($buttonLength / 2));
|
||||
|
||||
$this->emptyRow();
|
||||
|
||||
$this->write(sprintf(
|
||||
"%s%s%s%s%s\n",
|
||||
$this->style->getColoursSetCode(),
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->text,
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->style->getColoursResetCode()
|
||||
));
|
||||
|
||||
$this->emptyRow();
|
||||
|
||||
$this->write(sprintf(
|
||||
"%s%s%s%s%s\n",
|
||||
$this->style->getColoursSetCode(),
|
||||
str_repeat(' ', $leftFill),
|
||||
$buttonRow,
|
||||
str_repeat(' ', (int) ceil($promptWidth - $leftFill - $buttonLength)),
|
||||
$this->style->getColoursResetCode()
|
||||
));
|
||||
|
||||
$this->emptyRow();
|
||||
|
||||
$this->terminal->moveCursorToTop();
|
||||
}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Dialogue;
|
||||
|
||||
use ncc\PhpSchool\Terminal\InputCharacter;
|
||||
use ncc\PhpSchool\Terminal\NonCanonicalReader;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class Confirm extends Dialogue
|
||||
{
|
||||
|
||||
/**
|
||||
* Display confirmation with a button with the given text
|
||||
*/
|
||||
public function display(string $confirmText = 'OK') : void
|
||||
{
|
||||
$this->assertMenuOpen();
|
||||
|
||||
$this->terminal->moveCursorToRow($this->y);
|
||||
|
||||
$promptWidth = mb_strlen($this->text) + 4;
|
||||
|
||||
$this->emptyRow();
|
||||
|
||||
$this->write(sprintf(
|
||||
"%s%s%s%s%s\n",
|
||||
$this->style->getColoursSetCode(),
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->text,
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->style->getColoursResetCode()
|
||||
));
|
||||
|
||||
$this->emptyRow();
|
||||
|
||||
$confirmText = sprintf(' < %s > ', $confirmText);
|
||||
$leftFill = (int) (($promptWidth / 2) - (mb_strlen($confirmText) / 2));
|
||||
|
||||
$this->write(sprintf(
|
||||
"%s%s%s%s%s%s%s\n",
|
||||
$this->style->getColoursSetCode(),
|
||||
str_repeat(' ', $leftFill),
|
||||
$this->style->getInvertedColoursSetCode(),
|
||||
$confirmText,
|
||||
$this->style->getInvertedColoursUnsetCode(),
|
||||
str_repeat(' ', (int) ceil($promptWidth - $leftFill - mb_strlen($confirmText))),
|
||||
$this->style->getColoursResetCode()
|
||||
));
|
||||
|
||||
$this->write(sprintf(
|
||||
"%s%s%s%s%s\n",
|
||||
$this->style->getColoursSetCode(),
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
str_repeat(' ', mb_strlen($this->text)),
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->style->getColoursResetCode()
|
||||
));
|
||||
|
||||
$this->terminal->moveCursorToTop();
|
||||
|
||||
$reader = new NonCanonicalReader($this->terminal);
|
||||
|
||||
while ($char = $reader->readCharacter()) {
|
||||
if ($char->isControl() && $char->getControl() === InputCharacter::ENTER) {
|
||||
$this->parentMenu->redraw();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Dialogue;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\Exception\MenuNotOpenException;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\Terminal\Terminal;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
abstract class Dialogue
|
||||
{
|
||||
/**
|
||||
* @var MenuStyle
|
||||
*/
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
* @var CliMenu
|
||||
*/
|
||||
protected $parentMenu;
|
||||
|
||||
/**
|
||||
* @var Terminal
|
||||
*/
|
||||
protected $terminal;
|
||||
|
||||
/**
|
||||
* @var string $text
|
||||
*/
|
||||
protected $text;
|
||||
|
||||
/**
|
||||
* @var bool $cancellable
|
||||
*/
|
||||
protected $cancellable;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $x;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $y;
|
||||
|
||||
public function __construct(
|
||||
CliMenu $parentMenu,
|
||||
MenuStyle $menuStyle,
|
||||
Terminal $terminal,
|
||||
string $text
|
||||
) {
|
||||
$this->style = $menuStyle;
|
||||
$this->terminal = $terminal;
|
||||
$this->text = $text;
|
||||
$this->parentMenu = $parentMenu;
|
||||
|
||||
$this->calculateCoordinates();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws MenuNotOpenException
|
||||
*/
|
||||
protected function assertMenuOpen() : void
|
||||
{
|
||||
if (!$this->parentMenu->isOpen()) {
|
||||
throw new MenuNotOpenException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the co-ordinates to write the messages
|
||||
*/
|
||||
protected function calculateCoordinates() : void
|
||||
{
|
||||
//y
|
||||
$textLines = count(explode("\n", $this->text)) + 2;
|
||||
$this->y = (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($textLines / 2) + 1);
|
||||
|
||||
//x
|
||||
$parentStyle = $this->parentMenu->getStyle();
|
||||
$dialogueHalfLength = (int) ((mb_strlen($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2);
|
||||
$widthHalfLength = (int) ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
|
||||
$this->x = $widthHalfLength - $dialogueHalfLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an empty row
|
||||
*/
|
||||
protected function emptyRow() : void
|
||||
{
|
||||
$this->write(
|
||||
sprintf(
|
||||
"%s%s%s%s%s\n",
|
||||
$this->style->getColoursSetCode(),
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
str_repeat(' ', mb_strlen($this->text)),
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->style->getColoursResetCode()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write some text at a particular column
|
||||
*/
|
||||
protected function write(string $text, int $column = null) : void
|
||||
{
|
||||
$this->terminal->moveCursorToColumn($column ?: $this->x);
|
||||
$this->terminal->write($text);
|
||||
}
|
||||
|
||||
public function getStyle() : MenuStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Dialogue;
|
||||
|
||||
use ncc\PhpSchool\Terminal\NonCanonicalReader;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class Flash extends Dialogue
|
||||
{
|
||||
/**
|
||||
* Flash a message on top of the menu which
|
||||
* disappears on any keystroke.
|
||||
*/
|
||||
public function display() : void
|
||||
{
|
||||
$this->assertMenuOpen();
|
||||
|
||||
$this->terminal->moveCursorToRow($this->y);
|
||||
|
||||
$this->emptyRow();
|
||||
|
||||
$this->write(sprintf(
|
||||
"%s%s%s%s%s\n",
|
||||
$this->style->getColoursSetCode(),
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->text,
|
||||
str_repeat(' ', $this->style->getPaddingLeftRight()),
|
||||
$this->style->getColoursResetCode()
|
||||
));
|
||||
|
||||
$this->emptyRow();
|
||||
|
||||
$this->terminal->moveCursorToTop();
|
||||
|
||||
$reader = new NonCanonicalReader($this->terminal);
|
||||
$reader->readCharacter();
|
||||
|
||||
$this->parentMenu->redraw();
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Exception;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class CannotShrinkMenuException extends InvalidArgumentException
|
||||
{
|
||||
public static function fromMarginAndTerminalWidth(int $margin, int $terminalWidth) : self
|
||||
{
|
||||
return new self(
|
||||
sprintf(
|
||||
'Cannot shrink menu. Margin: %s * 2 with terminal width: %s leaves no space for menu',
|
||||
$margin,
|
||||
$terminalWidth
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Exception;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class InvalidShortcutException extends \RuntimeException
|
||||
{
|
||||
public static function fromShortcut(string $shortcut) : self
|
||||
{
|
||||
return new self(sprintf('Shortcut key must be only one character. Got: "%s"', $shortcut));
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Exception;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class InvalidTerminalException extends \Exception
|
||||
{
|
||||
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Exception;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class MenuNotOpenException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
47
src/ncc/ThirdParty/php-school/cli-menu/Frame.php
vendored
47
src/ncc/ThirdParty/php-school/cli-menu/Frame.php
vendored
|
@ -1,47 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu;
|
||||
|
||||
/**
|
||||
* Represents the current screen being displayed
|
||||
* contains all rows of output
|
||||
*
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class Frame implements \Countable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $rows = [];
|
||||
|
||||
public function newLine(int $count = 1) : void
|
||||
{
|
||||
foreach (range(1, $count) as $i) {
|
||||
$this->rows[] = "\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function addRows(array $rows = []) : void
|
||||
{
|
||||
foreach ($rows as $row) {
|
||||
$this->rows[] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
public function addRow(string $row) : void
|
||||
{
|
||||
$this->rows[] = $row;
|
||||
}
|
||||
|
||||
public function count() : int
|
||||
{
|
||||
return count($this->rows);
|
||||
}
|
||||
|
||||
public function getRows() : array
|
||||
{
|
||||
return $this->rows;
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Input;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
interface Input
|
||||
{
|
||||
public function ask() : InputResult;
|
||||
|
||||
public function validate(string $input) : bool;
|
||||
|
||||
public function setPromptText(string $promptText) : Input;
|
||||
|
||||
public function getPromptText() : string;
|
||||
|
||||
public function setValidationFailedText(string $validationFailedText) : Input;
|
||||
|
||||
public function getValidationFailedText() : string;
|
||||
|
||||
public function setPlaceholderText(string $placeholderText) : Input;
|
||||
|
||||
public function getPlaceholderText() : string;
|
||||
|
||||
public function filter(string $value) : string;
|
||||
|
||||
public function getStyle() : MenuStyle;
|
||||
}
|
|
@ -1,256 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Input;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\Util\StringUtil;
|
||||
use ncc\PhpSchool\Terminal\InputCharacter;
|
||||
use ncc\PhpSchool\Terminal\NonCanonicalReader;
|
||||
use ncc\PhpSchool\Terminal\Terminal;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class InputIO
|
||||
{
|
||||
/**
|
||||
* @var CliMenu
|
||||
*/
|
||||
private $parentMenu;
|
||||
|
||||
/**
|
||||
* @var Terminal
|
||||
*/
|
||||
private $terminal;
|
||||
|
||||
/**
|
||||
* @var callable[][]
|
||||
*/
|
||||
private $callbacks = [];
|
||||
|
||||
public function __construct(CliMenu $parentMenu, Terminal $terminal)
|
||||
{
|
||||
$this->terminal = $terminal;
|
||||
$this->parentMenu = $parentMenu;
|
||||
}
|
||||
|
||||
public function collect(Input $input) : InputResult
|
||||
{
|
||||
$this->drawInput($input, $input->getPlaceholderText());
|
||||
|
||||
$inputValue = $input->getPlaceholderText();
|
||||
$havePlaceHolderValue = !empty($inputValue);
|
||||
|
||||
$originalValue = $inputValue;
|
||||
|
||||
$reader = new NonCanonicalReader($this->terminal);
|
||||
|
||||
while ($char = $reader->readCharacter()) {
|
||||
if ($char->isNotControl()) {
|
||||
if ($havePlaceHolderValue) {
|
||||
$inputValue = $char->get();
|
||||
$havePlaceHolderValue = false;
|
||||
} else {
|
||||
$inputValue .= $char->get();
|
||||
}
|
||||
|
||||
$this->parentMenu->redraw();
|
||||
$this->drawInput($input, $inputValue);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($char->isHandledControl()) {
|
||||
switch ($char->getControl()) {
|
||||
case InputCharacter::ESC:
|
||||
$this->parentMenu->redraw();
|
||||
return new InputResult($originalValue);
|
||||
case InputCharacter::ENTER:
|
||||
if ($input->validate($inputValue)) {
|
||||
$this->parentMenu->redraw();
|
||||
return new InputResult($inputValue);
|
||||
} else {
|
||||
$this->drawInputWithError($input, $inputValue);
|
||||
continue 2;
|
||||
}
|
||||
|
||||
case InputCharacter::BACKSPACE:
|
||||
$inputValue = substr($inputValue, 0, -1);
|
||||
$this->parentMenu->redraw();
|
||||
$this->drawInput($input, $inputValue);
|
||||
continue 2;
|
||||
}
|
||||
|
||||
if (!empty($this->callbacks[$char->getControl()])) {
|
||||
foreach ($this->callbacks[$char->getControl()] as $callback) {
|
||||
$inputValue = $callback($inputValue);
|
||||
$this->drawInput($input, $inputValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function registerControlCallback(string $control, callable $callback) : void
|
||||
{
|
||||
if (!isset($this->callbacks[$control])) {
|
||||
$this->callbacks[$control] = [];
|
||||
}
|
||||
|
||||
$this->callbacks[$control][] = $callback;
|
||||
}
|
||||
|
||||
private function getInputWidth(array $lines) : int
|
||||
{
|
||||
return max(
|
||||
array_map(
|
||||
function (string $line) {
|
||||
return mb_strlen($line);
|
||||
},
|
||||
$lines
|
||||
)
|
||||
) ? : 0;
|
||||
}
|
||||
|
||||
private function calculateYPosition() : int
|
||||
{
|
||||
$lines = 5; //1. empty 2. prompt text 3. empty 4. input 5. empty
|
||||
|
||||
return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1);
|
||||
}
|
||||
|
||||
private function calculateYPositionWithError() : int
|
||||
{
|
||||
$lines = 7; //1. empty 2. prompt text 3. empty 4. input 5. empty 6. error 7. empty
|
||||
|
||||
return (int) (ceil($this->parentMenu->getCurrentFrame()->count() / 2) - ceil($lines /2) + 1);
|
||||
}
|
||||
|
||||
private function calculateXPosition(Input $input, string $userInput) : int
|
||||
{
|
||||
$width = $this->getInputWidth(
|
||||
[
|
||||
$input->getPromptText(),
|
||||
$input->getValidationFailedText(),
|
||||
$userInput
|
||||
]
|
||||
);
|
||||
|
||||
$parentStyle = $this->parentMenu->getStyle();
|
||||
$halfWidth = ($width + ($input->getStyle()->getPaddingLeftRight() * 2)) / 2;
|
||||
$parentHalfWidth = ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
|
||||
|
||||
return (int) ($parentHalfWidth - $halfWidth);
|
||||
}
|
||||
|
||||
private function drawLine(Input $input, string $userInput, string $text) : void
|
||||
{
|
||||
$this->terminal->moveCursorToColumn($this->calculateXPosition($input, $userInput));
|
||||
|
||||
$line = sprintf(
|
||||
"%s%s%s%s%s\n",
|
||||
$input->getStyle()->getColoursSetCode(),
|
||||
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
|
||||
$text,
|
||||
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
|
||||
$input->getStyle()->getColoursResetCode()
|
||||
);
|
||||
|
||||
$this->terminal->write($line);
|
||||
}
|
||||
|
||||
private function drawCenteredLine(Input $input, string $userInput, string $text) : void
|
||||
{
|
||||
$width = $this->getInputWidth(
|
||||
[
|
||||
$input->getPromptText(),
|
||||
$input->getValidationFailedText(),
|
||||
$userInput
|
||||
]
|
||||
);
|
||||
|
||||
$textLength = mb_strlen(StringUtil::stripAnsiEscapeSequence($text));
|
||||
$leftFill = (int) (($width / 2) - ($textLength / 2));
|
||||
$rightFill = (int) ceil($width - $leftFill - $textLength);
|
||||
|
||||
$this->drawLine(
|
||||
$input,
|
||||
$userInput,
|
||||
sprintf(
|
||||
'%s%s%s',
|
||||
str_repeat(' ', $leftFill),
|
||||
$text,
|
||||
str_repeat(' ', $rightFill)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function drawEmptyLine(Input $input, string $userInput) : void
|
||||
{
|
||||
$width = $this->getInputWidth(
|
||||
[
|
||||
$input->getPromptText(),
|
||||
$input->getValidationFailedText(),
|
||||
$userInput
|
||||
]
|
||||
);
|
||||
|
||||
$this->drawLine(
|
||||
$input,
|
||||
$userInput,
|
||||
str_repeat(' ', $width)
|
||||
);
|
||||
}
|
||||
|
||||
private function drawInput(Input $input, string $userInput) : void
|
||||
{
|
||||
$this->terminal->moveCursorToRow($this->calculateYPosition());
|
||||
|
||||
$this->drawEmptyLine($input, $userInput);
|
||||
$this->drawTitle($input, $userInput);
|
||||
$this->drawEmptyLine($input, $userInput);
|
||||
$this->drawInputField($input, $input->filter($userInput));
|
||||
$this->drawEmptyLine($input, $userInput);
|
||||
}
|
||||
|
||||
private function drawInputWithError(Input $input, string $userInput) : void
|
||||
{
|
||||
$this->terminal->moveCursorToRow($this->calculateYPositionWithError());
|
||||
|
||||
$this->drawEmptyLine($input, $userInput);
|
||||
$this->drawTitle($input, $userInput);
|
||||
$this->drawEmptyLine($input, $userInput);
|
||||
$this->drawInputField($input, $input->filter($userInput));
|
||||
$this->drawEmptyLine($input, $userInput);
|
||||
$this->drawCenteredLine(
|
||||
$input,
|
||||
$userInput,
|
||||
$input->getValidationFailedText()
|
||||
);
|
||||
$this->drawEmptyLine($input, $userInput);
|
||||
}
|
||||
|
||||
private function drawTitle(Input $input, string $userInput) : void
|
||||
{
|
||||
|
||||
$this->drawCenteredLine(
|
||||
$input,
|
||||
$userInput,
|
||||
$input->getPromptText()
|
||||
);
|
||||
}
|
||||
|
||||
private function drawInputField(Input $input, string $userInput) : void
|
||||
{
|
||||
$this->drawCenteredLine(
|
||||
$input,
|
||||
$userInput,
|
||||
sprintf(
|
||||
'%s%s%s',
|
||||
$input->getStyle()->getInvertedColoursSetCode(),
|
||||
$userInput,
|
||||
$input->getStyle()->getInvertedColoursUnsetCode()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Input;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class InputResult
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $input;
|
||||
|
||||
public function __construct(string $input)
|
||||
{
|
||||
$this->input = $input;
|
||||
}
|
||||
|
||||
public function fetch() : string
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
}
|
|
@ -1,130 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Input;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\Terminal\InputCharacter;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class Number implements Input
|
||||
{
|
||||
/**
|
||||
* @var InputIO
|
||||
*/
|
||||
private $inputIO;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $promptText = 'Enter a number:';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $validationFailedText = 'Not a valid number, try again';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $placeholderText = '';
|
||||
|
||||
/**
|
||||
* @var null|callable
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* @var MenuStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(InputIO $inputIO, MenuStyle $style)
|
||||
{
|
||||
$this->inputIO = $inputIO;
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
public function setPromptText(string $promptText) : Input
|
||||
{
|
||||
$this->promptText = $promptText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPromptText() : string
|
||||
{
|
||||
return $this->promptText;
|
||||
}
|
||||
|
||||
public function setValidationFailedText(string $validationFailedText) : Input
|
||||
{
|
||||
$this->validationFailedText = $validationFailedText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValidationFailedText() : string
|
||||
{
|
||||
return $this->validationFailedText;
|
||||
}
|
||||
|
||||
public function setPlaceholderText(string $placeholderText) : Input
|
||||
{
|
||||
$this->placeholderText = $placeholderText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPlaceholderText() : string
|
||||
{
|
||||
return $this->placeholderText;
|
||||
}
|
||||
|
||||
public function setValidator(callable $validator) : Input
|
||||
{
|
||||
$this->validator = $validator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function ask() : InputResult
|
||||
{
|
||||
$this->inputIO->registerControlCallback(InputCharacter::UP, function (string $input) {
|
||||
return $this->validate($input) ? (string) ((int) $input + 1) : $input;
|
||||
});
|
||||
|
||||
$this->inputIO->registerControlCallback(InputCharacter::DOWN, function (string $input) {
|
||||
return $this->validate($input) ? (string) ((int) $input - 1) : $input;
|
||||
});
|
||||
|
||||
return $this->inputIO->collect($this);
|
||||
}
|
||||
|
||||
public function validate(string $input) : bool
|
||||
{
|
||||
if ($this->validator) {
|
||||
$validator = $this->validator;
|
||||
|
||||
if ($validator instanceof \Closure) {
|
||||
$validator = $validator->bindTo($this);
|
||||
}
|
||||
|
||||
return $validator($input);
|
||||
}
|
||||
|
||||
return (bool) preg_match('/^-?\d+$/', $input);
|
||||
}
|
||||
|
||||
public function filter(string $value) : string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getStyle() : MenuStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
}
|
|
@ -1,131 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Input;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class Password implements Input
|
||||
{
|
||||
/**
|
||||
* @var InputIO
|
||||
*/
|
||||
private $inputIO;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $promptText = 'Enter password:';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $validationFailedText = 'Invalid password, try again';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $placeholderText = '';
|
||||
|
||||
/**
|
||||
* @var null|callable
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* @var MenuStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $passwordLength = 16;
|
||||
|
||||
public function __construct(InputIO $inputIO, MenuStyle $style)
|
||||
{
|
||||
$this->inputIO = $inputIO;
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
public function setPromptText(string $promptText) : Input
|
||||
{
|
||||
$this->promptText = $promptText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPromptText() : string
|
||||
{
|
||||
return $this->promptText;
|
||||
}
|
||||
|
||||
public function setValidationFailedText(string $validationFailedText) : Input
|
||||
{
|
||||
$this->validationFailedText = $validationFailedText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValidationFailedText() : string
|
||||
{
|
||||
return $this->validationFailedText;
|
||||
}
|
||||
|
||||
public function setPlaceholderText(string $placeholderText) : Input
|
||||
{
|
||||
$this->placeholderText = $placeholderText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPlaceholderText() : string
|
||||
{
|
||||
return $this->placeholderText;
|
||||
}
|
||||
|
||||
public function setValidator(callable $validator) : Input
|
||||
{
|
||||
$this->validator = $validator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function ask() : InputResult
|
||||
{
|
||||
return $this->inputIO->collect($this);
|
||||
}
|
||||
|
||||
public function validate(string $input) : bool
|
||||
{
|
||||
if ($this->validator) {
|
||||
$validator = $this->validator;
|
||||
|
||||
if ($validator instanceof \Closure) {
|
||||
$validator = $validator->bindTo($this);
|
||||
}
|
||||
|
||||
return $validator($input);
|
||||
}
|
||||
|
||||
return mb_strlen($input) >= $this->passwordLength;
|
||||
}
|
||||
|
||||
public function filter(string $value) : string
|
||||
{
|
||||
return str_repeat('*', mb_strlen($value));
|
||||
}
|
||||
|
||||
public function getStyle() : MenuStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setPasswordLength(int $length) : int
|
||||
{
|
||||
return $this->passwordLength = $length;
|
||||
}
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Input;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class Text implements Input
|
||||
{
|
||||
/**
|
||||
* @var InputIO
|
||||
*/
|
||||
private $inputIO;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $promptText = 'Enter text:';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $validationFailedText = 'Invalid, try again';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $placeholderText = '';
|
||||
|
||||
/**
|
||||
* @var null|callable
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* @var MenuStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(InputIO $inputIO, MenuStyle $style)
|
||||
{
|
||||
$this->inputIO = $inputIO;
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
public function setPromptText(string $promptText) : Input
|
||||
{
|
||||
$this->promptText = $promptText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPromptText() : string
|
||||
{
|
||||
return $this->promptText;
|
||||
}
|
||||
|
||||
public function setValidationFailedText(string $validationFailedText) : Input
|
||||
{
|
||||
$this->validationFailedText = $validationFailedText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValidationFailedText() : string
|
||||
{
|
||||
return $this->validationFailedText;
|
||||
}
|
||||
|
||||
public function setPlaceholderText(string $placeholderText) : Input
|
||||
{
|
||||
$this->placeholderText = $placeholderText;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPlaceholderText() : string
|
||||
{
|
||||
return $this->placeholderText;
|
||||
}
|
||||
|
||||
public function setValidator(callable $validator) : Input
|
||||
{
|
||||
$this->validator = $validator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function ask() : InputResult
|
||||
{
|
||||
return $this->inputIO->collect($this);
|
||||
}
|
||||
|
||||
public function validate(string $input) : bool
|
||||
{
|
||||
if ($this->validator) {
|
||||
$validator = $this->validator;
|
||||
|
||||
if ($validator instanceof \Closure) {
|
||||
$validator = $validator->bindTo($this);
|
||||
}
|
||||
|
||||
return $validator($input);
|
||||
}
|
||||
|
||||
return !empty($input);
|
||||
}
|
||||
|
||||
public function filter(string $value) : string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getStyle() : MenuStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
}
|
20
src/ncc/ThirdParty/php-school/cli-menu/LICENSE
vendored
20
src/ncc/ThirdParty/php-school/cli-menu/LICENSE
vendored
|
@ -1,20 +0,0 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2016 PHP School
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -1,187 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\Assert\Assertion;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\DefaultStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class AsciiArtItem implements MenuItemInterface
|
||||
{
|
||||
/**
|
||||
* Possible positions of the ascii art
|
||||
*/
|
||||
const POSITION_CENTER = 'center';
|
||||
const POSITION_LEFT = 'left';
|
||||
const POSITION_RIGHT = 'right';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $position;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $alternateText;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $artLength;
|
||||
|
||||
/**
|
||||
* @var DefaultStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(string $text, string $position = self::POSITION_CENTER, string $alt = '')
|
||||
{
|
||||
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);
|
||||
|
||||
$this->setText($text);
|
||||
$this->position = $position;
|
||||
$this->alternateText = $alt;
|
||||
|
||||
$this->style = new DefaultStyle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
if ($this->artLength > $style->getContentWidth()) {
|
||||
$alternate = new StaticItem($this->alternateText);
|
||||
return $alternate->getRows($style, false);
|
||||
}
|
||||
|
||||
$padding = $style->getContentWidth() - $this->artLength;
|
||||
|
||||
return array_map(function ($row) use ($padding) {
|
||||
switch ($this->position) {
|
||||
case self::POSITION_LEFT:
|
||||
break;
|
||||
case self::POSITION_RIGHT:
|
||||
$row = sprintf('%s%s', str_repeat(' ', $padding), $row);
|
||||
break;
|
||||
case self::POSITION_CENTER:
|
||||
default:
|
||||
$left = (int) ceil($padding / 2);
|
||||
$row = sprintf('%s%s', str_repeat(' ', $left), $row);
|
||||
break;
|
||||
}
|
||||
|
||||
return $row;
|
||||
}, explode("\n", $this->text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the raw string of text
|
||||
*/
|
||||
public function setText(string $text) : void
|
||||
{
|
||||
$this->text = implode("\n", array_map(function (string $line) {
|
||||
return rtrim($line, ' ');
|
||||
}, explode("\n", $text)));
|
||||
|
||||
$this->calculateArtLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the length of the art
|
||||
*/
|
||||
private function calculateArtLength() : void
|
||||
{
|
||||
$this->artLength = (int) max(array_map('mb_strlen', explode("\n", $this->text)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the length of the art
|
||||
*/
|
||||
public function getArtLength() : int
|
||||
{
|
||||
return $this->artLength;
|
||||
}
|
||||
|
||||
public function getPosition() : string
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
public function getAlternateText() : string
|
||||
{
|
||||
return $this->alternateText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the menu item is showing the menustyle extra value
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DefaultStyle
|
||||
*/
|
||||
public function getStyle() : ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(DefaultStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
}
|
|
@ -1,172 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\CheckboxStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
|
||||
class CheckboxItem implements MenuItemInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $selectAction;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $showItemExtra;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $disabled;
|
||||
|
||||
/**
|
||||
* The current checkbox state
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $checked = false;
|
||||
|
||||
/**
|
||||
* @var CheckboxStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(
|
||||
string $text,
|
||||
callable $selectAction,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) {
|
||||
$this->text = $text;
|
||||
$this->selectAction = $selectAction;
|
||||
$this->showItemExtra = $showItemExtra;
|
||||
$this->disabled = $disabled;
|
||||
|
||||
$this->style = new CheckboxStyle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the raw string of text
|
||||
*/
|
||||
public function setText(string $text) : void
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return function (CliMenu $cliMenu) {
|
||||
$this->toggle();
|
||||
$cliMenu->redraw();
|
||||
|
||||
return ($this->selectAction)($cliMenu);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return !$this->disabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not we are showing item extra
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return $this->showItemExtra;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the item is checked
|
||||
*/
|
||||
public function getChecked() : bool
|
||||
{
|
||||
return $this->checked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets checked state to true
|
||||
*/
|
||||
public function setChecked() : void
|
||||
{
|
||||
$this->checked = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets checked state to false
|
||||
*/
|
||||
public function setUnchecked() : void
|
||||
{
|
||||
$this->checked = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles checked state
|
||||
*/
|
||||
public function toggle() : void
|
||||
{
|
||||
$this->checked = !$this->checked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CheckboxStyle
|
||||
*/
|
||||
public function getStyle() : ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(CheckboxStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
}
|
|
@ -1,126 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use Assert\Assertion;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\DefaultStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class LineBreakItem implements MenuItemInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $breakChar;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $lines;
|
||||
|
||||
/**
|
||||
* @var DefaultStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(string $breakChar = ' ', int $lines = 1)
|
||||
{
|
||||
$this->breakChar = $breakChar;
|
||||
$this->lines = $lines;
|
||||
|
||||
$this->style = new DefaultStyle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
return explode(
|
||||
"\n",
|
||||
rtrim(str_repeat(sprintf(
|
||||
"%s\n",
|
||||
mb_substr(str_repeat($this->breakChar, $style->getContentWidth()), 0, $style->getContentWidth())
|
||||
), $this->lines))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
return $this->breakChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the raw string of text
|
||||
*/
|
||||
public function setText(string $text) : void
|
||||
{
|
||||
$this->breakChar = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the menu item is showing the menustyle extra value
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getLines() : int
|
||||
{
|
||||
return $this->lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DefaultStyle
|
||||
*/
|
||||
public function getStyle() : ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(DefaultStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
interface MenuItemInterface
|
||||
{
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array;
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string;
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool;
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable;
|
||||
|
||||
/**
|
||||
* Whether or not the menu item is showing the menustyle extra value
|
||||
*/
|
||||
public function showsItemExtra() : bool;
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void;
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void;
|
||||
|
||||
/**
|
||||
* Get the items style object. Can and
|
||||
* should be subclassed to provide bespoke
|
||||
* behaviour.
|
||||
*/
|
||||
public function getStyle() : ItemStyle;
|
||||
}
|
|
@ -1,157 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\SelectableStyle;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class MenuMenuItem implements MenuItemInterface, PropagatesStyles
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var CliMenu
|
||||
*/
|
||||
private $subMenu;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $showItemExtra = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $disabled;
|
||||
|
||||
/**
|
||||
* @var SelectableStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(
|
||||
string $text,
|
||||
CliMenu $subMenu,
|
||||
bool $disabled = false
|
||||
) {
|
||||
$this->text = $text;
|
||||
$this->subMenu = $subMenu;
|
||||
$this->disabled = $disabled;
|
||||
|
||||
$this->style = new SelectableStyle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the raw string of text
|
||||
*/
|
||||
public function setText(string $text) : void
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return function (CliMenu $menu) {
|
||||
$this->showSubMenu($menu);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sub menu
|
||||
*/
|
||||
public function getSubMenu() : CliMenu
|
||||
{
|
||||
return $this->subMenu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the sub menu
|
||||
*/
|
||||
public function showSubMenu(CliMenu $parentMenu) : void
|
||||
{
|
||||
$parentMenu->closeThis();
|
||||
$this->subMenu->open();
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return !$this->disabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not we are showing item extra
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return $this->showItemExtra;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SelectableStyle
|
||||
*/
|
||||
public function getStyle() : ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(SelectableStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function propagateStyles(CliMenu $parent): void
|
||||
{
|
||||
$this->getSubMenu()->importStyles($parent);
|
||||
$this->getSubMenu()->propagateStyles();
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
|
||||
interface PropagatesStyles
|
||||
{
|
||||
/**
|
||||
* Push the parents styles to any
|
||||
* child items or menus.
|
||||
*/
|
||||
public function propagateStyles(CliMenu $parent) : void;
|
||||
}
|
|
@ -1,192 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\RadioStyle;
|
||||
|
||||
class RadioItem implements MenuItemInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $selectAction;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $showItemExtra;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $disabled;
|
||||
|
||||
/**
|
||||
* The current checkbox state
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $checked = false;
|
||||
|
||||
/**
|
||||
* @var RadioStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(
|
||||
string $text,
|
||||
callable $selectAction,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) {
|
||||
$this->text = $text;
|
||||
$this->selectAction = $selectAction;
|
||||
$this->showItemExtra = $showItemExtra;
|
||||
$this->disabled = $disabled;
|
||||
|
||||
$this->style = new RadioStyle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the raw string of text
|
||||
*/
|
||||
public function setText(string $text) : void
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return function (CliMenu $cliMenu) {
|
||||
$parentItem = $cliMenu->getItemByIndex($cliMenu->getSelectedItemIndex());
|
||||
|
||||
$siblings = $parentItem instanceof SplitItem
|
||||
? $parentItem->getItems()
|
||||
: $cliMenu->getItems();
|
||||
|
||||
$filtered = array_filter(
|
||||
$siblings,
|
||||
function (MenuItemInterface $item) {
|
||||
return $item instanceof self;
|
||||
}
|
||||
);
|
||||
|
||||
array_walk(
|
||||
$filtered,
|
||||
function (RadioItem $item) {
|
||||
$item->setUnchecked();
|
||||
}
|
||||
);
|
||||
|
||||
$this->setChecked();
|
||||
$cliMenu->redraw();
|
||||
|
||||
return ($this->selectAction)($cliMenu);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return !$this->disabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not we are showing item extra
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return $this->showItemExtra;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the item is checked
|
||||
*/
|
||||
public function getChecked() : bool
|
||||
{
|
||||
return $this->checked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets checked state to true
|
||||
*/
|
||||
public function setChecked() : void
|
||||
{
|
||||
$this->checked = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets checked state to false
|
||||
*/
|
||||
public function setUnchecked() : void
|
||||
{
|
||||
$this->checked = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles checked state
|
||||
*/
|
||||
public function toggle() : void
|
||||
{
|
||||
$this->checked = !$this->checked;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RadioStyle
|
||||
*/
|
||||
public function getStyle() : ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(RadioStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
}
|
|
@ -1,132 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Util\StringUtil;
|
||||
use ncc\PhpSchool\CliMenu\Style\SelectableStyle;
|
||||
use function ncc\PhpSchool\CliMenu\Util\mapWithKeys;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class SelectableItem implements MenuItemInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $selectAction;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $showItemExtra;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $disabled;
|
||||
|
||||
/**
|
||||
* @var SelectableStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(
|
||||
string $text,
|
||||
callable $selectAction,
|
||||
bool $showItemExtra = false,
|
||||
bool $disabled = false
|
||||
) {
|
||||
$this->text = $text;
|
||||
$this->selectAction = $selectAction;
|
||||
$this->showItemExtra = $showItemExtra;
|
||||
$this->disabled = $disabled;
|
||||
|
||||
$this->style = new SelectableStyle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the raw string of text
|
||||
*/
|
||||
public function setText(string $text) : void
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return $this->selectAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return !$this->disabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not we are showing item extra
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return $this->showItemExtra;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
$this->showItemExtra = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SelectableStyle
|
||||
*/
|
||||
public function getStyle() : ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(SelectableStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\Selectable;
|
||||
use ncc\PhpSchool\CliMenu\Util\StringUtil as s;
|
||||
use function ncc\PhpSchool\CliMenu\Util\mapWithKeys;
|
||||
|
||||
class SelectableItemRenderer
|
||||
{
|
||||
public function render(MenuStyle $menuStyle, MenuItemInterface $item, bool $selected, bool $disabled) : array
|
||||
{
|
||||
$itemStyle = $item->getStyle();
|
||||
$marker = $itemStyle->getMarker($item, $selected);
|
||||
$availableTextWidth = $this->getAvailableTextWidth($menuStyle, $itemStyle);
|
||||
|
||||
return mapWithKeys(
|
||||
$this->wrapAndIndentText($marker, $item->getText(), $availableTextWidth),
|
||||
function (int $key, string $row) use ($menuStyle, $item, $availableTextWidth, $disabled) {
|
||||
$text = $disabled ? $menuStyle->getDisabledItemText($row) : $row;
|
||||
|
||||
return $key === 0 && $item->showsItemExtra()
|
||||
? $this->lineWithExtra($text, $availableTextWidth, $item->getStyle())
|
||||
: $text;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function wrapAndIndentText(string $marker, string $text, int $availableWidth) : array
|
||||
{
|
||||
return explode(
|
||||
"\n",
|
||||
s::wordwrap(
|
||||
"{$marker}{$text}",
|
||||
$availableWidth,
|
||||
sprintf("\n%s", $this->emptyString(mb_strlen($marker)))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function lineWithExtra(string $text, int $availableWidth, ItemStyle $itemStyle) : string
|
||||
{
|
||||
return sprintf(
|
||||
'%s%s %s',
|
||||
$text,
|
||||
$this->emptyString($availableWidth - s::length($text)),
|
||||
$itemStyle->getItemExtra()
|
||||
);
|
||||
}
|
||||
|
||||
public function emptyString(int $numCharacters) : string
|
||||
{
|
||||
return str_repeat(' ', $numCharacters);
|
||||
}
|
||||
|
||||
public function getAvailableTextWidth(MenuStyle $menuStyle, ItemStyle $itemStyle) : int
|
||||
{
|
||||
return $itemStyle->getDisplaysExtra()
|
||||
? $menuStyle->getContentWidth() - (mb_strlen($itemStyle->getItemExtra()) + 2)
|
||||
: $menuStyle->getContentWidth();
|
||||
}
|
||||
}
|
|
@ -1,388 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use Assert\Assertion;
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\DefaultStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\Selectable;
|
||||
use ncc\PhpSchool\CliMenu\Util\StringUtil;
|
||||
use function ncc\PhpSchool\CliMenu\Util\collect;
|
||||
use function ncc\PhpSchool\CliMenu\Util\each;
|
||||
use function ncc\PhpSchool\CliMenu\Util\mapWithKeys;
|
||||
use function ncc\PhpSchool\CliMenu\Util\max;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class SplitItem implements MenuItemInterface, PropagatesStyles
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $items = [];
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
private $selectedItemIndex;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $canBeSelected = true;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $gutter = 2;
|
||||
|
||||
/**
|
||||
* @var DefaultStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $blacklistedItems = [
|
||||
\ncc\PhpSchool\CliMenu\MenuItem\AsciiArtItem::class,
|
||||
\ncc\PhpSchool\CliMenu\MenuItem\LineBreakItem::class,
|
||||
\ncc\PhpSchool\CliMenu\MenuItem\SplitItem::class,
|
||||
];
|
||||
|
||||
public function __construct(array $items = [])
|
||||
{
|
||||
$this->addItems($items);
|
||||
$this->setDefaultSelectedItem();
|
||||
|
||||
$this->style = new DefaultStyle();
|
||||
}
|
||||
|
||||
public function getGutter() : int
|
||||
{
|
||||
return $this->gutter;
|
||||
}
|
||||
|
||||
public function setGutter(int $gutter) : void
|
||||
{
|
||||
Assertion::greaterOrEqualThan($gutter, 0);
|
||||
$this->gutter = $gutter;
|
||||
}
|
||||
|
||||
public function addItem(MenuItemInterface $item) : self
|
||||
{
|
||||
foreach (self::$blacklistedItems as $bl) {
|
||||
if ($item instanceof $bl) {
|
||||
throw new \InvalidArgumentException("Cannot add a $bl to a SplitItem");
|
||||
}
|
||||
}
|
||||
$this->items[] = $item;
|
||||
$this->setDefaultSelectedItem();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addItems(array $items) : self
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->addItem($item);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setItems(array $items) : self
|
||||
{
|
||||
$this->items = [];
|
||||
$this->addItems($items);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select default item
|
||||
*/
|
||||
private function setDefaultSelectedItem() : void
|
||||
{
|
||||
foreach ($this->items as $index => $item) {
|
||||
if ($item->canSelect()) {
|
||||
$this->canBeSelected = true;
|
||||
$this->selectedItemIndex = $index;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->canBeSelected = false;
|
||||
$this->selectedItemIndex = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
$numberOfItems = count($this->items);
|
||||
|
||||
if ($numberOfItems === 0) {
|
||||
throw new \RuntimeException(sprintf('There should be at least one item added to: %s', __CLASS__));
|
||||
}
|
||||
|
||||
if (!$selected) {
|
||||
$this->setDefaultSelectedItem();
|
||||
}
|
||||
|
||||
$largestItemExtra = $this->calculateItemExtra();
|
||||
|
||||
$length = $largestItemExtra > 0
|
||||
? floor($style->getContentWidth() / $numberOfItems) - ($largestItemExtra + 2)
|
||||
: floor($style->getContentWidth() / $numberOfItems);
|
||||
|
||||
$length -= $this->gutter;
|
||||
$length = (int) $length;
|
||||
|
||||
$missingLength = $style->getContentWidth() % $numberOfItems;
|
||||
|
||||
return $this->buildRows(
|
||||
mapWithKeys($this->items, function (int $index, MenuItemInterface $item) use ($selected, $length, $style) {
|
||||
$isSelected = $selected && $index === $this->selectedItemIndex;
|
||||
|
||||
$marker = $item->getStyle()->getMarker($item, $isSelected);
|
||||
|
||||
$itemExtra = '';
|
||||
if ($item->getStyle()->getDisplaysExtra()) {
|
||||
$itemExtraVal = $item->getStyle()->getItemExtra();
|
||||
$itemExtra = $item->showsItemExtra()
|
||||
? sprintf(' %s', $itemExtraVal)
|
||||
: sprintf(' %s', str_repeat(' ', mb_strlen($itemExtraVal)));
|
||||
}
|
||||
|
||||
return $this->buildCell(
|
||||
explode(
|
||||
"\n",
|
||||
StringUtil::wordwrap(
|
||||
sprintf('%s%s', $marker, $item->getText()),
|
||||
$length,
|
||||
sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
|
||||
)
|
||||
),
|
||||
$length,
|
||||
$style,
|
||||
$isSelected,
|
||||
$itemExtra
|
||||
);
|
||||
}),
|
||||
$missingLength,
|
||||
$length,
|
||||
$largestItemExtra
|
||||
);
|
||||
}
|
||||
|
||||
private function buildRows(array $cells, int $missingLength, int $length, int $largestItemExtra) : array
|
||||
{
|
||||
$extraPadLength = $largestItemExtra > 0 ? 2 + $largestItemExtra : 0;
|
||||
|
||||
return array_map(
|
||||
function ($i) use ($cells, $length, $missingLength, $extraPadLength) {
|
||||
return $this->buildRow($cells, $i, $length, $missingLength, $extraPadLength);
|
||||
},
|
||||
range(0, max(array_map('count', $cells)) - 1)
|
||||
);
|
||||
}
|
||||
|
||||
private function buildRow(array $cells, int $index, int $length, int $missingLength, int $extraPadLength) : string
|
||||
{
|
||||
return sprintf(
|
||||
'%s%s',
|
||||
implode(
|
||||
'',
|
||||
array_map(
|
||||
function ($cell) use ($index, $length, $extraPadLength) {
|
||||
return $cell[$index] ?? str_repeat(' ', $length + $this->gutter + $extraPadLength);
|
||||
},
|
||||
$cells
|
||||
)
|
||||
),
|
||||
str_repeat(' ', $missingLength)
|
||||
);
|
||||
}
|
||||
|
||||
private function buildCell(
|
||||
array $content,
|
||||
int $length,
|
||||
MenuStyle $style,
|
||||
bool $isSelected,
|
||||
string $itemExtra
|
||||
) : array {
|
||||
return array_map(function ($row, $index) use ($length, $style, $isSelected, $itemExtra) {
|
||||
$invertedColoursSetCode = $isSelected
|
||||
? $style->getInvertedColoursSetCode()
|
||||
: '';
|
||||
$invertedColoursUnsetCode = $isSelected
|
||||
? $style->getInvertedColoursUnsetCode()
|
||||
: '';
|
||||
|
||||
return sprintf(
|
||||
'%s%s%s%s%s%s',
|
||||
$invertedColoursSetCode,
|
||||
$row,
|
||||
str_repeat(' ', $length - mb_strlen($row)),
|
||||
$index === 0 ? $itemExtra : str_repeat(' ', mb_strlen($itemExtra)),
|
||||
$invertedColoursUnsetCode,
|
||||
str_repeat(' ', $this->gutter)
|
||||
);
|
||||
}, $content, array_keys($content));
|
||||
}
|
||||
|
||||
/**
|
||||
* Is there an item with this index and can it be
|
||||
* selected?
|
||||
*/
|
||||
public function canSelectIndex(int $index) : bool
|
||||
{
|
||||
return isset($this->items[$index]) && $this->items[$index]->canSelect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the item index which should be selected. If the item does
|
||||
* not exist then throw an exception.
|
||||
*/
|
||||
public function setSelectedItemIndex(int $index) : void
|
||||
{
|
||||
if (!isset($this->items[$index])) {
|
||||
throw new \InvalidArgumentException(sprintf('Index: "%s" does not exist', $index));
|
||||
}
|
||||
|
||||
$this->selectedItemIndex = $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currently select item index.
|
||||
* May be null in case of no selectable item.
|
||||
*/
|
||||
public function getSelectedItemIndex() : ?int
|
||||
{
|
||||
return $this->selectedItemIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currently selected item - if no items are selectable
|
||||
* then throw an exception.
|
||||
*/
|
||||
public function getSelectedItem() : MenuItemInterface
|
||||
{
|
||||
if (null === $this->selectedItemIndex) {
|
||||
throw new \RuntimeException('No item is selected');
|
||||
}
|
||||
|
||||
return $this->items[$this->selectedItemIndex];
|
||||
}
|
||||
|
||||
public function getItems() : array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
* In this case, it indicates if at least 1 item inside the SplitItem can be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return $this->canBeSelected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the menu item is showing the menustyle extra value
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Nothing to return with SplitItem
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
throw new \BadMethodCallException(sprintf('Not supported on: %s', __CLASS__));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds largest itemExtra value in items
|
||||
*/
|
||||
private function calculateItemExtra() : int
|
||||
{
|
||||
return max(array_map(
|
||||
function (MenuItemInterface $item) {
|
||||
return mb_strlen($item->getStyle()->getItemExtra());
|
||||
},
|
||||
array_filter($this->items, function (MenuItemInterface $item) {
|
||||
return $item->getStyle()->getDisplaysExtra();
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DefaultStyle
|
||||
*/
|
||||
public function getStyle(): ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(DefaultStyle $style): void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function propagateStyles(CliMenu $parent): void
|
||||
{
|
||||
collect($this->items)
|
||||
->filter(function (int $k, MenuItemInterface $item) use ($parent) {
|
||||
return $parent->getStyleLocator()->hasStyleForMenuItem($item);
|
||||
})
|
||||
->filter(function (int $k, MenuItemInterface $item) {
|
||||
return !$item->getStyle()->hasChangedFromDefaults();
|
||||
})
|
||||
->each(function (int $k, $item) use ($parent) {
|
||||
$item->setStyle(clone $parent->getItemStyleForItem($item));
|
||||
});
|
||||
|
||||
collect($this->items)
|
||||
->filter(function (int $k, MenuItemInterface $item) {
|
||||
return $item instanceof PropagatesStyles;
|
||||
})
|
||||
->each(function (int $k, $item) use ($parent) {
|
||||
$item->propagateStyles($parent);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,109 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\MenuItem;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\DefaultStyle;
|
||||
use ncc\PhpSchool\CliMenu\Style\ItemStyle;
|
||||
use ncc\PhpSchool\CliMenu\Util\StringUtil;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class StaticItem implements MenuItemInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var DefaultStyle
|
||||
*/
|
||||
private $style;
|
||||
|
||||
public function __construct(string $text)
|
||||
{
|
||||
$this->text = $text;
|
||||
|
||||
$this->style = new DefaultStyle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The output text for the item
|
||||
*/
|
||||
public function getRows(MenuStyle $style, bool $selected = false) : array
|
||||
{
|
||||
return explode("\n", StringUtil::wordwrap($this->text, $style->getContentWidth()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw string of text
|
||||
*/
|
||||
public function getText() : string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the raw string of text
|
||||
*/
|
||||
public function setText(string $text) : void
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the items callable if required
|
||||
*/
|
||||
public function getSelectAction() : ?callable
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the item be selected
|
||||
*/
|
||||
public function canSelect() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not we are showing item extra
|
||||
*/
|
||||
public function showsItemExtra() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable showing item extra
|
||||
*/
|
||||
public function showItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable showing item extra
|
||||
*/
|
||||
public function hideItemExtra() : void
|
||||
{
|
||||
//noop
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DefaultStyle
|
||||
*/
|
||||
public function getStyle() : ItemStyle
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
public function setStyle(DefaultStyle $style) : void
|
||||
{
|
||||
$this->style = $style;
|
||||
}
|
||||
}
|
852
src/ncc/ThirdParty/php-school/cli-menu/MenuStyle.php
vendored
852
src/ncc/ThirdParty/php-school/cli-menu/MenuStyle.php
vendored
|
@ -1,852 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\Exception\CannotShrinkMenuException;
|
||||
use ncc\PhpSchool\CliMenu\Terminal\TerminalFactory;
|
||||
use ncc\PhpSchool\CliMenu\Util\ColourUtil;
|
||||
use ncc\PhpSchool\CliMenu\Util\StringUtil as s;
|
||||
use ncc\PhpSchool\Terminal\Terminal;
|
||||
use Assert\Assertion;
|
||||
|
||||
//TODO: B/W fallback
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class MenuStyle
|
||||
{
|
||||
/**
|
||||
* @var Terminal
|
||||
*/
|
||||
protected $terminal;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $fg;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $bg;
|
||||
|
||||
/**
|
||||
* The width of the menu. Including borders and padding.
|
||||
* Does not include margin.
|
||||
*
|
||||
* May not be the value that was requested in the
|
||||
* circumstance that the terminal is smaller then the
|
||||
* requested width.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $width;
|
||||
|
||||
/**
|
||||
* In case the requested width is wider than the terminal
|
||||
* then we shrink the width to fit the terminal. We keep
|
||||
* the requested size in case the margins are changed and
|
||||
* we need to recalculate the width.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $requestedWidth;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $margin = 0;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $paddingTopBottom = 0;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $paddingLeftRight = 0;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $paddingTopBottomRows = [];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $contentWidth;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $itemExtra;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $displaysExtra;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $titleSeparator;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $coloursSetCode;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $invertedColoursSetCode = "\033[7m";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $invertedColoursUnsetCode = "\033[27m";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $coloursResetCode = "\033[0m";
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $borderTopWidth = 0;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $borderRightWidth = 0;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $borderBottomWidth = 0;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $borderLeftWidth = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $borderColour = 'white';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $borderTopRows = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $borderBottomRows = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $marginAuto = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $debugMode = false;
|
||||
|
||||
/**
|
||||
* Default Values
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $defaultStyleValues = [
|
||||
'fg' => 'white',
|
||||
'bg' => 'blue',
|
||||
'width' => 100,
|
||||
'paddingTopBottom' => 1,
|
||||
'paddingLeftRight' => 2,
|
||||
'margin' => 2,
|
||||
'itemExtra' => '✔',
|
||||
'displaysExtra' => false,
|
||||
'titleSeparator' => '=',
|
||||
'borderTopWidth' => 0,
|
||||
'borderRightWidth' => 0,
|
||||
'borderBottomWidth' => 0,
|
||||
'borderLeftWidth' => 0,
|
||||
'borderColour' => 'white',
|
||||
'marginAuto' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $availableForegroundColors = [
|
||||
'black' => 30,
|
||||
'red' => 31,
|
||||
'green' => 32,
|
||||
'yellow' => 33,
|
||||
'blue' => 34,
|
||||
'magenta' => 35,
|
||||
'cyan' => 36,
|
||||
'white' => 37,
|
||||
'default' => 39,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $availableBackgroundColors = [
|
||||
'black' => 40,
|
||||
'red' => 41,
|
||||
'green' => 42,
|
||||
'yellow' => 43,
|
||||
'blue' => 44,
|
||||
'magenta' => 45,
|
||||
'cyan' => 46,
|
||||
'white' => 47,
|
||||
'default' => 49,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $availableOptions = [
|
||||
'bold' => ['set' => 1, 'unset' => 22],
|
||||
'dim' => ['set' => 2, 'unset' => 22],
|
||||
'underscore' => ['set' => 4, 'unset' => 24],
|
||||
'blink' => ['set' => 5, 'unset' => 25],
|
||||
'reverse' => ['set' => 7, 'unset' => 27],
|
||||
'conceal' => ['set' => 8, 'unset' => 28]
|
||||
];
|
||||
|
||||
/**
|
||||
* Initialise style
|
||||
*/
|
||||
public function __construct(Terminal $terminal = null)
|
||||
{
|
||||
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
|
||||
|
||||
$this->fg = self::$defaultStyleValues['fg'];
|
||||
$this->bg = self::$defaultStyleValues['bg'];
|
||||
|
||||
$this->generateColoursSetCode();
|
||||
|
||||
$this->setWidth(self::$defaultStyleValues['width']);
|
||||
$this->setPaddingTopBottom(self::$defaultStyleValues['paddingTopBottom']);
|
||||
$this->setPaddingLeftRight(self::$defaultStyleValues['paddingLeftRight']);
|
||||
$this->setMargin(self::$defaultStyleValues['margin']);
|
||||
$this->setItemExtra(self::$defaultStyleValues['itemExtra']);
|
||||
$this->setDisplaysExtra(self::$defaultStyleValues['displaysExtra']);
|
||||
$this->setTitleSeparator(self::$defaultStyleValues['titleSeparator']);
|
||||
$this->setBorderTopWidth(self::$defaultStyleValues['borderTopWidth']);
|
||||
$this->setBorderRightWidth(self::$defaultStyleValues['borderRightWidth']);
|
||||
$this->setBorderBottomWidth(self::$defaultStyleValues['borderBottomWidth']);
|
||||
$this->setBorderLeftWidth(self::$defaultStyleValues['borderLeftWidth']);
|
||||
$this->setBorderColour(self::$defaultStyleValues['borderColour']);
|
||||
}
|
||||
|
||||
public function hasChangedFromDefaults() : bool
|
||||
{
|
||||
$currentValues = [
|
||||
$this->fg,
|
||||
$this->bg,
|
||||
$this->width,
|
||||
$this->paddingTopBottom,
|
||||
$this->paddingLeftRight,
|
||||
$this->margin,
|
||||
$this->itemExtra,
|
||||
$this->displaysExtra,
|
||||
$this->titleSeparator,
|
||||
$this->borderTopWidth,
|
||||
$this->borderRightWidth,
|
||||
$this->borderBottomWidth,
|
||||
$this->borderLeftWidth,
|
||||
$this->borderColour,
|
||||
$this->marginAuto,
|
||||
];
|
||||
|
||||
$defaultStyleValues = self::$defaultStyleValues;
|
||||
if ($this->width !== $this->requestedWidth) {
|
||||
$defaultStyleValues['width'] = $this->width;
|
||||
}
|
||||
|
||||
return $currentValues !== array_values($defaultStyleValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text for a disabled menu item.
|
||||
*
|
||||
* This sets the foreground colour to the ansi bright equivalent,
|
||||
* and on supported terminals, adds additional dim formatting.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDisabledItemText(string $text) : string
|
||||
{
|
||||
return sprintf(
|
||||
"\033[%sm\033[%sm%s\033[%sm\033[%sm",
|
||||
self::$availableOptions['dim']['set'],
|
||||
$this->getForegroundColourCode(true),
|
||||
$text,
|
||||
$this->getForegroundColourCode(),
|
||||
self::$availableOptions['dim']['unset']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ansi escape sequence for the foreground colour.
|
||||
*
|
||||
* @param bool $bright Whether to modify to the ansi bright variation
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getForegroundColourCode(bool $bright = false) : string
|
||||
{
|
||||
if (!ctype_digit($this->fg)) {
|
||||
$fgCode = (int)self::$availableForegroundColors[$this->fg];
|
||||
$fgCode += ($bright ? 60 : 0);
|
||||
} else {
|
||||
$fgCode = sprintf("38;5;%s", ((int)$this->fg + ($bright ? 60 : 0)));
|
||||
}
|
||||
|
||||
return (string)$fgCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ansi escape sequence for the background colour.
|
||||
*
|
||||
* @param bool $bright Whether to modify to the ansi bright variation
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getBackgroundColourCode(bool $bright = false) : string
|
||||
{
|
||||
if (!ctype_digit($this->bg)) {
|
||||
$bgCode = (int)self::$availableBackgroundColors[$this->bg];
|
||||
$bgCode += ($bright ? 60 : 0);
|
||||
} else {
|
||||
$bgCode = sprintf("48;5;%s", ((int)$this->bg + ($bright ? 60 : 0)));
|
||||
}
|
||||
|
||||
return (string)$bgCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the ansi escape sequence to set the colours
|
||||
*/
|
||||
private function generateColoursSetCode() : void
|
||||
{
|
||||
$this->coloursSetCode = sprintf(
|
||||
"\033[%s;%sm",
|
||||
$this->getForegroundColourCode(),
|
||||
$this->getBackgroundColourCode()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the colour code for Bg and Fg
|
||||
*/
|
||||
public function getColoursSetCode() : string
|
||||
{
|
||||
return $this->coloursSetCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the inverted escape sequence (used for selected elements)
|
||||
*/
|
||||
public function getInvertedColoursSetCode() : string
|
||||
{
|
||||
return $this->invertedColoursSetCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the inverted escape sequence (used for selected elements)
|
||||
*/
|
||||
public function getInvertedColoursUnsetCode() : string
|
||||
{
|
||||
return $this->invertedColoursUnsetCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the escape sequence used to reset colours to default
|
||||
*/
|
||||
public function getColoursResetCode() : string
|
||||
{
|
||||
return $this->coloursResetCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the contents width
|
||||
*
|
||||
* The content width is menu width minus borders and padding.
|
||||
*/
|
||||
protected function calculateContentWidth() : void
|
||||
{
|
||||
$this->contentWidth = $this->width
|
||||
- ($this->paddingLeftRight * 2)
|
||||
- ($this->borderRightWidth + $this->borderLeftWidth);
|
||||
|
||||
if ($this->contentWidth < 0) {
|
||||
$this->contentWidth = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public function getFg() : string
|
||||
{
|
||||
return $this->fg;
|
||||
}
|
||||
|
||||
public function setFg(string $fg, string $fallback = null) : self
|
||||
{
|
||||
$this->fg = ColourUtil::validateColour(
|
||||
$this->terminal,
|
||||
$fg,
|
||||
$fallback
|
||||
);
|
||||
$this->generateColoursSetCode();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBg() : string
|
||||
{
|
||||
return $this->bg;
|
||||
}
|
||||
|
||||
public function setBg(string $bg, string $fallback = null) : self
|
||||
{
|
||||
$this->bg = ColourUtil::validateColour(
|
||||
$this->terminal,
|
||||
$bg,
|
||||
$fallback
|
||||
);
|
||||
|
||||
$this->generateColoursSetCode();
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWidth() : int
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
public function setWidth(int $width) : self
|
||||
{
|
||||
Assertion::greaterOrEqualThan($width, 0);
|
||||
|
||||
$this->requestedWidth = $width;
|
||||
|
||||
$this->width = $this->maybeShrinkWidth($this->marginAuto ? 0 : $this->margin, $width);
|
||||
|
||||
if ($this->marginAuto) {
|
||||
$this->calculateMarginAuto($this->width);
|
||||
}
|
||||
|
||||
$this->calculateContentWidth();
|
||||
$this->generateBorderRows();
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function maybeShrinkWidth(int $margin, int $width) : int
|
||||
{
|
||||
if ($width + ($margin * 2) >= $this->terminal->getWidth()) {
|
||||
$width = $this->terminal->getWidth() - ($margin * 2);
|
||||
|
||||
if ($width <= 0) {
|
||||
throw CannotShrinkMenuException::fromMarginAndTerminalWidth($margin, $this->terminal->getWidth());
|
||||
}
|
||||
}
|
||||
|
||||
return $width;
|
||||
}
|
||||
|
||||
public function getPaddingTopBottom() : int
|
||||
{
|
||||
return $this->paddingTopBottom;
|
||||
}
|
||||
|
||||
public function getPaddingLeftRight() : int
|
||||
{
|
||||
return $this->paddingLeftRight;
|
||||
}
|
||||
|
||||
private function generatePaddingTopBottomRows() : void
|
||||
{
|
||||
if ($this->borderLeftWidth || $this->borderRightWidth) {
|
||||
$borderColour = $this->getBorderColourCode();
|
||||
} else {
|
||||
$borderColour = '';
|
||||
}
|
||||
|
||||
$paddingRow = sprintf(
|
||||
"%s%s%s%s%s%s%s%s%s%s\n",
|
||||
$this->debugMode ? $this->getDebugString($this->margin) : str_repeat(' ', $this->margin),
|
||||
$borderColour,
|
||||
str_repeat(' ', $this->borderLeftWidth),
|
||||
$this->getColoursSetCode(),
|
||||
str_repeat(' ', $this->paddingLeftRight),
|
||||
str_repeat(' ', $this->contentWidth),
|
||||
str_repeat(' ', $this->paddingLeftRight),
|
||||
$borderColour,
|
||||
str_repeat(' ', $this->borderRightWidth),
|
||||
$this->coloursResetCode
|
||||
);
|
||||
|
||||
|
||||
if ($this->debugMode && s::length($paddingRow) <= $this->terminal->getWidth()) {
|
||||
$paddingRow = substr_replace(
|
||||
$paddingRow,
|
||||
sprintf("%s\n", $this->getDebugString($this->terminal->getWidth() - (s::length($paddingRow) - 1))),
|
||||
-1
|
||||
);
|
||||
}
|
||||
|
||||
$this->paddingTopBottom = max($this->paddingTopBottom, 0);
|
||||
$this->paddingTopBottomRows = array_fill(0, $this->paddingTopBottom, $paddingRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getPaddingTopBottomRows() : array
|
||||
{
|
||||
return $this->paddingTopBottomRows;
|
||||
}
|
||||
|
||||
public function setPadding(int $topBottom, int $leftRight = null) : self
|
||||
{
|
||||
if ($leftRight === null) {
|
||||
$leftRight = $topBottom;
|
||||
}
|
||||
|
||||
$this->setPaddingTopBottom($topBottom);
|
||||
$this->setPaddingLeftRight($leftRight);
|
||||
|
||||
$this->calculateContentWidth();
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPaddingTopBottom(int $topBottom) : self
|
||||
{
|
||||
Assertion::greaterOrEqualThan($topBottom, 0);
|
||||
$this->paddingTopBottom = $topBottom;
|
||||
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPaddingLeftRight(int $leftRight) : self
|
||||
{
|
||||
Assertion::greaterOrEqualThan($leftRight, 0);
|
||||
$this->paddingLeftRight = $leftRight;
|
||||
|
||||
$this->calculateContentWidth();
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMargin() : int
|
||||
{
|
||||
return $this->margin;
|
||||
}
|
||||
|
||||
public function setMarginAuto() : self
|
||||
{
|
||||
$this->marginAuto = true;
|
||||
$this->margin = 0;
|
||||
|
||||
$this->setWidth($this->requestedWidth);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function calculateMarginAuto(int $width) : void
|
||||
{
|
||||
$this->margin = (int) floor(($this->terminal->getWidth() - ($width)) / 2);
|
||||
}
|
||||
|
||||
public function setMargin(int $margin) : self
|
||||
{
|
||||
Assertion::greaterOrEqualThan($margin, 0);
|
||||
|
||||
$this->marginAuto = false;
|
||||
$this->margin = $margin;
|
||||
|
||||
//margin + width may now exceed terminal size
|
||||
//so set width again to trigger width check + maybe resize
|
||||
$this->setWidth($this->requestedWidth);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getContentWidth() : int
|
||||
{
|
||||
return $this->contentWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get padding for right had side of content
|
||||
*/
|
||||
public function getRightHandPadding(int $contentLength) : int
|
||||
{
|
||||
$rightPadding = $this->getContentWidth() - $contentLength + $this->getPaddingLeftRight();
|
||||
|
||||
if ($rightPadding < 0) {
|
||||
$rightPadding = 0;
|
||||
}
|
||||
|
||||
return $rightPadding;
|
||||
}
|
||||
|
||||
public function setItemExtra(string $itemExtra) : self
|
||||
{
|
||||
$this->itemExtra = $itemExtra;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getItemExtra() : string
|
||||
{
|
||||
return $this->itemExtra;
|
||||
}
|
||||
|
||||
public function getDisplaysExtra() : bool
|
||||
{
|
||||
return $this->displaysExtra;
|
||||
}
|
||||
|
||||
public function setDisplaysExtra(bool $displaysExtra) : self
|
||||
{
|
||||
$this->displaysExtra = $displaysExtra;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTitleSeparator() : string
|
||||
{
|
||||
return $this->titleSeparator;
|
||||
}
|
||||
|
||||
public function setTitleSeparator(string $actionSeparator) : self
|
||||
{
|
||||
$this->titleSeparator = $actionSeparator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function generateBorderRows() : void
|
||||
{
|
||||
$borderRow = sprintf(
|
||||
"%s%s%s%s\n",
|
||||
$this->debugMode ? $this->getDebugString($this->margin) : str_repeat(' ', $this->margin),
|
||||
$this->getBorderColourCode(),
|
||||
str_repeat(' ', $this->width),
|
||||
$this->getColoursResetCode()
|
||||
);
|
||||
|
||||
if ($this->debugMode && s::length($borderRow) <= $this->terminal->getWidth()) {
|
||||
$borderRow = substr_replace(
|
||||
$borderRow,
|
||||
sprintf("%s\n", $this->getDebugString($this->terminal->getWidth() - (s::length($borderRow) - 1))),
|
||||
-1
|
||||
);
|
||||
}
|
||||
|
||||
$this->borderTopWidth = max($this->borderTopWidth, 0);
|
||||
$this->borderBottomWidth = max($this->borderBottomWidth, 0);
|
||||
|
||||
$this->borderTopRows = array_fill(0, $this->borderTopWidth, $borderRow);
|
||||
$this->borderBottomRows = array_fill(0, $this->borderBottomWidth, $borderRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBorderTopRows() : array
|
||||
{
|
||||
return $this->borderTopRows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBorderBottomRows() : array
|
||||
{
|
||||
return $this->borderBottomRows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string|null $rightWidth
|
||||
* @param int|string|null $bottomWidth
|
||||
* @param int|string|null $leftWidth
|
||||
*
|
||||
* Shorthand function to set all borders values at once
|
||||
*/
|
||||
public function setBorder(
|
||||
int $topWidth,
|
||||
$rightWidth = null,
|
||||
$bottomWidth = null,
|
||||
$leftWidth = null,
|
||||
string $colour = null
|
||||
) : self {
|
||||
if (!is_int($rightWidth)) {
|
||||
$colour = $rightWidth;
|
||||
$rightWidth = $bottomWidth = $leftWidth = $topWidth;
|
||||
} elseif (!is_int($bottomWidth)) {
|
||||
$colour = $bottomWidth;
|
||||
$bottomWidth = $topWidth;
|
||||
$leftWidth = $rightWidth;
|
||||
} elseif (!is_int($leftWidth)) {
|
||||
$colour = $leftWidth;
|
||||
$leftWidth = $rightWidth;
|
||||
}
|
||||
|
||||
$this->borderTopWidth = $topWidth;
|
||||
$this->borderRightWidth = $rightWidth;
|
||||
$this->borderBottomWidth = $bottomWidth;
|
||||
$this->borderLeftWidth = $leftWidth;
|
||||
|
||||
if (is_string($colour)) {
|
||||
$this->setBorderColour($colour);
|
||||
}
|
||||
|
||||
$this->calculateContentWidth();
|
||||
$this->generateBorderRows();
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderTopWidth(int $width) : self
|
||||
{
|
||||
$this->borderTopWidth = $width;
|
||||
|
||||
$this->generateBorderRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderRightWidth(int $width) : self
|
||||
{
|
||||
$this->borderRightWidth = $width;
|
||||
$this->calculateContentWidth();
|
||||
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderBottomWidth(int $width) : self
|
||||
{
|
||||
$this->borderBottomWidth = $width;
|
||||
|
||||
$this->generateBorderRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderLeftWidth(int $width) : self
|
||||
{
|
||||
$this->borderLeftWidth = $width;
|
||||
$this->calculateContentWidth();
|
||||
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBorderColour(string $colour, string $fallback = null) : self
|
||||
{
|
||||
$this->borderColour = ColourUtil::validateColour(
|
||||
$this->terminal,
|
||||
$colour,
|
||||
$fallback
|
||||
);
|
||||
|
||||
$this->generateBorderRows();
|
||||
$this->generatePaddingTopBottomRows();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBorderTopWidth() : int
|
||||
{
|
||||
return $this->borderTopWidth;
|
||||
}
|
||||
|
||||
public function getBorderRightWidth() : int
|
||||
{
|
||||
return $this->borderRightWidth;
|
||||
}
|
||||
|
||||
public function getBorderBottomWidth() : int
|
||||
{
|
||||
return $this->borderBottomWidth;
|
||||
}
|
||||
|
||||
public function getBorderLeftWidth() : int
|
||||
{
|
||||
return $this->borderLeftWidth;
|
||||
}
|
||||
|
||||
public function getBorderColour() : string
|
||||
{
|
||||
return $this->borderColour;
|
||||
}
|
||||
|
||||
public function getBorderColourCode() : string
|
||||
{
|
||||
if (!ctype_digit($this->borderColour)) {
|
||||
$borderColourCode = self::$availableBackgroundColors[$this->borderColour];
|
||||
} else {
|
||||
$borderColourCode = sprintf("48;5;%s", $this->borderColour);
|
||||
}
|
||||
|
||||
return sprintf("\033[%sm", $borderColourCode);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get ansi escape sequence for setting or unsetting the specified option code.
|
||||
*
|
||||
* @param string $string Option code (bold|dim|underscore|blink|reverse|conceal)
|
||||
* @param bool $set Whether to set or unset the code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOptionCode(string $string, bool $set = true): string
|
||||
{
|
||||
return sprintf("\033[%sm", self::$availableOptions[$string][$set ? 'set' : 'unset']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string of given length consisting of 0-9
|
||||
* eg $length = 15 : 012345678901234
|
||||
*/
|
||||
private function getDebugString(int $length) : string
|
||||
{
|
||||
$nums = [];
|
||||
for ($i = 0, $j = 0; $i < $length; $i++, $j++) {
|
||||
if ($j === 10) {
|
||||
$j = 0;
|
||||
}
|
||||
|
||||
$nums[] = $j;
|
||||
}
|
||||
|
||||
return implode('', $nums);
|
||||
}
|
||||
}
|
1473
src/ncc/ThirdParty/php-school/cli-menu/README.md
vendored
1473
src/ncc/ThirdParty/php-school/cli-menu/README.md
vendored
File diff suppressed because it is too large
Load diff
|
@ -1,119 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Style;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\CheckboxItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
|
||||
class CheckboxStyle implements ItemStyle
|
||||
{
|
||||
private const DEFAULT_STYLES = [
|
||||
'checkedMarker' => '[✔] ',
|
||||
'uncheckedMarker' => '[ ] ',
|
||||
'itemExtra' => '✔',
|
||||
'displaysExtra' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $checkedMarker;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $uncheckedMarker;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $itemExtra;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $displaysExtra;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->checkedMarker = self::DEFAULT_STYLES['checkedMarker'];
|
||||
$this->uncheckedMarker = self::DEFAULT_STYLES['uncheckedMarker'];
|
||||
$this->itemExtra = self::DEFAULT_STYLES['itemExtra'];
|
||||
$this->displaysExtra = self::DEFAULT_STYLES['displaysExtra'];
|
||||
}
|
||||
|
||||
public function hasChangedFromDefaults() : bool
|
||||
{
|
||||
$currentValues = [
|
||||
$this->checkedMarker,
|
||||
$this->uncheckedMarker,
|
||||
$this->itemExtra,
|
||||
$this->displaysExtra,
|
||||
];
|
||||
|
||||
return $currentValues !== array_values(self::DEFAULT_STYLES);
|
||||
}
|
||||
|
||||
public function getMarker(MenuItemInterface $item, bool $selected) : string
|
||||
{
|
||||
if (!$item instanceof CheckboxItem) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Expected an instance of: %s. Got: %s', CheckboxItem::class, get_class($item))
|
||||
);
|
||||
}
|
||||
|
||||
return $item->getChecked() ? $this->checkedMarker : $this->uncheckedMarker;
|
||||
}
|
||||
|
||||
public function getCheckedMarker() : string
|
||||
{
|
||||
return $this->checkedMarker;
|
||||
}
|
||||
|
||||
public function setCheckedMarker(string $marker) : self
|
||||
{
|
||||
$this->checkedMarker = $marker;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUncheckedMarker() : string
|
||||
{
|
||||
return $this->uncheckedMarker;
|
||||
}
|
||||
|
||||
public function setUncheckedMarker(string $marker) : self
|
||||
{
|
||||
$this->uncheckedMarker = $marker;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getItemExtra() : string
|
||||
{
|
||||
return $this->itemExtra;
|
||||
}
|
||||
|
||||
public function setItemExtra(string $itemExtra) : self
|
||||
{
|
||||
$this->itemExtra = $itemExtra;
|
||||
|
||||
// if we customise item extra, it means we most likely want to display it
|
||||
$this->setDisplaysExtra(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDisplaysExtra() : bool
|
||||
{
|
||||
return $this->displaysExtra;
|
||||
}
|
||||
|
||||
public function setDisplaysExtra(bool $displaysExtra) : self
|
||||
{
|
||||
$this->displaysExtra = $displaysExtra;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Style;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
|
||||
class DefaultStyle implements ItemStyle
|
||||
{
|
||||
public function hasChangedFromDefaults() : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getDisplaysExtra() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getItemExtra() : string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getMarker(MenuItemInterface $item, bool $isSelected) : string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Style\Exception;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
|
||||
class InvalidStyle extends \RuntimeException
|
||||
{
|
||||
public static function unregisteredStyle(string $styleClass) : self
|
||||
{
|
||||
return new self("Style class: '$styleClass' is not registered");
|
||||
}
|
||||
|
||||
public static function notSubClassOf(string $styleClass) : self
|
||||
{
|
||||
return new self("Style instance must be a subclass of: '$styleClass'");
|
||||
}
|
||||
|
||||
public static function unregisteredItem(string $itemClass) : self
|
||||
{
|
||||
return new self("Menu item: '$itemClass' does not have a registered style class");
|
||||
}
|
||||
|
||||
public static function itemAlreadyRegistered(string $itemClass) : self
|
||||
{
|
||||
return new self("Menu item: '$itemClass' already has a registered style class");
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Style;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
|
||||
interface ItemStyle
|
||||
{
|
||||
public function hasChangedFromDefaults() : bool;
|
||||
|
||||
public function getDisplaysExtra() : bool;
|
||||
|
||||
public function getItemExtra() : string;
|
||||
|
||||
public function getMarker(MenuItemInterface $menuItem, bool $isSelected) : string;
|
||||
}
|
|
@ -1,115 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Style;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\AsciiArtItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\CheckboxItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\LineBreakItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuMenuItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\RadioItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\SelectableItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\SplitItem;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\StaticItem;
|
||||
use ncc\PhpSchool\CliMenu\Style\Exception\InvalidStyle;
|
||||
use function ncc\PhpSchool\CliMenu\Util\mapWithKeys;
|
||||
|
||||
class Locator
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $itemStyleMap = [
|
||||
StaticItem::class => DefaultStyle::class,
|
||||
AsciiArtItem::class => DefaultStyle::class,
|
||||
LineBreakItem::class => DefaultStyle::class,
|
||||
SplitItem::class => DefaultStyle::class,
|
||||
SelectableItem::class => SelectableStyle::class,
|
||||
MenuMenuItem::class => SelectableStyle::class,
|
||||
CheckboxItem::class => CheckboxStyle::class,
|
||||
RadioItem::class => RadioStyle::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $styles;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->styles = [
|
||||
DefaultStyle::class => new DefaultStyle(),
|
||||
SelectableStyle::class => new SelectableStyle(),
|
||||
CheckboxStyle::class => new CheckboxStyle(),
|
||||
RadioStyle::class => new RadioStyle()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* For each of our unmodified item styles, we replace ours with the versions
|
||||
* from the given style locator.
|
||||
*
|
||||
* @param Locator $other
|
||||
*/
|
||||
public function importFrom(self $other) : void
|
||||
{
|
||||
$this->styles = mapWithKeys(
|
||||
$this->styles,
|
||||
function ($styleClass, ItemStyle $instance) use ($other) {
|
||||
return $instance->hasChangedFromDefaults()
|
||||
? $instance
|
||||
: $other->getStyle($styleClass);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function getStyle(string $styleClass) : ItemStyle
|
||||
{
|
||||
if (!isset($this->styles[$styleClass])) {
|
||||
throw InvalidStyle::unregisteredStyle($styleClass);
|
||||
}
|
||||
|
||||
return $this->styles[$styleClass];
|
||||
}
|
||||
|
||||
public function setStyle(ItemStyle $itemStyle, string $styleClass) : void
|
||||
{
|
||||
if (!isset($this->styles[$styleClass])) {
|
||||
throw InvalidStyle::unregisteredStyle($styleClass);
|
||||
}
|
||||
|
||||
if (!$itemStyle instanceof $styleClass) {
|
||||
throw InvalidStyle::notSubClassOf($styleClass);
|
||||
}
|
||||
|
||||
$this->styles[$styleClass] = $itemStyle;
|
||||
}
|
||||
|
||||
public function hasStyleForMenuItem(MenuItemInterface $item) : bool
|
||||
{
|
||||
return isset($this->itemStyleMap[get_class($item)]);
|
||||
}
|
||||
|
||||
public function getStyleForMenuItem(MenuItemInterface $item) : ItemStyle
|
||||
{
|
||||
if (!isset($this->itemStyleMap[get_class($item)])) {
|
||||
throw InvalidStyle::unregisteredItem(get_class($item));
|
||||
}
|
||||
|
||||
$styleClass = $this->itemStyleMap[get_class($item)];
|
||||
|
||||
return $this->getStyle($styleClass);
|
||||
}
|
||||
|
||||
public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : void
|
||||
{
|
||||
if (isset($this->itemStyleMap[$itemClass])) {
|
||||
throw InvalidStyle::itemAlreadyRegistered($itemClass);
|
||||
}
|
||||
|
||||
$this->itemStyleMap[$itemClass] = get_class($itemStyle);
|
||||
$this->styles[get_class($itemStyle)] = $itemStyle;
|
||||
}
|
||||
}
|
|
@ -1,119 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Style;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\RadioItem;
|
||||
|
||||
class RadioStyle implements ItemStyle
|
||||
{
|
||||
private const DEFAULT_STYLES = [
|
||||
'checkedMarker' => '[●] ',
|
||||
'uncheckedMarker' => '[○] ',
|
||||
'itemExtra' => '✔',
|
||||
'displaysExtra' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $checkedMarker;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $uncheckedMarker;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $itemExtra;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $displaysExtra;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->checkedMarker = self::DEFAULT_STYLES['checkedMarker'];
|
||||
$this->uncheckedMarker = self::DEFAULT_STYLES['uncheckedMarker'];
|
||||
$this->itemExtra = self::DEFAULT_STYLES['itemExtra'];
|
||||
$this->displaysExtra = self::DEFAULT_STYLES['displaysExtra'];
|
||||
}
|
||||
|
||||
public function hasChangedFromDefaults() : bool
|
||||
{
|
||||
$currentValues = [
|
||||
$this->checkedMarker,
|
||||
$this->uncheckedMarker,
|
||||
$this->itemExtra,
|
||||
$this->displaysExtra,
|
||||
];
|
||||
|
||||
return $currentValues !== array_values(self::DEFAULT_STYLES);
|
||||
}
|
||||
|
||||
public function getMarker(MenuItemInterface $item, bool $selected) : string
|
||||
{
|
||||
if (!$item instanceof RadioItem) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Expected an instance of: %s. Got: %s', RadioItem::class, get_class($item))
|
||||
);
|
||||
}
|
||||
|
||||
return $item->getChecked() ? $this->checkedMarker : $this->uncheckedMarker;
|
||||
}
|
||||
|
||||
public function getCheckedMarker() : string
|
||||
{
|
||||
return $this->checkedMarker;
|
||||
}
|
||||
|
||||
public function setCheckedMarker(string $marker) : self
|
||||
{
|
||||
$this->checkedMarker = $marker;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUncheckedMarker() : string
|
||||
{
|
||||
return $this->uncheckedMarker;
|
||||
}
|
||||
|
||||
public function setUncheckedMarker(string $marker) : self
|
||||
{
|
||||
$this->uncheckedMarker = $marker;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getItemExtra() : string
|
||||
{
|
||||
return $this->itemExtra;
|
||||
}
|
||||
|
||||
public function setItemExtra(string $itemExtra) : self
|
||||
{
|
||||
$this->itemExtra = $itemExtra;
|
||||
|
||||
// if we customise item extra, it means we most likely want to display it
|
||||
$this->setDisplaysExtra(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDisplaysExtra() : bool
|
||||
{
|
||||
return $this->displaysExtra;
|
||||
}
|
||||
|
||||
public function setDisplaysExtra(bool $displaysExtra) : self
|
||||
{
|
||||
$this->displaysExtra = $displaysExtra;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,112 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Style;
|
||||
|
||||
use ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface;
|
||||
|
||||
class SelectableStyle implements ItemStyle
|
||||
{
|
||||
private const DEFAULT_STYLES = [
|
||||
'selectedMarker' => '● ',
|
||||
'unselectedMarker' => '○ ',
|
||||
'itemExtra' => '✔',
|
||||
'displaysExtra' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $selectedMarker;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $unselectedMarker;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $itemExtra;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $displaysExtra;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->selectedMarker = self::DEFAULT_STYLES['selectedMarker'];
|
||||
$this->unselectedMarker = self::DEFAULT_STYLES['unselectedMarker'];
|
||||
$this->itemExtra = self::DEFAULT_STYLES['itemExtra'];
|
||||
$this->displaysExtra = self::DEFAULT_STYLES['displaysExtra'];
|
||||
}
|
||||
|
||||
public function hasChangedFromDefaults() : bool
|
||||
{
|
||||
$currentValues = [
|
||||
$this->selectedMarker,
|
||||
$this->unselectedMarker,
|
||||
$this->itemExtra,
|
||||
$this->displaysExtra,
|
||||
];
|
||||
|
||||
return $currentValues !== array_values(self::DEFAULT_STYLES);
|
||||
}
|
||||
|
||||
public function getMarker(MenuItemInterface $item, bool $selected) : string
|
||||
{
|
||||
return $selected ? $this->selectedMarker : $this->unselectedMarker;
|
||||
}
|
||||
|
||||
public function getSelectedMarker() : string
|
||||
{
|
||||
return $this->selectedMarker;
|
||||
}
|
||||
|
||||
public function setSelectedMarker(string $marker) : self
|
||||
{
|
||||
$this->selectedMarker = $marker;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUnselectedMarker() : string
|
||||
{
|
||||
return $this->unselectedMarker;
|
||||
}
|
||||
|
||||
public function setUnselectedMarker(string $marker) : self
|
||||
{
|
||||
$this->unselectedMarker = $marker;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getItemExtra() : string
|
||||
{
|
||||
return $this->itemExtra;
|
||||
}
|
||||
|
||||
public function setItemExtra(string $itemExtra) : self
|
||||
{
|
||||
$this->itemExtra = $itemExtra;
|
||||
|
||||
// if we customise item extra, it means we most likely want to display it
|
||||
$this->setDisplaysExtra(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDisplaysExtra() : bool
|
||||
{
|
||||
return $this->displaysExtra;
|
||||
}
|
||||
|
||||
public function setDisplaysExtra(bool $displaysExtra) : self
|
||||
{
|
||||
$this->displaysExtra = $displaysExtra;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Terminal;
|
||||
|
||||
use ncc\PhpSchool\Terminal\IO\ResourceInputStream;
|
||||
use ncc\PhpSchool\Terminal\IO\ResourceOutputStream;
|
||||
use ncc\PhpSchool\Terminal\Terminal;
|
||||
use ncc\PhpSchool\Terminal\UnixTerminal;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class TerminalFactory
|
||||
{
|
||||
public static function fromSystem() : Terminal
|
||||
{
|
||||
return new UnixTerminal(new ResourceInputStream, new ResourceOutputStream);
|
||||
}
|
||||
}
|
113
src/ncc/ThirdParty/php-school/cli-menu/UPGRADE.md
vendored
113
src/ncc/ThirdParty/php-school/cli-menu/UPGRADE.md
vendored
|
@ -1,113 +0,0 @@
|
|||
# Upgrade Documentation
|
||||
|
||||
This document serves as a reference to upgrade your current cli-menu installation if improvements, deprecations
|
||||
or backwards compatibility (BC) breakages occur.
|
||||
|
||||
## 4.0.0
|
||||
|
||||
### BC breaks
|
||||
|
||||
* Trait `ncc\PhpSchool\CliMenu\MenuItem\SelectableTrait` was removed. Copy the old code into your menu item
|
||||
if you need it.
|
||||
* Methods `ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder#setUnselectedMarker()` & `ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder#setSelectedMarker()` were removed.
|
||||
Customise markers on the individual item styles:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder;
|
||||
use ncc\PhpSchool\CliMenu\Style\SelectableStyle;
|
||||
|
||||
$menu = (new CliMenuBuilder)
|
||||
->modifySelectableStyle(function (SelectableStyle $style) {
|
||||
$style->setUnselectedMarker('❅ ')
|
||||
->setSelectedMarker('✏ ')
|
||||
|
||||
// disable unselected marker
|
||||
->setUnselectedMarker('')
|
||||
;
|
||||
})
|
||||
->build();
|
||||
```
|
||||
* Method getStyle() was added to interface ncc\PhpSchool\CliMenu\MenuItem\MenuItemInterface. Items must now implement this
|
||||
method. For selectable items use `\ncc\PhpSchool\CliMenu\Style\SelectableStyle` or a subclass of. For static items use
|
||||
`\ncc\PhpSchool\CliMenu\Style\DefaultStyle` or a subclass of.
|
||||
* `ncc\PhpSchool\CliMenu\MenuStyle` marker methods have been removed. If you were using these directly. Operate on the item
|
||||
style object instead.
|
||||
|
||||
## 3.0.0
|
||||
|
||||
### BC breaks
|
||||
|
||||
* Class `ncc\PhpSchool\CliMenu\CliMenuBuilder` has been moved, use
|
||||
`ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder` instead. Please migrate to the new namespace.
|
||||
* `ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder#addSubMenu` now takes a text and a closure used to configure the submenu. The callback
|
||||
invoked with a new instance of `ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder` as a parameter. `addSubMenu` now returns itself instead of
|
||||
the sub menu `ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder`. See below for upgrade example.
|
||||
* Removed `ncc\PhpSchool\CliMenu\Terminal` namespace, the code has been migrated to the `php-school/terminal` package and is
|
||||
largely modified.
|
||||
* Removed methods `setTerminal`, `getSubMenu`, `getMenuStyle` and `end` from `ncc\PhpSchool\CliMenu\CliMenuBuilder`.
|
||||
* Removed static method `getDefaultStyleValues` on `ncc\PhpSchool\CliMenu\MenuStyle`.
|
||||
|
||||
|
||||
#### Migrating to new `addSubMenu` method in `CliMenuBuilder`
|
||||
|
||||
Previous code:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use ncc\PhpSchool\CliMenu\CliMenuBuilder;
|
||||
|
||||
require_once(__DIR__ . '/../vendor/autoload.php');
|
||||
|
||||
$itemCallable = function (CliMenu $menu) {
|
||||
echo $menu->getSelectedItem()->getText();
|
||||
};
|
||||
|
||||
$menu = (new CliMenuBuilder)
|
||||
->setTitle('CLI Menu')
|
||||
->addItem('First Item', $itemCallable)
|
||||
->addLineBreak('-')
|
||||
->addSubMenu('Options')
|
||||
->setTitle('CLI Menu > Options')
|
||||
->addItem('First option', function (CliMenu $menu) {
|
||||
echo sprintf('Executing option: %s', $menu->getSelectedItem()->getText());
|
||||
})
|
||||
->addLineBreak('-')
|
||||
->end()
|
||||
->build();
|
||||
|
||||
$menu->open();
|
||||
```
|
||||
|
||||
Would now become:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use ncc\PhpSchool\CliMenu\CliMenu;
|
||||
use \ncc\PhpSchool\CliMenu\Builder\CliMenuBuilder;
|
||||
|
||||
require_once(__DIR__ . '/../vendor/autoload.php');
|
||||
|
||||
$itemCallable = function (CliMenu $menu) {
|
||||
echo $menu->getSelectedItem()->getText();
|
||||
};
|
||||
|
||||
$menu = (new CliMenuBuilder)
|
||||
->setTitle('CLI Menu')
|
||||
->addItem('First Item', $itemCallable)
|
||||
->addLineBreak('-')
|
||||
->addSubMenu('Options', function (CliMenuBuilder $b) {
|
||||
$b->setTitle('CLI Menu > Options')
|
||||
->addItem('First option', function (CliMenu $menu) {
|
||||
echo sprintf('Executing option: %s', $menu->getSelectedItem()->getText());
|
||||
})
|
||||
->addLineBreak('-');
|
||||
})
|
||||
->build();
|
||||
|
||||
$menu->open();
|
||||
```
|
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Util;
|
||||
|
||||
function mapWithKeys(array $array, callable $callback) : array
|
||||
{
|
||||
$arr = array_combine(
|
||||
array_keys($array),
|
||||
array_map($callback, array_keys($array), $array)
|
||||
);
|
||||
|
||||
assert(is_array($arr));
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
function filter(array $array, callable $callback) : array
|
||||
{
|
||||
return array_filter($array, function ($v, $k) use ($callback) {
|
||||
return $callback($k, $v);
|
||||
}, ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
|
||||
function each(array $array, callable $callback) : void
|
||||
{
|
||||
foreach ($array as $k => $v) {
|
||||
$callback($k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
function max(array $items) : int
|
||||
{
|
||||
return count($items) > 0 ? \max($items) : 0;
|
||||
}
|
||||
|
||||
function collect(array $items) : Collection
|
||||
{
|
||||
return new Collection($items);
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Util;
|
||||
|
||||
class Collection
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $items;
|
||||
|
||||
public function __construct(array $items)
|
||||
{
|
||||
$this->items = $items;
|
||||
}
|
||||
|
||||
public function map(callable $cb) : self
|
||||
{
|
||||
return new self(mapWithKeys($this->items, $cb));
|
||||
}
|
||||
|
||||
public function filter(callable $cb) : self
|
||||
{
|
||||
return new self(filter($this->items, $cb));
|
||||
}
|
||||
|
||||
public function values() : self
|
||||
{
|
||||
return new self(array_values($this->items));
|
||||
}
|
||||
|
||||
public function each(callable $cb) : self
|
||||
{
|
||||
each($this->items, $cb);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function all() : array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
}
|
|
@ -1,334 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Util;
|
||||
|
||||
use ncc\Assert\Assertion;
|
||||
use ncc\PhpSchool\Terminal\Terminal;
|
||||
|
||||
class ColourUtil
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $defaultColoursNames = [
|
||||
'black',
|
||||
'red',
|
||||
'green',
|
||||
'yellow',
|
||||
'blue',
|
||||
'magenta',
|
||||
'cyan',
|
||||
'white',
|
||||
'default',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $coloursMap = [
|
||||
0 => 'black',
|
||||
1 => 'red',
|
||||
2 => 'green',
|
||||
3 => 'yellow',
|
||||
4 => 'blue',
|
||||
5 => 'magenta',
|
||||
6 => 'cyan',
|
||||
7 => 'white',
|
||||
8 => 'black',
|
||||
9 => 'red',
|
||||
10 => 'green',
|
||||
11 => 'yellow',
|
||||
12 => 'blue',
|
||||
13 => 'magenta',
|
||||
14 => 'cyan',
|
||||
15 => 'white',
|
||||
16 => 'black',
|
||||
17 => 'blue',
|
||||
18 => 'blue',
|
||||
19 => 'blue',
|
||||
20 => 'blue',
|
||||
21 => 'blue',
|
||||
22 => 'green',
|
||||
23 => 'cyan',
|
||||
24 => 'cyan',
|
||||
25 => 'blue',
|
||||
26 => 'blue',
|
||||
27 => 'blue',
|
||||
28 => 'green',
|
||||
29 => 'green',
|
||||
30 => 'cyan',
|
||||
31 => 'cyan',
|
||||
32 => 'blue',
|
||||
33 => 'blue',
|
||||
34 => 'green',
|
||||
35 => 'green',
|
||||
36 => 'green',
|
||||
37 => 'cyan',
|
||||
38 => 'cyan',
|
||||
39 => 'cyan',
|
||||
40 => 'green',
|
||||
41 => 'green',
|
||||
42 => 'green',
|
||||
43 => 'cyan',
|
||||
44 => 'cyan',
|
||||
45 => 'cyan',
|
||||
46 => 'green',
|
||||
47 => 'green',
|
||||
48 => 'green',
|
||||
49 => 'green',
|
||||
50 => 'cyan',
|
||||
51 => 'cyan',
|
||||
52 => 'red',
|
||||
53 => 'blue',
|
||||
54 => 'blue',
|
||||
55 => 'blue',
|
||||
56 => 'blue',
|
||||
57 => 'blue',
|
||||
58 => 'yellow',
|
||||
59 => 'black',
|
||||
60 => 'blue',
|
||||
61 => 'blue',
|
||||
62 => 'blue',
|
||||
63 => 'blue',
|
||||
64 => 'green',
|
||||
65 => 'green',
|
||||
66 => 'cyan',
|
||||
67 => 'cyan',
|
||||
68 => 'blue',
|
||||
69 => 'blue',
|
||||
70 => 'green',
|
||||
71 => 'green',
|
||||
72 => 'green',
|
||||
73 => 'cyan',
|
||||
74 => 'cyan',
|
||||
75 => 'cyan',
|
||||
76 => 'green',
|
||||
77 => 'green',
|
||||
78 => 'green',
|
||||
79 => 'green',
|
||||
80 => 'cyan',
|
||||
81 => 'cyan',
|
||||
82 => 'green',
|
||||
83 => 'green',
|
||||
84 => 'green',
|
||||
85 => 'green',
|
||||
86 => 'cyan',
|
||||
87 => 'cyan',
|
||||
88 => 'red',
|
||||
89 => 'magenta',
|
||||
90 => 'magenta',
|
||||
91 => 'magenta',
|
||||
92 => 'blue',
|
||||
93 => 'blue',
|
||||
94 => 'yellow',
|
||||
95 => 'red',
|
||||
96 => 'magenta',
|
||||
97 => 'magenta',
|
||||
98 => 'blue',
|
||||
99 => 'blue',
|
||||
100 => 'yellow',
|
||||
101 => 'yellow',
|
||||
102 => 'white',
|
||||
103 => 'blue',
|
||||
104 => 'blue',
|
||||
105 => 'blue',
|
||||
106 => 'green',
|
||||
107 => 'green',
|
||||
108 => 'green',
|
||||
109 => 'cyan',
|
||||
110 => 'cyan',
|
||||
111 => 'cyan',
|
||||
112 => 'green',
|
||||
113 => 'green',
|
||||
114 => 'green',
|
||||
115 => 'green',
|
||||
116 => 'cyan',
|
||||
117 => 'cyan',
|
||||
118 => 'green',
|
||||
119 => 'green',
|
||||
120 => 'green',
|
||||
121 => 'green',
|
||||
122 => 'green',
|
||||
123 => 'cyan',
|
||||
124 => 'red',
|
||||
125 => 'magenta',
|
||||
126 => 'magenta',
|
||||
127 => 'magenta',
|
||||
128 => 'magenta',
|
||||
129 => 'magenta',
|
||||
130 => 'red',
|
||||
131 => 'red',
|
||||
132 => 'magenta',
|
||||
133 => 'magenta',
|
||||
134 => 'magenta',
|
||||
135 => 'magenta',
|
||||
136 => 'yellow',
|
||||
137 => 'red',
|
||||
138 => 'red',
|
||||
139 => 'magenta',
|
||||
140 => 'magenta',
|
||||
141 => 'magenta',
|
||||
142 => 'yellow',
|
||||
143 => 'yellow',
|
||||
144 => 'yellow',
|
||||
145 => 'white',
|
||||
146 => 'white',
|
||||
147 => 'white',
|
||||
148 => 'yellow',
|
||||
149 => 'green',
|
||||
150 => 'green',
|
||||
151 => 'green',
|
||||
152 => 'cyan',
|
||||
153 => 'white',
|
||||
154 => 'green',
|
||||
155 => 'green',
|
||||
156 => 'green',
|
||||
157 => 'green',
|
||||
158 => 'green',
|
||||
159 => 'cyan',
|
||||
160 => 'red',
|
||||
161 => 'magenta',
|
||||
162 => 'magenta',
|
||||
163 => 'magenta',
|
||||
164 => 'magenta',
|
||||
165 => 'magenta',
|
||||
166 => 'red',
|
||||
167 => 'red',
|
||||
168 => 'magenta',
|
||||
169 => 'magenta',
|
||||
170 => 'magenta',
|
||||
171 => 'magenta',
|
||||
172 => 'red',
|
||||
173 => 'red',
|
||||
174 => 'red',
|
||||
175 => 'magenta',
|
||||
176 => 'magenta',
|
||||
177 => 'magenta',
|
||||
178 => 'yellow',
|
||||
179 => 'yellow',
|
||||
180 => 'white',
|
||||
181 => 'white',
|
||||
182 => 'magenta',
|
||||
183 => 'magenta',
|
||||
184 => 'yellow',
|
||||
185 => 'yellow',
|
||||
186 => 'yellow',
|
||||
187 => 'yellow',
|
||||
188 => 'white',
|
||||
189 => 'white',
|
||||
190 => 'yellow',
|
||||
191 => 'yellow',
|
||||
192 => 'green',
|
||||
193 => 'green',
|
||||
194 => 'green',
|
||||
195 => 'cyan',
|
||||
196 => 'red',
|
||||
197 => 'red',
|
||||
198 => 'magenta',
|
||||
199 => 'magenta',
|
||||
200 => 'magenta',
|
||||
201 => 'magenta',
|
||||
202 => 'red',
|
||||
203 => 'red',
|
||||
204 => 'magenta',
|
||||
205 => 'magenta',
|
||||
206 => 'magenta',
|
||||
207 => 'magenta',
|
||||
208 => 'red',
|
||||
209 => 'red',
|
||||
210 => 'red',
|
||||
211 => 'magenta',
|
||||
212 => 'magenta',
|
||||
213 => 'magenta',
|
||||
214 => 'red',
|
||||
215 => 'white',
|
||||
216 => 'red',
|
||||
217 => 'red',
|
||||
218 => 'magenta',
|
||||
219 => 'magenta',
|
||||
220 => 'yellow',
|
||||
221 => 'yellow',
|
||||
222 => 'yellow',
|
||||
223 => 'white',
|
||||
224 => 'white',
|
||||
225 => 'magenta',
|
||||
226 => 'yellow',
|
||||
227 => 'yellow',
|
||||
228 => 'yellow',
|
||||
229 => 'yellow',
|
||||
230 => 'yellow',
|
||||
231 => 'white',
|
||||
232 => 'black',
|
||||
233 => 'black',
|
||||
234 => 'black',
|
||||
235 => 'black',
|
||||
236 => 'black',
|
||||
237 => 'black',
|
||||
238 => 'black',
|
||||
239 => 'black',
|
||||
240 => 'black',
|
||||
241 => 'black',
|
||||
242 => 'black',
|
||||
243 => 'black',
|
||||
244 => 'white',
|
||||
245 => 'white',
|
||||
246 => 'white',
|
||||
247 => 'white',
|
||||
248 => 'white',
|
||||
249 => 'white',
|
||||
250 => 'white',
|
||||
251 => 'white',
|
||||
252 => 'white',
|
||||
253 => 'white',
|
||||
254 => 'white',
|
||||
255 => 'white',
|
||||
];
|
||||
|
||||
public static function getDefaultColourNames() : array
|
||||
{
|
||||
return self::$defaultColoursNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple function to transform a 8-bit (256 colours) colour code
|
||||
* to one of the default 8 colors available in the terminal
|
||||
*/
|
||||
public static function map256To8(int $colourCode) : string
|
||||
{
|
||||
if (!isset(self::$coloursMap[$colourCode])) {
|
||||
throw new \InvalidArgumentException('Invalid colour code');
|
||||
}
|
||||
|
||||
return self::$coloursMap[$colourCode];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if $colour exists
|
||||
* If it's a 256-colours code and $terminal doesn't support it, returns a fallback value
|
||||
*/
|
||||
public static function validateColour(Terminal $terminal, string $colour, string $fallback = null) : string
|
||||
{
|
||||
if (!ctype_digit($colour)) {
|
||||
return self::validateColourName($colour);
|
||||
}
|
||||
|
||||
Assertion::between($colour, 0, 255, 'Invalid colour code');
|
||||
|
||||
if ($terminal->getColourSupport() >= 256) {
|
||||
return $colour;
|
||||
}
|
||||
|
||||
if ($fallback !== null) {
|
||||
return self::validateColourName($fallback);
|
||||
}
|
||||
|
||||
return static::map256To8((int) $colour);
|
||||
}
|
||||
|
||||
private static function validateColourName(string $colourName) : string
|
||||
{
|
||||
Assertion::inArray($colourName, static::getDefaultColourNames());
|
||||
return $colourName;
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ncc\PhpSchool\CliMenu\Util;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
*/
|
||||
class StringUtil
|
||||
{
|
||||
/**
|
||||
* Minimal multi-byte wordwrap implementation
|
||||
* which also takes break length into consideration
|
||||
*/
|
||||
public static function wordwrap(string $string, int $width, string $break = "\n") : string
|
||||
{
|
||||
return implode(
|
||||
$break,
|
||||
array_map(function (string $line) use ($width, $break) {
|
||||
$line = rtrim($line);
|
||||
if (mb_strlen($line) <= $width) {
|
||||
return $line;
|
||||
}
|
||||
|
||||
$words = explode(' ', $line);
|
||||
$line = '';
|
||||
$actual = '';
|
||||
foreach ($words as $word) {
|
||||
if (mb_strlen($actual . $word) <= $width) {
|
||||
$actual .= $word . ' ';
|
||||
} else {
|
||||
if ($actual !== '') {
|
||||
$line .= rtrim($actual) . $break;
|
||||
}
|
||||
$actual = $word . ' ';
|
||||
}
|
||||
}
|
||||
return $line . trim($actual);
|
||||
}, explode("\n", $string))
|
||||
);
|
||||
}
|
||||
|
||||
public static function stripAnsiEscapeSequence(string $str) : string
|
||||
{
|
||||
return (string) preg_replace('/\x1b[^m]*m/', '', $str);
|
||||
}
|
||||
|
||||
public static function length(string $str, bool $ignoreAnsiEscapeSequence = true) : int
|
||||
{
|
||||
return mb_strlen($ignoreAnsiEscapeSequence ? self::stripAnsiEscapeSequence($str) : $str);
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal\Exception;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class NotInteractiveTerminal extends \RuntimeException
|
||||
{
|
||||
public static function inputNotInteractive() : self
|
||||
{
|
||||
return new self('Input stream is not interactive (non TTY)');
|
||||
}
|
||||
|
||||
public static function outputNotInteractive() : self
|
||||
{
|
||||
return new self('Output stream is not interactive (non TTY)');
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal\IO;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class BufferedOutput implements OutputStream
|
||||
{
|
||||
private $buffer = '';
|
||||
|
||||
public function write(string $buffer): void
|
||||
{
|
||||
$this->buffer .= $buffer;
|
||||
}
|
||||
|
||||
public function fetch(bool $clean = true) : string
|
||||
{
|
||||
$buffer = $this->buffer;
|
||||
|
||||
if ($clean) {
|
||||
$this->buffer = '';
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
public function __toString() : string
|
||||
{
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the stream is connected to an interactive terminal
|
||||
*/
|
||||
public function isInteractive() : bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal\IO;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
interface InputStream
|
||||
{
|
||||
/**
|
||||
* Callback should be called with the number of bytes requested
|
||||
* when ready.
|
||||
*/
|
||||
public function read(int $numBytes, callable $callback) : void;
|
||||
|
||||
/**
|
||||
* Whether the stream is connected to an interactive terminal
|
||||
*/
|
||||
public function isInteractive() : bool;
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal\IO;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
interface OutputStream
|
||||
{
|
||||
/**
|
||||
* Write the buffer to the stream
|
||||
*/
|
||||
public function write(string $buffer) : void;
|
||||
|
||||
/**
|
||||
* Whether the stream is connected to an interactive terminal
|
||||
*/
|
||||
public function isInteractive() : bool;
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal\IO;
|
||||
|
||||
use function is_resource;
|
||||
use function get_resource_type;
|
||||
use function stream_get_meta_data;
|
||||
use function strpos;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class ResourceInputStream implements InputStream
|
||||
{
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
private $stream;
|
||||
|
||||
public function __construct($stream = STDIN)
|
||||
{
|
||||
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
|
||||
throw new \InvalidArgumentException('Expected a valid stream');
|
||||
}
|
||||
|
||||
$meta = stream_get_meta_data($stream);
|
||||
if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {
|
||||
throw new \InvalidArgumentException('Expected a readable stream');
|
||||
}
|
||||
|
||||
$this->stream = $stream;
|
||||
}
|
||||
|
||||
public function read(int $numBytes, callable $callback) : void
|
||||
{
|
||||
$buffer = fread($this->stream, $numBytes);
|
||||
$callback($buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the stream is connected to an interactive terminal
|
||||
*/
|
||||
public function isInteractive() : bool
|
||||
{
|
||||
return posix_isatty($this->stream);
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal\IO;
|
||||
|
||||
use function is_resource;
|
||||
use function get_resource_type;
|
||||
use function stream_get_meta_data;
|
||||
use function strpos;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class ResourceOutputStream implements OutputStream
|
||||
{
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
private $stream;
|
||||
|
||||
public function __construct($stream = STDOUT)
|
||||
{
|
||||
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
|
||||
throw new \InvalidArgumentException('Expected a valid stream');
|
||||
}
|
||||
|
||||
$meta = stream_get_meta_data($stream);
|
||||
if (strpos($meta['mode'], 'r') !== false && strpos($meta['mode'], '+') === false) {
|
||||
throw new \InvalidArgumentException('Expected a writable stream');
|
||||
}
|
||||
|
||||
$this->stream = $stream;
|
||||
}
|
||||
|
||||
public function write(string $buffer): void
|
||||
{
|
||||
fwrite($this->stream, $buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the stream is connected to an interactive terminal
|
||||
*/
|
||||
public function isInteractive() : bool
|
||||
{
|
||||
return posix_isatty($this->stream);
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal;
|
||||
|
||||
use function in_array;
|
||||
|
||||
/**
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class InputCharacter
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $data;
|
||||
|
||||
public const UP = 'UP';
|
||||
public const DOWN = 'DOWN';
|
||||
public const RIGHT = 'RIGHT';
|
||||
public const LEFT = 'LEFT';
|
||||
public const CTRLA = 'CTRLA';
|
||||
public const CTRLB = 'CTRLB';
|
||||
public const CTRLE = 'CTRLE';
|
||||
public const CTRLF = 'CTRLF';
|
||||
public const BACKSPACE = 'BACKSPACE';
|
||||
public const CTRLW = 'CTRLW';
|
||||
public const ENTER = 'ENTER';
|
||||
public const TAB = 'TAB';
|
||||
public const ESC = 'ESC';
|
||||
|
||||
private static $controls = [
|
||||
"\033[A" => self::UP,
|
||||
"\033[B" => self::DOWN,
|
||||
"\033[C" => self::RIGHT,
|
||||
"\033[D" => self::LEFT,
|
||||
"\033OA" => self::UP,
|
||||
"\033OB" => self::DOWN,
|
||||
"\033OC" => self::RIGHT,
|
||||
"\033OD" => self::LEFT,
|
||||
"\001" => self::CTRLA,
|
||||
"\002" => self::CTRLB,
|
||||
"\005" => self::CTRLE,
|
||||
"\006" => self::CTRLF,
|
||||
"\010" => self::BACKSPACE,
|
||||
"\177" => self::BACKSPACE,
|
||||
"\027" => self::CTRLW,
|
||||
"\n" => self::ENTER,
|
||||
"\t" => self::TAB,
|
||||
"\e" => self::ESC,
|
||||
];
|
||||
|
||||
public function __construct(string $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function isHandledControl() : bool
|
||||
{
|
||||
return isset(static::$controls[$this->data]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this character a control sequence?
|
||||
*/
|
||||
public function isControl() : bool
|
||||
{
|
||||
return preg_match('/[\x00-\x1F\x7F]/', $this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this character a normal character?
|
||||
*/
|
||||
public function isNotControl() : bool
|
||||
{
|
||||
return ! $this->isControl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw character or control sequence
|
||||
*/
|
||||
public function get() : string
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actual control name that this sequence represents.
|
||||
* One of the class constants. Eg. self::UP.
|
||||
*
|
||||
* Throws an exception if the character is not actually a control sequence
|
||||
*/
|
||||
public function getControl() : string
|
||||
{
|
||||
if (!isset(static::$controls[$this->data])) {
|
||||
throw new \RuntimeException(sprintf('Character "%s" is not a control', $this->data));
|
||||
}
|
||||
|
||||
return static::$controls[$this->data];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw character or control sequence
|
||||
*/
|
||||
public function __toString() : string
|
||||
{
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the given control name exist? eg self::UP.
|
||||
*/
|
||||
public static function controlExists(string $controlName) : bool
|
||||
{
|
||||
return in_array($controlName, static::$controls, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the available control names
|
||||
*/
|
||||
public static function getControls() : array
|
||||
{
|
||||
return array_values(array_unique(static::$controls));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a instance from a given control name. Throws an exception if the
|
||||
* control name does not exist.
|
||||
*/
|
||||
public static function fromControlName(string $controlName) : self
|
||||
{
|
||||
if (!static::controlExists($controlName)) {
|
||||
throw new \InvalidArgumentException(sprintf('Control "%s" does not exist', $controlName));
|
||||
}
|
||||
|
||||
return new static(array_search($controlName, static::$controls, true));
|
||||
}
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal;
|
||||
|
||||
/**
|
||||
* This class takes a terminal and disabled canonical mode. It reads the input
|
||||
* and returns characters and control sequences as `InputCharacters` as soon
|
||||
* as they are read - character by character.
|
||||
*
|
||||
* On destruct canonical mode will be enabled if it was when in it was constructed.
|
||||
*
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class NonCanonicalReader
|
||||
{
|
||||
/**
|
||||
* @var Terminal
|
||||
*/
|
||||
private $terminal;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $wasCanonicalModeEnabled;
|
||||
|
||||
/**
|
||||
* Map of characters to controls.
|
||||
* Eg map 'w' to the up control.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $mappings = [];
|
||||
|
||||
public function __construct(Terminal $terminal)
|
||||
{
|
||||
$this->terminal = $terminal;
|
||||
$this->wasCanonicalModeEnabled = $terminal->isCanonicalMode();
|
||||
$this->terminal->disableCanonicalMode();
|
||||
}
|
||||
|
||||
public function addControlMapping(string $character, string $mapToControl) : void
|
||||
{
|
||||
if (!InputCharacter::controlExists($mapToControl)) {
|
||||
throw new \InvalidArgumentException(sprintf('Control "%s" does not exist', $mapToControl));
|
||||
}
|
||||
|
||||
$this->mappings[$character] = $mapToControl;
|
||||
}
|
||||
|
||||
public function addControlMappings(array $mappings) : void
|
||||
{
|
||||
foreach ($mappings as $character => $mapToControl) {
|
||||
$this->addControlMapping($character, $mapToControl);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be ran with the terminal canonical mode disabled.
|
||||
*
|
||||
* @return InputCharacter
|
||||
*/
|
||||
public function readCharacter() : InputCharacter
|
||||
{
|
||||
$char = $this->terminal->read(4);
|
||||
|
||||
if (isset($this->mappings[$char])) {
|
||||
return InputCharacter::fromControlName($this->mappings[$char]);
|
||||
}
|
||||
|
||||
return new InputCharacter($char);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->wasCanonicalModeEnabled) {
|
||||
$this->terminal->enableCanonicalMode();
|
||||
}
|
||||
}
|
||||
}
|
32
src/ncc/ThirdParty/php-school/terminal/README.md
vendored
32
src/ncc/ThirdParty/php-school/terminal/README.md
vendored
|
@ -1,32 +0,0 @@
|
|||
<h1 align="center">Terminal Utility</h1>
|
||||
|
||||
<p align="center">
|
||||
Small utility to help provide a simple, consise API for terminal interaction
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://travis-ci.org/php-school/terminal" title="Build Status" target="_blank">
|
||||
<img src="https://img.shields.io/travis/php-school/terminal/master.svg?style=flat-square&label=Linux" />
|
||||
</a>
|
||||
<a href="https://codecov.io/github/php-school/terminal" title="Coverage Status" target="_blank">
|
||||
<img src="https://img.shields.io/codecov/c/github/php-school/terminal.svg?style=flat-square" />
|
||||
</a>
|
||||
<a href="https://scrutinizer-ci.com/g/php-school/terminal/" title="Scrutinizer Code Quality" target="_blank">
|
||||
<img src="https://img.shields.io/scrutinizer/g/php-school/terminal.svg?style=flat-square" />
|
||||
</a>
|
||||
<a href="https://phpschool-team.slack.com/messages">
|
||||
<img src="https://phpschool.herokuapp.com/badge.svg">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
composer require php-school/terminal
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
- [ ] Docs
|
133
src/ncc/ThirdParty/php-school/terminal/Terminal.php
vendored
133
src/ncc/ThirdParty/php-school/terminal/Terminal.php
vendored
|
@ -1,133 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
interface Terminal
|
||||
{
|
||||
/**
|
||||
* Get the available width of the terminal
|
||||
*/
|
||||
public function getWidth() : int;
|
||||
|
||||
/**
|
||||
* Get the available height of the terminal
|
||||
*/
|
||||
public function getHeight() : int;
|
||||
|
||||
/**
|
||||
* Get the number of colours the terminal supports (1, 8, 256, true colours)
|
||||
*/
|
||||
public function getColourSupport() : int;
|
||||
|
||||
/**
|
||||
* Disables echoing every character back to the terminal. This means
|
||||
* we do not have to clear the line when reading.
|
||||
*/
|
||||
public function disableEchoBack() : void;
|
||||
|
||||
/**
|
||||
* Enable echoing back every character input to the terminal.
|
||||
*/
|
||||
public function enableEchoBack() : void;
|
||||
|
||||
/**
|
||||
* Is echo back mode enabled
|
||||
*/
|
||||
public function isEchoBack() : bool;
|
||||
|
||||
/**
|
||||
* Disable canonical input (allow each key press for reading, rather than the whole line)
|
||||
*
|
||||
* @see https://www.gnu.org/software/libc/manual/html_node/Canonical-or-Not.html
|
||||
*/
|
||||
public function disableCanonicalMode() : void;
|
||||
|
||||
/**
|
||||
* Enable canonical input - read input by line
|
||||
*
|
||||
* @see https://www.gnu.org/software/libc/manual/html_node/Canonical-or-Not.html
|
||||
*/
|
||||
public function enableCanonicalMode() : void;
|
||||
|
||||
/**
|
||||
* Is canonical mode enabled or not
|
||||
*/
|
||||
public function isCanonicalMode() : bool;
|
||||
|
||||
/**
|
||||
* Check if the Input & Output streams are interactive. Eg - they are
|
||||
* connected to a terminal.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isInteractive() : bool;
|
||||
|
||||
/**
|
||||
* Restore the terminals original configuration
|
||||
*/
|
||||
public function restoreOriginalConfiguration() : void;
|
||||
|
||||
/**
|
||||
* Test whether terminal supports colour output
|
||||
*/
|
||||
public function supportsColour() : bool;
|
||||
|
||||
/**
|
||||
* Clear the terminal window
|
||||
*/
|
||||
public function clear() : void;
|
||||
|
||||
/**
|
||||
* Clear the current cursors line
|
||||
*/
|
||||
public function clearLine() : void;
|
||||
|
||||
/**
|
||||
* Erase screen from the current line down to the bottom of the screen
|
||||
*/
|
||||
public function clearDown() : void;
|
||||
|
||||
/**
|
||||
* Clean the whole console without jumping the window
|
||||
*/
|
||||
public function clean() : void;
|
||||
|
||||
/**
|
||||
* Enable cursor display
|
||||
*/
|
||||
public function enableCursor() : void;
|
||||
|
||||
/**
|
||||
* Disable cursor display
|
||||
*/
|
||||
public function disableCursor() : void;
|
||||
|
||||
/**
|
||||
* Move the cursor to the top left of the window
|
||||
*/
|
||||
public function moveCursorToTop() : void;
|
||||
|
||||
/**
|
||||
* Move the cursor to the start of a specific row
|
||||
*/
|
||||
public function moveCursorToRow(int $rowNumber) : void;
|
||||
|
||||
/**
|
||||
* Move the cursor to a specific column
|
||||
*/
|
||||
public function moveCursorToColumn(int $columnNumber) : void;
|
||||
|
||||
/**
|
||||
* Read from the input stream
|
||||
*/
|
||||
public function read(int $bytes) : string;
|
||||
|
||||
/**
|
||||
* Write to the output stream
|
||||
*/
|
||||
public function write(string $buffer) : void;
|
||||
}
|
|
@ -1,287 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace ncc\PhpSchool\Terminal;
|
||||
|
||||
use ncc\PhpSchool\Terminal\Exception\NotInteractiveTerminal;
|
||||
use ncc\PhpSchool\Terminal\IO\InputStream;
|
||||
use ncc\PhpSchool\Terminal\IO\OutputStream;
|
||||
|
||||
/**
|
||||
* @author Michael Woodward <mikeymike.mw@gmail.com>
|
||||
* @author Aydin Hassan <aydin@hotmail.co.uk>
|
||||
*/
|
||||
class UnixTerminal implements Terminal
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isCanonical;
|
||||
|
||||
/**
|
||||
* Whether terminal echo back is enabled or not.
|
||||
* Eg. user key presses and the terminal immediately shows it.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $echoBack = true;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $width;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $height;
|
||||
|
||||
/**
|
||||
* @var int;
|
||||
*/
|
||||
private $colourSupport;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $originalConfiguration;
|
||||
|
||||
/**
|
||||
* @var InputStream
|
||||
*/
|
||||
private $input;
|
||||
|
||||
/**
|
||||
* @var OutputStream
|
||||
*/
|
||||
private $output;
|
||||
|
||||
public function __construct(InputStream $input, OutputStream $output)
|
||||
{
|
||||
$this->getOriginalConfiguration();
|
||||
$this->getOriginalCanonicalMode();
|
||||
$this->input = $input;
|
||||
$this->output = $output;
|
||||
}
|
||||
|
||||
private function getOriginalCanonicalMode() : void
|
||||
{
|
||||
exec('stty -a', $output);
|
||||
$this->isCanonical = (strpos(implode("\n", $output), ' icanon') !== false);
|
||||
}
|
||||
|
||||
public function getWidth() : int
|
||||
{
|
||||
return $this->width ?: $this->width = (int) exec('tput cols');
|
||||
}
|
||||
|
||||
public function getHeight() : int
|
||||
{
|
||||
return $this->height ?: $this->height = (int) exec('tput lines');
|
||||
}
|
||||
|
||||
public function getColourSupport() : int
|
||||
{
|
||||
return $this->colourSupport ?: $this->colourSupport = (int) exec('tput colors');
|
||||
}
|
||||
|
||||
private function getOriginalConfiguration() : string
|
||||
{
|
||||
return $this->originalConfiguration ?: $this->originalConfiguration = exec('stty -g');
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables echoing every character back to the terminal. This means
|
||||
* we do not have to clear the line when reading.
|
||||
*/
|
||||
public function disableEchoBack() : void
|
||||
{
|
||||
exec('stty -echo');
|
||||
$this->echoBack = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable echoing back every character input to the terminal.
|
||||
*/
|
||||
public function enableEchoBack() : void
|
||||
{
|
||||
exec('stty echo');
|
||||
$this->echoBack = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is echo back mode enabled
|
||||
*/
|
||||
public function isEchoBack() : bool
|
||||
{
|
||||
return $this->echoBack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable canonical input (allow each key press for reading, rather than the whole line)
|
||||
*
|
||||
* @see https://www.gnu.org/software/libc/manual/html_node/Canonical-or-Not.html
|
||||
*/
|
||||
public function disableCanonicalMode() : void
|
||||
{
|
||||
if ($this->isCanonical) {
|
||||
exec('stty -icanon');
|
||||
$this->isCanonical = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable canonical input - read input by line
|
||||
*
|
||||
* @see https://www.gnu.org/software/libc/manual/html_node/Canonical-or-Not.html
|
||||
*/
|
||||
public function enableCanonicalMode() : void
|
||||
{
|
||||
if (!$this->isCanonical) {
|
||||
exec('stty icanon');
|
||||
$this->isCanonical = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is canonical mode enabled or not
|
||||
*/
|
||||
public function isCanonicalMode() : bool
|
||||
{
|
||||
return $this->isCanonical;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the original terminal configuration
|
||||
*/
|
||||
public function restoreOriginalConfiguration() : void
|
||||
{
|
||||
exec('stty ' . $this->getOriginalConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the Input & Output streams are interactive. Eg - they are
|
||||
* connected to a terminal.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isInteractive() : bool
|
||||
{
|
||||
return $this->input->isInteractive() && $this->output->isInteractive();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that both the Input & Output streams are interactive. Throw
|
||||
* `NotInteractiveTerminal` if not.
|
||||
*/
|
||||
public function mustBeInteractive() : void
|
||||
{
|
||||
if (!$this->input->isInteractive()) {
|
||||
throw NotInteractiveTerminal::inputNotInteractive();
|
||||
}
|
||||
|
||||
if (!$this->output->isInteractive()) {
|
||||
throw NotInteractiveTerminal::outputNotInteractive();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/symfony/Console/blob/master/Output/StreamOutput.php#L95-L102
|
||||
*/
|
||||
public function supportsColour() : bool
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR === '\\') {
|
||||
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM');
|
||||
}
|
||||
|
||||
return $this->isInteractive();
|
||||
}
|
||||
|
||||
public function clear() : void
|
||||
{
|
||||
$this->output->write("\033[2J");
|
||||
}
|
||||
|
||||
public function clearLine() : void
|
||||
{
|
||||
$this->output->write("\033[2K");
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase screen from the current line down to the bottom of the screen
|
||||
*/
|
||||
public function clearDown() : void
|
||||
{
|
||||
$this->output->write("\033[J");
|
||||
}
|
||||
|
||||
public function clean() : void
|
||||
{
|
||||
foreach (range(0, $this->getHeight()) as $rowNum) {
|
||||
$this->moveCursorToRow($rowNum);
|
||||
$this->clearLine();
|
||||
}
|
||||
}
|
||||
|
||||
public function enableCursor() : void
|
||||
{
|
||||
$this->output->write("\033[?25h");
|
||||
}
|
||||
|
||||
public function disableCursor() : void
|
||||
{
|
||||
$this->output->write("\033[?25l");
|
||||
}
|
||||
|
||||
public function moveCursorToTop() : void
|
||||
{
|
||||
$this->output->write("\033[H");
|
||||
}
|
||||
|
||||
public function moveCursorToRow(int $rowNumber) : void
|
||||
{
|
||||
$this->output->write(sprintf("\033[%d;0H", $rowNumber));
|
||||
}
|
||||
|
||||
public function moveCursorToColumn(int $column) : void
|
||||
{
|
||||
$this->output->write(sprintf("\033[%dC", $column));
|
||||
}
|
||||
|
||||
public function showSecondaryScreen() : void
|
||||
{
|
||||
$this->output->write("\033[?47h");
|
||||
}
|
||||
|
||||
public function showPrimaryScreen() : void
|
||||
{
|
||||
$this->output->write("\033[?47l");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read bytes from the input stream
|
||||
*/
|
||||
public function read(int $bytes): string
|
||||
{
|
||||
$buffer = '';
|
||||
$this->input->read($bytes, function ($data) use (&$buffer) {
|
||||
$buffer .= $data;
|
||||
});
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to the output stream
|
||||
*/
|
||||
public function write(string $buffer): void
|
||||
{
|
||||
$this->output->write($buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the original terminal configuration on shutdown.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->restoreOriginalConfiguration();
|
||||
}
|
||||
}
|
|
@ -13,13 +13,6 @@ spl_autoload_register(
|
|||
'ncc\\abstracts\\regexpatterns' => '/Abstracts/RegexPatterns.php',
|
||||
'ncc\\abstracts\\scopes' => '/Abstracts/Scopes.php',
|
||||
'ncc\\abstracts\\stringpaddingmethod' => '/Abstracts/StringPaddingMethod.php',
|
||||
'ncc\\assert\\assert' => '/ThirdParty/beberlei/assert/Assert.php',
|
||||
'ncc\\assert\\assertion' => '/ThirdParty/beberlei/assert/Assertion.php',
|
||||
'ncc\\assert\\assertionchain' => '/ThirdParty/beberlei/assert/AssertionChain.php',
|
||||
'ncc\\assert\\assertionfailedexception' => '/ThirdParty/beberlei/assert/AssertionFailedException.php',
|
||||
'ncc\\assert\\invalidargumentexception' => '/ThirdParty/beberlei/assert/InvalidArgumentException.php',
|
||||
'ncc\\assert\\lazyassertion' => '/ThirdParty/beberlei/assert/LazyAssertion.php',
|
||||
'ncc\\assert\\lazyassertionexception' => '/ThirdParty/beberlei/assert/LazyAssertionException.php',
|
||||
'ncc\\cli\\functions' => '/CLI/Functions.php',
|
||||
'ncc\\cli\\helpmenu' => '/CLI/HelpMenu.php',
|
||||
'ncc\\cli\\main' => '/CLI/Main.php',
|
||||
|
@ -44,59 +37,6 @@ spl_autoload_register(
|
|||
'ncc\\objects\\projectconfiguration\\compiler' => '/Objects/ProjectConfiguration/Compiler.php',
|
||||
'ncc\\objects\\projectconfiguration\\dependency' => '/Objects/ProjectConfiguration/Dependency.php',
|
||||
'ncc\\objects\\projectconfiguration\\project' => '/Objects/ProjectConfiguration/Project.php',
|
||||
'ncc\\phpschool\\climenu\\action\\exitaction' => '/ThirdParty/php-school/cli-menu/Action/ExitAction.php',
|
||||
'ncc\\phpschool\\climenu\\action\\gobackaction' => '/ThirdParty/php-school/cli-menu/Action/GoBackAction.php',
|
||||
'ncc\\phpschool\\climenu\\builder\\climenubuilder' => '/ThirdParty/php-school/cli-menu/Builder/CliMenuBuilder.php',
|
||||
'ncc\\phpschool\\climenu\\builder\\splititembuilder' => '/ThirdParty/php-school/cli-menu/Builder/SplitItemBuilder.php',
|
||||
'ncc\\phpschool\\climenu\\climenu' => '/ThirdParty/php-school/cli-menu/CliMenu.php',
|
||||
'ncc\\phpschool\\climenu\\dialogue\\cancellableconfirm' => '/ThirdParty/php-school/cli-menu/Dialogue/CancellableConfirm.php',
|
||||
'ncc\\phpschool\\climenu\\dialogue\\confirm' => '/ThirdParty/php-school/cli-menu/Dialogue/Confirm.php',
|
||||
'ncc\\phpschool\\climenu\\dialogue\\dialogue' => '/ThirdParty/php-school/cli-menu/Dialogue/Dialogue.php',
|
||||
'ncc\\phpschool\\climenu\\dialogue\\flash' => '/ThirdParty/php-school/cli-menu/Dialogue/Flash.php',
|
||||
'ncc\\phpschool\\climenu\\exception\\cannotshrinkmenuexception' => '/ThirdParty/php-school/cli-menu/Exception/CannotShrinkMenuException.php',
|
||||
'ncc\\phpschool\\climenu\\exception\\invalidshortcutexception' => '/ThirdParty/php-school/cli-menu/Exception/InvalidShortcutException.php',
|
||||
'ncc\\phpschool\\climenu\\exception\\invalidterminalexception' => '/ThirdParty/php-school/cli-menu/Exception/InvalidTerminalException.php',
|
||||
'ncc\\phpschool\\climenu\\exception\\menunotopenexception' => '/ThirdParty/php-school/cli-menu/Exception/MenuNotOpenException.php',
|
||||
'ncc\\phpschool\\climenu\\frame' => '/ThirdParty/php-school/cli-menu/Frame.php',
|
||||
'ncc\\phpschool\\climenu\\input\\input' => '/ThirdParty/php-school/cli-menu/Input/Input.php',
|
||||
'ncc\\phpschool\\climenu\\input\\inputio' => '/ThirdParty/php-school/cli-menu/Input/InputIO.php',
|
||||
'ncc\\phpschool\\climenu\\input\\inputresult' => '/ThirdParty/php-school/cli-menu/Input/InputResult.php',
|
||||
'ncc\\phpschool\\climenu\\input\\number' => '/ThirdParty/php-school/cli-menu/Input/Number.php',
|
||||
'ncc\\phpschool\\climenu\\input\\password' => '/ThirdParty/php-school/cli-menu/Input/Password.php',
|
||||
'ncc\\phpschool\\climenu\\input\\text' => '/ThirdParty/php-school/cli-menu/Input/Text.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\asciiartitem' => '/ThirdParty/php-school/cli-menu/MenuItem/AsciiArtItem.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\checkboxitem' => '/ThirdParty/php-school/cli-menu/MenuItem/CheckboxItem.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\linebreakitem' => '/ThirdParty/php-school/cli-menu/MenuItem/LineBreakItem.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\menuiteminterface' => '/ThirdParty/php-school/cli-menu/MenuItem/MenuItemInterface.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\menumenuitem' => '/ThirdParty/php-school/cli-menu/MenuItem/MenuMenuItem.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\propagatesstyles' => '/ThirdParty/php-school/cli-menu/MenuItem/PropagatesStyles.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\radioitem' => '/ThirdParty/php-school/cli-menu/MenuItem/RadioItem.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\selectableitem' => '/ThirdParty/php-school/cli-menu/MenuItem/SelectableItem.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\selectableitemrenderer' => '/ThirdParty/php-school/cli-menu/MenuItem/SelectableItemRenderer.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\splititem' => '/ThirdParty/php-school/cli-menu/MenuItem/SplitItem.php',
|
||||
'ncc\\phpschool\\climenu\\menuitem\\staticitem' => '/ThirdParty/php-school/cli-menu/MenuItem/StaticItem.php',
|
||||
'ncc\\phpschool\\climenu\\menustyle' => '/ThirdParty/php-school/cli-menu/MenuStyle.php',
|
||||
'ncc\\phpschool\\climenu\\style\\checkboxstyle' => '/ThirdParty/php-school/cli-menu/Style/CheckboxStyle.php',
|
||||
'ncc\\phpschool\\climenu\\style\\defaultstyle' => '/ThirdParty/php-school/cli-menu/Style/DefaultStyle.php',
|
||||
'ncc\\phpschool\\climenu\\style\\exception\\invalidstyle' => '/ThirdParty/php-school/cli-menu/Style/Exception/InvalidStyle.php',
|
||||
'ncc\\phpschool\\climenu\\style\\itemstyle' => '/ThirdParty/php-school/cli-menu/Style/ItemStyle.php',
|
||||
'ncc\\phpschool\\climenu\\style\\locator' => '/ThirdParty/php-school/cli-menu/Style/Locator.php',
|
||||
'ncc\\phpschool\\climenu\\style\\radiostyle' => '/ThirdParty/php-school/cli-menu/Style/RadioStyle.php',
|
||||
'ncc\\phpschool\\climenu\\style\\selectablestyle' => '/ThirdParty/php-school/cli-menu/Style/SelectableStyle.php',
|
||||
'ncc\\phpschool\\climenu\\terminal\\terminalfactory' => '/ThirdParty/php-school/cli-menu/Terminal/TerminalFactory.php',
|
||||
'ncc\\phpschool\\climenu\\util\\collection' => '/ThirdParty/php-school/cli-menu/Util/Collection.php',
|
||||
'ncc\\phpschool\\climenu\\util\\colourutil' => '/ThirdParty/php-school/cli-menu/Util/ColourUtil.php',
|
||||
'ncc\\phpschool\\climenu\\util\\stringutil' => '/ThirdParty/php-school/cli-menu/Util/StringUtil.php',
|
||||
'ncc\\phpschool\\terminal\\exception\\notinteractiveterminal' => '/ThirdParty/php-school/terminal/Exception/NotInteractiveTerminal.php',
|
||||
'ncc\\phpschool\\terminal\\inputcharacter' => '/ThirdParty/php-school/terminal/InputCharacter.php',
|
||||
'ncc\\phpschool\\terminal\\io\\bufferedoutput' => '/ThirdParty/php-school/terminal/IO/BufferedOutput.php',
|
||||
'ncc\\phpschool\\terminal\\io\\inputstream' => '/ThirdParty/php-school/terminal/IO/InputStream.php',
|
||||
'ncc\\phpschool\\terminal\\io\\outputstream' => '/ThirdParty/php-school/terminal/IO/OutputStream.php',
|
||||
'ncc\\phpschool\\terminal\\io\\resourceinputstream' => '/ThirdParty/php-school/terminal/IO/ResourceInputStream.php',
|
||||
'ncc\\phpschool\\terminal\\io\\resourceoutputstream' => '/ThirdParty/php-school/terminal/IO/ResourceOutputStream.php',
|
||||
'ncc\\phpschool\\terminal\\noncanonicalreader' => '/ThirdParty/php-school/terminal/NonCanonicalReader.php',
|
||||
'ncc\\phpschool\\terminal\\terminal' => '/ThirdParty/php-school/terminal/Terminal.php',
|
||||
'ncc\\phpschool\\terminal\\unixterminal' => '/ThirdParty/php-school/terminal/UnixTerminal.php',
|
||||
'ncc\\symfony\\component\\process\\exception\\exceptioninterface' => '/ThirdParty/Symfony/Process/Exception/ExceptionInterface.php',
|
||||
'ncc\\symfony\\component\\process\\exception\\invalidargumentexception' => '/ThirdParty/Symfony/Process/Exception/InvalidArgumentException.php',
|
||||
'ncc\\symfony\\component\\process\\exception\\logicexception' => '/ThirdParty/Symfony/Process/Exception/LogicException.php',
|
||||
|
|
Loading…
Add table
Reference in a new issue