updated packages
This commit is contained in:
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Dotenv\Regex;
|
||||
|
||||
use PhpOption\None;
|
||||
use PhpOption\Some;
|
||||
|
||||
class Error extends Result
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* Internal constructor for an error value.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new error value.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public static function create($value)
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \PhpOption\Option
|
||||
*/
|
||||
public function success()
|
||||
{
|
||||
return None::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @param callable $f
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public function mapSuccess(callable $f)
|
||||
{
|
||||
return self::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \PhpOption\Option
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
return Some::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @param callable $f
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public function mapError(callable $f)
|
||||
{
|
||||
return self::create($f($this->value));
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Dotenv\Regex;
|
||||
|
||||
use PhpOption\Option;
|
||||
|
||||
class Regex
|
||||
{
|
||||
/**
|
||||
* Perform a preg replace, wrapping up the result.
|
||||
*
|
||||
* @param string $pattern
|
||||
* @param string $replacement
|
||||
* @param string $subject
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public static function replace($pattern, $replacement, $subject)
|
||||
{
|
||||
return self::pregAndWrap(function ($subject) use ($pattern, $replacement) {
|
||||
return preg_replace($pattern, $replacement, $subject);
|
||||
}, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a preg replace callback, wrapping up the result.
|
||||
*
|
||||
* @param string $pattern
|
||||
* @param callable $callback
|
||||
* @param string $subject
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public static function replaceCallback($pattern, callable $callback, $subject)
|
||||
{
|
||||
return self::pregAndWrap(function ($subject) use ($pattern, $callback) {
|
||||
return preg_replace_callback($pattern, $callback, $subject);
|
||||
}, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a preg operation, wrapping up the result.
|
||||
*
|
||||
* @param callable $operation
|
||||
* @param string $subject
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
private static function pregAndWrap(callable $operation, $subject)
|
||||
{
|
||||
$result = (string) @$operation($subject);
|
||||
|
||||
if (($e = preg_last_error()) !== PREG_NO_ERROR) {
|
||||
return Error::create(self::lookupError($e));
|
||||
}
|
||||
|
||||
return Success::create($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup the preg error code.
|
||||
*
|
||||
* @param int $code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function lookupError($code)
|
||||
{
|
||||
return Option::fromValue(get_defined_constants(true))
|
||||
->filter(function (array $consts) {
|
||||
return isset($consts['pcre']) && defined('ARRAY_FILTER_USE_KEY');
|
||||
})
|
||||
->map(function (array $consts) {
|
||||
return array_filter($consts['pcre'], function ($msg) {
|
||||
return substr($msg, -6) === '_ERROR';
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
})
|
||||
->flatMap(function (array $errors) use ($code) {
|
||||
return Option::fromValue(
|
||||
array_search($code, $errors, true)
|
||||
);
|
||||
})
|
||||
->getOrElse('PREG_ERROR');
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Dotenv\Regex;
|
||||
|
||||
abstract class Result
|
||||
{
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \PhpOption\Option
|
||||
*/
|
||||
abstract public function success();
|
||||
|
||||
/**
|
||||
* Get the error value, if possible.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSuccess()
|
||||
{
|
||||
return $this->success()->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @param callable $f
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
abstract public function mapSuccess(callable $f);
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \PhpOption\Option
|
||||
*/
|
||||
abstract public function error();
|
||||
|
||||
/**
|
||||
* Get the error value, if possible.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error()->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @param callable $f
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
abstract public function mapError(callable $f);
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Dotenv\Regex;
|
||||
|
||||
use PhpOption\None;
|
||||
use PhpOption\Some;
|
||||
|
||||
class Success extends Result
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* Internal constructor for a success value.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new success value.
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public static function create($value)
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \PhpOption\Option
|
||||
*/
|
||||
public function success()
|
||||
{
|
||||
return Some::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @param callable $f
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public function mapSuccess(callable $f)
|
||||
{
|
||||
return self::create($f($this->value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \PhpOption\Option
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
return None::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @param callable $f
|
||||
*
|
||||
* @return \Dotenv\Regex\Result
|
||||
*/
|
||||
public function mapError(callable $f)
|
||||
{
|
||||
return self::create($this->value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user