diff --git a/DataStorage/Database/Query/Grammar/MysqlGrammar.php b/DataStorage/Database/Query/Grammar/MysqlGrammar.php index d777d2174..d87c869d0 100644 --- a/DataStorage/Database/Query/Grammar/MysqlGrammar.php +++ b/DataStorage/Database/Query/Grammar/MysqlGrammar.php @@ -16,6 +16,17 @@ namespace phpOMS\DataStorage\Database\Query\Grammar; +/** + * Grammar class. + * + * @category Framework + * @package phpOMS\DataStorage\Database\Query\Grammar + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class MysqlGrammar extends Grammar { diff --git a/DataStorage/Database/Query/Grammar/SQLiteGrammar.php b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php index 4525a722d..9bc62563d 100644 --- a/DataStorage/Database/Query/Grammar/SQLiteGrammar.php +++ b/DataStorage/Database/Query/Grammar/SQLiteGrammar.php @@ -16,6 +16,17 @@ namespace phpOMS\DataStorage\Database\Query\Grammar; +/** + * Grammar class. + * + * @category Framework + * @package phpOMS\DataStorage\Database\Query\Grammar + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class SqliteGrammar extends Grammar { diff --git a/DataStorage/Database/Schema/Grammar/Grammar.php b/DataStorage/Database/Schema/Grammar/Grammar.php index 1a01e499c..8db2ab858 100644 --- a/DataStorage/Database/Schema/Grammar/Grammar.php +++ b/DataStorage/Database/Schema/Grammar/Grammar.php @@ -18,7 +18,6 @@ namespace phpOMS\DataStorage\Database\Schema\Grammar; use phpOMS\DataStorage\Database\BuilderAbstract; use phpOMS\DataStorage\Database\GrammarAbstract; -use phpOMS\DataStorage\Database\Schema\Builder; use phpOMS\DataStorage\Database\Schema\QueryType; /** @@ -55,6 +54,16 @@ class Grammar extends GrammarAbstract 'drop', ]; + /** + * Compile components based on query type. + * + * @param BuilderAbstract $query Query + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function compileComponents(BuilderAbstract $query) : array { $sql = []; @@ -77,7 +86,18 @@ class Grammar extends GrammarAbstract return $sql; } - protected function compileDrop(Builder $query, array $tables) : string + /** + * Compile drop query. + * + * @param BuilderAbstract $query Query + * @param array $tables Tables to drop + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + protected function compileDrop(BuilderAbstract $query, array $tables) : string { $expression = $this->expressionizeTableColumn($tables, $query->getPrefix()); diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 3d7fae029..f59b03499 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -104,7 +104,7 @@ class FileLogger implements LoggerInterface } if (is_dir($lpath) || strpos($lpath, '.') === false) { - Directory::createPath($lpath, 0644); + Directory::createPath($lpath, '0644'); File::createFile($path = $lpath . '/' . date('Y-m-d') . '.log'); $path = realpath($path); diff --git a/Math/Complex.php b/Math/Complex.php deleted file mode 100644 index 7a8a1acf7..000000000 --- a/Math/Complex.php +++ /dev/null @@ -1,59 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ -namespace phpOMS\Math; - -class Complex -{ - private $real = null; - - private $im = null; - - public function getReal() - { - } - - public function setReal() - { - } - - public function setImaginary() - { - } - - public function getImaginary() - { - } - - public function pow() - { - } - - public function add() - { - } - - public function sub() - { - } - - public function mult() - { - } - - public function div() - { - } -} diff --git a/Math/Forecast/Error.php b/Math/Forecast/Error.php new file mode 100644 index 000000000..42f976fb4 --- /dev/null +++ b/Math/Forecast/Error.php @@ -0,0 +1,341 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\Math\Forecast; + +use phpOMS\Math\Functions; +use phpOMS\Math\Statistic\Average; +use phpOMS\Math\Statistic\MeasureOfDispersion; + +/** + * Basic forecast functions. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Error +{ + /** + * Get the error of a forecast. + * + * @param float $observed Dataset + * @param float $forecasted Forecasted + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getForecastError(float $observed, float $forecasted) : float + { + return $observed - $forecasted; + } + + /** + * Get array of errors of a forecast. + * + * @param array $observed Dataset + * @param array $forecasted Forecasted + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getForecastErrorArray(array $observed, array $forecasted) : array + { + $errors = []; + + foreach ($forecasted as $key => $expected) { + $errors[] = self::getForecastError($observed[$key], $expected); + } + + return $errors; + } + + /** + * Get error percentage. + * + * @param float $error Error + * @param float $observed Dataset + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPercentageError(float $error, float $observed) : float + { + return $error / $observed; + } + + /** + * Get error percentages. + * + * @param array $errors Errors + * @param array $observed Dataset + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPercentageErrorArray(array $errors, array $observed) : array + { + $percentages = []; + + foreach ($errors as $key => $error) { + $percentages[] = self::getPercentageError($error, $observed[$key]); + } + + return $percentages; + } + + /** + * Get mean absolute error (MAE). + * + * @param array $errors Errors + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getMeanAbsoulteError(array $errors) : float + { + return Average::arithmeticMean(Functions::abs($errors)); + } + + /** + * Get root mean squared error (RMSE). + * + * @param array $errors Errors + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRootMeanSquaredError(array $errors) : float + { + return sqrt(Average::arithmeticMean(self::square($errors))); + } + + /** + * Get mean absolute percentage error (MAPE). + * + * @param array $observed Dataset + * @param array $forecasted Forecasted + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getMeanAbsolutePercentageError(array $observed, array $forecasted) : float + { + return Average::arithmeticMean(Functions::abs(self::getPercentageErrorArray(self::getForecastErrorArray($observed, $forecasted), $observed))); + } + + /** + * Get mean absolute percentage error (sMAPE). + * + * @param array $observed Dataset + * @param array $forecasted Forecasted + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getSymmetricMeanAbsolutePercentageError(array $observed, array $forecasted) : float + { + $error = []; + + foreach ($observed as $key => $value) { + $error[] = 200 * abs($value - $forecasted[$key]) / ($value + $forecasted[$key]); + } + + return Average::arithmeticMean($error); + } + + /** + * Square all values in array. + * + * @param array $values Values to square + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + * todo: move to utils?! implement sqrt for array as well... could be usefull for others (e.g. matrix) + */ + private static function square(array $values) : array + { + $squared = []; + + foreach ($values as $value) { + $squared[] = $value * $value; + } + + return $squared; + } + + /** + * Get cross sectional scaled errors (CSSE) + * + * @param array $errors Errors + * @param array $observed Dataset + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCrossSectionalScaledErrorArray(array $errors, array $observed) : array + { + $scaled = []; + $deviation = MeasureOfDispersion::meanDeviation($observed); + + foreach ($errors as $error) { + $error[] = $error / $deviation; + } + + return $scaled; + } + + /** + * Get cross sectional scaled errors (CSSE) + * + * @param float $error Errors + * @param array $observed Dataset + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCrossSectionalScaledError(float $error, array $observed) : float + { + $mean = Average::arithmeticMean($observed); + $sum = 0.0; + + foreach ($observed as $value) { + $sum += abs($value - $mean); + } + + return $error / MeasureOfDispersion::meanDeviation($observed); + } + + /** + * Get mean absolute scaled error (MASE) + * + * @param array $scaledErrors Scaled errors + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getMeanAbsoluteScaledError(array $scaledErrors) : float + { + return Average::arithmeticMean(Functions::abs($scaledErrors)); + } + + /** + * Get mean absolute scaled error (MASE) + * + * @param array $scaledErrors Scaled errors + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getMeanSquaredScaledError(array $scaledErrors) : float + { + return Average::arithmeticMean(self::square($scaledErrors)); + } + + /** + * Get scaled error (SE) + * + * @param array $errors Errors + * @param array $observed Dataset + * @param int $m Shift + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getScaledErrorArray(array $errors, array $observed, int $m = 1) : array + { + $scaled = []; + $naive = 1 / (count($observed) - $m) * self::getNaiveForecast($observed, $m); + + foreach ($errors as $error) { + $error[] = $error / $naive; + } + + return $scaled; + } + + /** + * Get scaled error (SE) + * + * @param float $error Errors + * @param array $observed Dataset + * @param int $m Shift + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getScaledError(float $error, array $observed, int $m = 1) : float + { + return $error / (1 / (count($observed) - $m) * self::getNaiveForecast($observed, $m)); + } + + /** + * Get naive forecast + * + * @param array $observed Dataset + * @param int $m Shift + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private static function getNaiveForecast(array $observed, int $m = 1) : float + { + $sum = 0.0; + $count = count($observed); + + for ($i = 0 + $m; $i < $count; $i++) { + $sum += abs($observed[$i] - $observed[$i - $m]); + } + + return $sum; + } +} \ No newline at end of file diff --git a/Math/Function/Functions.php b/Math/Function/Functions.php index 30967a5ca..edc4c11ea 100644 --- a/Math/Function/Functions.php +++ b/Math/Function/Functions.php @@ -138,6 +138,18 @@ class Functions return self::ackermann($m - 1, self::ackermann($m, $n - 1)); } + + public static function abs(array $values) : array + { + $abs = []; + + foreach ($values as $value) { + $abs[] = abs($value); + } + + return $abs; + } + /** * Calculate inverse modular. * diff --git a/Math/Number/Complex.php b/Math/Number/Complex.php index 143095c4e..e7364fc02 100644 --- a/Math/Number/Complex.php +++ b/Math/Number/Complex.php @@ -1,14 +1,59 @@ -class Complex implements Number { -private $real = 0; -private $imaginary = 0; + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Number; -public function __construct() {} +class Complex +{ + private $real = null; -public function setReal($real) { -$this->real = $real; + private $im = null; + + public function getReal() + { + } + + public function setReal() + { + } + + public function setImaginary() + { + } + + public function getImaginary() + { + } + + public function pow() + { + } + + public function add() + { + } + + public function sub() + { + } + + public function mult() + { + } + + public function div() + { + } } - -public function setImaginary($imaginary) { -$this->imaginary = $imaginary; -} -} \ No newline at end of file diff --git a/Math/Number/Number.php b/Math/Number/Number.php deleted file mode 100644 index 5c47d7a0b..000000000 --- a/Math/Number/Number.php +++ /dev/null @@ -1,9 +0,0 @@ - + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\Math\Number; + +use phpOMS\Datatypes\Enum; + +/** + * Number type enum. + * + * @category Framework + * @package Utils + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class AccountType extends Enum +{ + const INTEGER = 1; + const NATURAL = 21; + const EVEN = 211; + const UNEVEN = 212; + const PRIME = 22; + const REAL = 3; + const RATIONAL = 4; + const IRRATIONAL = 5; + const COMPLEX = 6; } \ No newline at end of file diff --git a/Math/Number/OpperationInterface.php b/Math/Number/OpperationInterface.php index 1b7fc2a30..d662d1d9d 100644 --- a/Math/Number/OpperationInterface.php +++ b/Math/Number/OpperationInterface.php @@ -1,16 +1,102 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Number; -interface NumberInterface +/** + * Basic operation interface. + * + * @category Framework + * @package phpOMS\Account + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface OperationInterface { - public function add($number); + /** + * Add value. + * + * @param mixed $x Value + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function add($x); - public function sub($number); + /** + * Subtract value. + * + * @param mixed $x Value + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function sub($x); - public function mult($number); + /** + * Right multiplicate value. + * + * @param mixed $x Value + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function mult($x); - public function div($number); + /** + * Right devision value. + * + * @param mixed $x Value + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function div($x); + /** + * Power of value. + * + * @param mixed $p Power + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function pow($p); - public function abs($number); + /** + * Abs of value. + * + * @param mixed $x Value + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function abs($x); } \ No newline at end of file diff --git a/Math/Statistic/Average.php b/Math/Statistic/Average.php index 8503e8be0..a7cbfa1ff 100644 --- a/Math/Statistic/Average.php +++ b/Math/Statistic/Average.php @@ -60,6 +60,25 @@ class Average return $avg; } + /** + * Average change. + * + * @param array $x Dataset + * @param int $h Future steps + * + * @return float + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function averageChange(array $x, int $h = 1) : float + { + $count = count($x); + + return $x[$count - 1] + $h * ($x[$count - 1] - $x[0]) / ($count - 1); + } /** * Calculate the mode. @@ -104,7 +123,7 @@ class Average } else { $low = $values[$middleval]; $high = $values[$middleval + 1]; - $median = (($low + $high) / 2); + $median = ($low + $high) / 2; } return $median; diff --git a/Math/Statistic/Forecasts.php b/Math/Statistic/Forecasts.php new file mode 100644 index 000000000..e69de29bb diff --git a/Math/Statistic/MeasureOfDispersion.php b/Math/Statistic/MeasureOfDispersion.php index 585f9aae7..7ae9a4832 100644 --- a/Math/Statistic/MeasureOfDispersion.php +++ b/Math/Statistic/MeasureOfDispersion.php @@ -80,7 +80,7 @@ class MeasureOfDispersion $sum += $value - $mean; } - return $sum / $count; + return $sum / ($count - 1); } /** @@ -204,6 +204,89 @@ class MeasureOfDispersion */ public static function bravaisPersonCorrelationcoefficient(array $x, array $y) : float { - return self::empiricalCovariance($x, $y) / sqrt(self::empiricalCovariance($x, $x) * self::empiricalCovariance($y, $y)); + return self::empiricalCovariance($x, $y) / (self::standardDeviation($x) * self::standardDeviation($y)); + } + + /** + * Get interquartile range. + * + * @param array $x Dataset + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getIQR(array $x) : float + { + } + + /** + * Get mean deviation. + * + * @param array $x Values + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function meanDeviation(array $x) : float + { + $mean = Average::arithmeticMean($x); + $sum = 0.0; + + foreach ($x as $xi) { + $sum += ($xi - $mean); + } + + return $sum / count($x); + } + + /** + * Get squared mean deviation. + * + * @param array $x Values + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function squaredMeanDeviation(array $x) : float + { + $mean = Average::arithmeticMean($x); + $sum = 0.0; + + foreach ($x as $xi) { + $sum += ($xi - $mean) ** 2; + } + + return $sum / count($x); + } + + /** + * Get the autocorrelation coefficient (ACF). + * + * @param array $x Dataset + * @param int $k k-th coefficient + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function autocorrelationCoefficient(array $x, int $k = 0) : float + { + $squaredMeanDeviation = self::squaredMeanDeviation($x); + $mean = Average::arithmeticMean($x); + $count = count($x); + $sum = 0.0; + + for ($i = $k + 1; $i < $count; $i++) { + $sum += ($x[$i] - $mean) * ($x[$i - $k] - $mean); + } + + return $sum / ($squaredMeanDeviation * count($x)); } } diff --git a/Math/Stochastic/Distribution/UniformDistributionContinuous.php b/Math/Stochastic/Distribution/UniformDistributionContinuous.php index 07a61623f..2ab995e82 100644 --- a/Math/Stochastic/Distribution/UniformDistributionContinuous.php +++ b/Math/Stochastic/Distribution/UniformDistributionContinuous.php @@ -182,9 +182,4 @@ class UniformDistributionContinuous { return 1 / 12 * ($b - $a) ** 2; } - - public static function getRandom() - { - - } } diff --git a/Message/Http/Header.php b/Message/Http/Header.php index 9d51b8ff9..68b48af3c 100644 --- a/Message/Http/Header.php +++ b/Message/Http/Header.php @@ -168,6 +168,9 @@ class Header extends HeaderAbstract case RequestStatus::R_403: $this->generate403(); break; + case RequestStatus::R_404: + $this->generate404(); + break; case RequestStatus::R_406: $this->generate406(); break; @@ -182,6 +185,18 @@ class Header extends HeaderAbstract } } + /** + * Get status code. + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getStatusCode() : int { + return http_response_code(); + } + /** * Generate predefined header. * @@ -194,6 +209,22 @@ class Header extends HeaderAbstract { $this->set('HTTP', 'HTTP/1.0 403 Forbidden'); $this->set('Status', 'Status: HTTP/1.0 403 Forbidden'); + http_response_code(403); + } + + /** + * Generate predefined header. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private function generate404() + { + $this->set('HTTP', 'HTTP/1.0 404 Not Found'); + $this->set('Status', 'Status: HTTP/1.0 404 Not Found'); + http_response_code(404); } /** @@ -208,6 +239,7 @@ class Header extends HeaderAbstract { $this->set('HTTP', 'HTTP/1.0 406 Not acceptable'); $this->set('Status', 'Status: 406 Not acceptable'); + http_response_code(406); } /** @@ -223,6 +255,7 @@ class Header extends HeaderAbstract $this->set('HTTP', 'HTTP/1.0 503 Service Temporarily Unavailable'); $this->set('Status', 'Status: 503 Service Temporarily Unavailable'); $this->set('Retry-After', 'Retry-After: 300'); + http_response_code(503); } /** diff --git a/Message/ResponseAbstract.php b/Message/ResponseAbstract.php index 9e63b8afe..f3ac6fa13 100644 --- a/Message/ResponseAbstract.php +++ b/Message/ResponseAbstract.php @@ -122,6 +122,7 @@ abstract class ResponseAbstract implements MessageInterface /** * {@inheritdoc} + * todo: shouldn't this only be available in the header?! */ public function setStatusCode(string $status) { @@ -131,6 +132,7 @@ abstract class ResponseAbstract implements MessageInterface /** * {@inheritdoc} + * todo: shouldn't this only be available in the header?! */ public function getStatusCode() : string { diff --git a/System/File/Directory.php b/System/File/Directory.php index 71cd22b72..3bfc4f2e2 100644 --- a/System/File/Directory.php +++ b/System/File/Directory.php @@ -14,6 +14,7 @@ * @link http://orange-management.com */ namespace phpOMS\System\File; + use phpOMS\Validation\Validator; /** @@ -159,7 +160,7 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess * Create directory. * * @param string $path Path - * @param int $permission Directory permission + * @param string $permission Directory permission * @param bool $recursive Create parent directories if applicable * * @return bool @@ -167,7 +168,7 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess * @since 1.0.0 * @author Dennis Eichhorn */ - public static function createPath(string $path, int $permission = 0644, bool $recursive = false) : bool + public static function createPath(string $path, string $permission = '0644', bool $recursive = false) : bool { if ($recursive && !file_exists($parent = self::getParent($path))) { self::createPath($parent, $permission, $recursive); diff --git a/Utils/Git/Author.php b/Utils/Git/Author.php index f86cb9336..2804f1d74 100644 --- a/Utils/Git/Author.php +++ b/Utils/Git/Author.php @@ -44,6 +44,14 @@ class Author */ private $email = ''; + /** + * Commit count. + * + * @var int + * @since 1.0.0 + */ + private $commitCount = 0; + /** * Constructor * @@ -55,8 +63,8 @@ class Author */ public function __construct(string $name = '', string $email = '') { - $this->name = escapeshellarg($name); - $this->email = escapeshellarg($email); + $this->name = $name; + $this->email = $email; } /** @@ -84,4 +92,32 @@ class Author { return $this->email; } + + /** + * Set commit count + * + * @param int $count Commit count + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setCommitCount(int $count) + { + $this->commitCount = $count; + } + + /** + * Get commit count + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getCommitCount() : int + { + return $this->commitCount; + } } \ No newline at end of file diff --git a/Utils/Git/Commit.php b/Utils/Git/Commit.php index b77545b8c..8a6abfde6 100644 --- a/Utils/Git/Commit.php +++ b/Utils/Git/Commit.php @@ -102,7 +102,7 @@ class Commit */ public function __construct(string $id = '') { - $this->id = escapeshellarg($id); + $this->id = $id; $this->author = new Author(); $this->branch = new Branch(); $this->tag = new Tag(); diff --git a/Utils/Git/Repository.php b/Utils/Git/Repository.php index e6dde0a8e..8e0a027f1 100644 --- a/Utils/Git/Repository.php +++ b/Utils/Git/Repository.php @@ -15,8 +15,8 @@ */ namespace phpOMS\Utils\Git; +use phpOMS\Auth\Auth; use phpOMS\System\File\PathException; -use phpOMS\Validation\Validator; /** * Repository class @@ -73,8 +73,8 @@ class Repository */ public function __construct(string $path) { - $this->branch = $this->getActiveBranch(); $this->setPath($path); + $this->branch = $this->getActiveBranch(); } /** @@ -119,7 +119,7 @@ class Repository $this->path = realpath($path); - if ($this->path === false || !Validator::startsWith($this->path, ROOT_PATH)) { + if ($this->path === false) { throw new PathException($path); } @@ -178,7 +178,7 @@ class Repository throw new \Exception($stderr); } - return $this->parseLines($stdout); + return $this->parseLines(trim($stdout)); } /** @@ -193,22 +193,20 @@ class Repository */ private function parseLines(string $lines) : array { - $lines = preg_replace('/!\\t+/', '|', $lines); - $lines = preg_replace('!\s+!', ' ', $lines); - $lineArray = preg_split('/\\r\\n|\\r|\\n/', $lines); + //$lines = preg_replace('/!\\t+/', '|', $lines); + //$lines = preg_replace('!\s+!', ' ', $lines); + $lineArray = preg_split('/\r\n|\n|\r/', $lines); + $lines = []; foreach ($lineArray as $key => $line) { - $lineArray[$key] = trim($line, ' |'); + $temp = trim($line, ' |'); - if (empty($line)) { - unset($lineArray[$key]); - } else { - $lineArray[$key] = $line; + if (!empty($temp)) { + $lines[] = $temp; } } - return $lineArray; - + return $lines; } /** @@ -333,8 +331,8 @@ class Repository /** * Create local branch. * - * @param Branch $branch Branch - * @param bool $force Force? + * @param Branch $branch Branch + * @param bool $force Force? * * @return string * @@ -346,12 +344,20 @@ class Repository return implode("\n", $this->run('branch ' . ($force ? '-D' : '-d') . ' ' . $branch->getName())); } + /** + * Get all branches. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getBranches() : array { $branches = $this->run('branch'); foreach ($branches as $key => &$branch) { - $branch = trim($branch); + $branch = trim($branch, '* '); if ($branch === '') { unset($branches[$key]); @@ -361,12 +367,20 @@ class Repository return $branches; } + /** + * Get all remote branches. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getBranchesRemote() : array { $branches = $this->run('branch -r'); foreach ($branches as $key => &$branch) { - $branch = trim($branch); + $branch = trim($branch, '* '); if ($branch === '' || strpos($branch, 'HEAD -> ') !== false) { unset($branches[$key]); @@ -568,16 +582,28 @@ class Repository * * @return Commit * + * @throws \Exception + * * @since 1.0.0 * @author Dennis Eichhorn */ public function getCommit(string $commit) : Commit { $commit = escapeshellarg($commit); - $lines = $this->run('log show --name-only ' . $commit); + $lines = $this->run('show --name-only ' . $commit); $count = count($lines); + if (empty($lines)) { + // todo: return null commit + return new Commit(); + } + preg_match('/[0-9ABCDEFabcdef]{40}/', $lines[0], $matches); + + if (!isset($matches[0]) || strlen($matches[0]) !== 40) { + throw new \Exception('Invalid commit id'); + } + $author = explode(':', $lines[1]); $author = explode('<', trim($author[1])); $date = explode(':', $lines[2]); @@ -622,13 +648,50 @@ class Repository $commits = []; foreach ($lines as $line) { - $count = explode('|', $line); - $commits[$count[1]] = $count[0]; + preg_match('/^[0-9]*/', $line, $matches); + + $commits[substr($line, strlen($matches[0]) + 1)] = (int) $matches[0]; } return $commits; } + /** + * Get contributors. + * + * @param \DateTime $start Start date + * @param \DateTime $end End date + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getContributors(\DateTime $start = null, \DateTime $end = null) : array + { + if (!isset($start)) { + $start = new \DateTime('1970-12-31'); + } + + if (!isset($end)) { + $end = new \DateTime('now'); + } + + $lines = $this->run('shortlog -s -n --since="' . $start->format('Y-m-d') . '" --before="' . $end->format('Y-m-d') . '" --all'); + $contributors = []; + + foreach ($lines as $line) { + preg_match('/^[0-9]*/', $line, $matches); + + $contributor = new Author(substr($line, strlen($matches[0]) + 1)); + $contributor->setCommitCount($this->getCommitsCount($start, $end)[$contributor->getName()]); + + $contributors[] = $contributor; + } + + return $contributors; + } + /** * Get remote. * @@ -691,14 +754,25 @@ class Repository * * @return Commit * + * @throws \Exception + * * @since 1.0.0 * @author Dennis Eichhorn */ public function getNewest() : Commit { - $lines = $this->run('log --name-status HEAD^..HEAD'); + $lines = $this->run('log -n 1'); - preg_match('[0-9ABCDEFabcdef]{40}', $lines[0], $matches); + if (empty($lines)) { + // todo: return nullcommit + return new Commit(); + } + + preg_match('/[0-9ABCDEFabcdef]{40}/', $lines[0], $matches); + + if (!isset($matches[0]) || strlen($matches[0]) !== 40) { + throw new \Exception('Invalid commit id'); + } return $this->getCommit($matches[0]); } diff --git a/Utils/TaskSchedule/CronJob.php b/Utils/TaskSchedule/CronJob.php index dd91e8504..05c882d6b 100644 --- a/Utils/TaskSchedule/CronJob.php +++ b/Utils/TaskSchedule/CronJob.php @@ -26,7 +26,7 @@ namespace phpOMS\Utils\TaskSchedule; * @link http://orange-management.com * @since 1.0.0 */ -class CronJob extends TaskAbstract +class CronJob extends TaskAbstract implements \Serializable { /** @@ -49,7 +49,15 @@ class CronJob extends TaskAbstract $this->command = $cmd; } - public function __toString() + /** + * Serialize cronjob. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function serialize() { $minute = $this->printValue($this->interval->getMinute()); $hour = $this->printValue($this->interval->getHour()); @@ -60,6 +68,16 @@ class CronJob extends TaskAbstract return $minute . ' ' . $hour . ' ' . $dayOfMonth . ' ' . $month . ' ' . $dayOfWeek . ' ' . $this->command; } + /** + * Print value. + * + * @param array $value Element to serialize + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private function printValue(array $value) : string { if (($count = count($value['dayOfWeek'])) > 0) { @@ -78,4 +96,17 @@ class CronJob extends TaskAbstract return $parsed; } + + /** + * Unserialize cronjob. + * + * @param string $serialized To unserialize + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function unserialize($serialized) + { + // TODO: Implement unserialize() method. + } } diff --git a/Utils/TaskSchedule/Schedule.php b/Utils/TaskSchedule/Schedule.php index a14cf620d..63ebe1745 100644 --- a/Utils/TaskSchedule/Schedule.php +++ b/Utils/TaskSchedule/Schedule.php @@ -26,7 +26,7 @@ namespace phpOMS\Utils\TaskSchedule; * @link http://orange-management.com * @since 1.0.0 */ -class Schedule extends TaskAbstract +class Schedule extends TaskAbstract implements \Serializable { /** * Constructor. @@ -48,8 +48,28 @@ class Schedule extends TaskAbstract $this->command = $cmd; } - public function __toString() + /** + * String representation of object + * @link http://php.net/manual/en/serializable.serialize.php + * @return string the string representation of the object or null + * @since 5.1.0 + */ + public function serialize() { - return ''; + // TODO: Implement serialize() method. + } + + /** + * Constructs the object + * @link http://php.net/manual/en/serializable.unserialize.php + * @param string $serialized

+ * The string representation of the object. + *

+ * @return void + * @since 5.1.0 + */ + public function unserialize($serialized) + { + // TODO: Implement unserialize() method. } }