This commit is contained in:
Dennis Eichhorn 2023-10-09 22:06:39 +00:00
parent eff9f243d9
commit 99211861eb
23 changed files with 302 additions and 45 deletions

View File

@ -207,7 +207,7 @@ final class Kmeans
*/
private function kpp(array $points, int $n) : array
{
$clusters = [clone $points[\mt_rand(0, \count($points) - 1)]];
$clusters = [clone $points[\array_rand($points, 1)]];
$d = \array_fill(0, $n, 0.0);
for ($i = 1; $i < $n; ++$i) {

View File

@ -81,7 +81,7 @@ class MazeGenerator
if (!empty($neighbors)) {
--$n;
$next = $neighbors[\mt_rand(0, \count($neighbors) - 1)];
$next = $neighbors[\array_rand($neighbors, 1)];
$unvisited[$next[0] + 1][$next[1] + 1] = false;
if ($next[0] === $pos[0]) {

View File

@ -0,0 +1,27 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package phpOMS\Business
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS\Business;
/**
* Depreciation class.
*
* @package phpOMS\Business
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
final class BusinessHelper
{
}

View File

@ -271,6 +271,20 @@ class DataMapperFactory
return (new ReadMapper(new static(), $db ?? self::$db))->count();
}
/**
* Create read mapper
*
* @param ConnectionAbstract $db Database connection
*
* @return ReadMapper
*
* @since 1.0.0
*/
public static function sum(ConnectionAbstract $db = null) : ReadMapper
{
return (new ReadMapper(new static(), $db ?? self::$db))->sum();
}
/**
* Create read mapper
*

View File

@ -40,9 +40,11 @@ abstract class MapperType extends Enum
public const COUNT_MODELS = 12;
public const MODEL_EXISTS = 13;
public const SUM_MODELS = 13;
public const MODEL_HAS_RELATION = 14;
public const MODEL_EXISTS = 14;
public const MODEL_HAS_RELATION = 15;
// -------------------------------------------- //

View File

@ -118,6 +118,20 @@ final class ReadMapper extends DataMapperAbstract
return $this;
}
/**
* Create sum mapper
*
* @return self
*
* @since 1.0.0
*/
public function sum() : self
{
$this->type = MapperType::SUM_MODELS;
return $this;
}
/**
* Create exists mapper
*
@ -205,6 +219,8 @@ final class ReadMapper extends DataMapperAbstract
return $this->executeGetRaw();
case MapperType::COUNT_MODELS:
return $this->executeCount();
case MapperType::SUM_MODELS:
return $this->executeSum();
case MapperType::MODEL_EXISTS:
return $this->executeExists();
case MapperType::MODEL_HAS_RELATION:
@ -404,11 +420,35 @@ final class ReadMapper extends DataMapperAbstract
*/
public function executeCount() : int
{
$query = $this->getQuery(null, ['COUNT(*)' => 'count']);
$query = $this->getQuery(
null,
[
'COUNT(' . (empty($this->columns) ? '*' : \implode($this->columns)) . ')' => 'count'
]
);
return (int) $query->execute()?->fetchColumn();
}
/**
* Sum the number of elements
*
* @return int
*
* @since 1.0.0
*/
public function executeSum() : int|float
{
$query = $this->getQuery(
null,
[
'SUM(' . (empty($this->columns) ? '*' : \implode($this->columns)) . ')' => 'sum'
]
);
return $query->execute()?->fetchColumn();
}
/**
* Check if any element exists
*

View File

@ -232,7 +232,7 @@ final class WriteMapper extends DataMapperAbstract
if (isset($this->mapper::BELONGS_TO[$propertyName]['by'])) {
// has by (obj is stored as a different model e.g. model = profile but reference/db is account)
if ($this->mapper::BELONGS_TO[$propertyName]['private']) {
if ($this->mapper::BELONGS_TO[$propertyName]['private'] ?? false) {
$refClass = new \ReflectionClass($obj);
$refProp = $refClass->getProperty($this->mapper::BELONGS_TO[$propertyName]['by']);
$obj = $refProp->getValue($obj);
@ -339,9 +339,10 @@ final class WriteMapper extends DataMapperAbstract
$relProperty = $relReflectionClass->getProperty($internalName);
}
// todo maybe consider to just set the column type to object, and then check for that (might be faster)
// @todo maybe consider to just set the column type to object, and then check for that (might be faster)
if (isset($mapper::BELONGS_TO[$internalName])
|| isset($mapper::OWNS_ONE[$internalName])) {
|| isset($mapper::OWNS_ONE[$internalName])
) {
if ($isRelPrivate) {
$relProperty->setValue($value, $this->mapper::createNullModel($objId));
} else {

View File

@ -78,7 +78,7 @@ class ISO3166NameEnum extends Enum
public const _BTN = 'Bhutan';
public const _BOL = 'Bolivia (Plurinational State of)';
public const _BOL = 'Bolivia';
public const _BES = 'Bonaire, Sint Eustatius and Saba';
@ -234,7 +234,7 @@ class ISO3166NameEnum extends Enum
public const _IDN = 'Indonesia';
public const _IRN = 'Iran (Islamic Republic of)';
public const _IRN = 'Iran';
public const _IRQ = 'Iraq';
@ -260,9 +260,9 @@ class ISO3166NameEnum extends Enum
public const _KIR = 'Kiribati';
public const _PRK = 'Korea (Democratic People\'s Republic of)';
public const _PRK = 'North Korea';
public const _KOR = 'Korea (Republic of)';
public const _KOR = 'South Korea';
public const _KWT = 'Kuwait';
@ -314,9 +314,9 @@ class ISO3166NameEnum extends Enum
public const _MEX = 'Mexico';
public const _FSM = 'Micronesia (Federated States of)';
public const _FSM = 'Micronesia';
public const _MDA = 'Moldova (Republic of)';
public const _MDA = 'Moldova';
public const _MCO = 'Monaco';
@ -364,7 +364,7 @@ class ISO3166NameEnum extends Enum
public const _PLW = 'Palau';
public const _PSE = 'Palestine, State of';
public const _PSE = 'Palestine';
public const _PAN = 'Panama';
@ -460,11 +460,11 @@ class ISO3166NameEnum extends Enum
public const _SYR = 'Syrian Arab Republic';
public const _TWN = 'Taiwan, Province of China[a]';
public const _TWN = 'Taiwan';
public const _TJK = 'Tajikistan';
public const _TZA = 'Tanzania, United Republic of';
public const _TZA = 'Tanzania';
public const _THA = 'Thailand';
@ -494,7 +494,7 @@ class ISO3166NameEnum extends Enum
public const _ARE = 'United Arab Emirates';
public const _GBR = 'United Kingdom of Great Britain and Northern Ireland';
public const _GBR = 'United Kingdom';
public const _USA = 'United States of America';
@ -506,7 +506,7 @@ class ISO3166NameEnum extends Enum
public const _VUT = 'Vanuatu';
public const _VEN = 'Venezuela (Bolivarian Republic of)';
public const _VEN = 'Venezuela';
public const _VNM = 'Viet Nam';

View File

@ -37,10 +37,44 @@ trait ISO3166Trait
{
/** @var string $code3 */
$code3 = ISO3166TwoEnum::getName($code);
if ($code3 === false) {
$code3 = '';
}
return self::getByName($code3);
}
/**
* Get countries in a region
*
* @param string $region Region name
*
* @return array
*
* @since 1.0.0
*/
public static function getSubregions(string $region) : array
{
$region = \strtolower($region);
switch ($region) {
case 'continents':
return ['Europe', 'Asia', 'America', 'Oceania', 'Africa'];
case 'europe':
return ['North-Europe', 'South-Europe', 'East-Europe', 'West-Europe'];
case 'asia':
return ['Central-Asia', 'South-Asia', 'Southeast-Asia', 'East-Asia', 'West-Asia'];
case 'america':
return ['North-america', 'South-america', 'Central-america', 'Caribbean'];
case 'oceania':
return ['Australia', 'Polynesia', 'Melanesia', 'Micronesia', 'Antarctica'];
case 'africa':
return ['North-Africa', 'South-Africa', 'East-Africa', 'West-Africa', 'Central-Africa'];
default:
return [$region];
}
}
/**
* Get countries in a region
*
@ -55,6 +89,14 @@ trait ISO3166Trait
$region = \strtolower($region);
switch ($region) {
case 'continents':
return \array_merge(
self::getRegion('europe'),
self::getRegion('asia'),
self::getRegion('america'),
self::getRegion('oceania'),
self::getRegion('africa')
);
case 'europe':
return \array_merge(
self::getRegion('north-europe'),
@ -83,7 +125,7 @@ trait ISO3166Trait
self::getRegion('polynesia'),
self::getRegion('melanesia'),
self::getRegion('micronesia'),
self::getRegion('antartica')
self::getRegion('antarctica')
);
case 'africa':
return \array_merge(

View File

@ -0,0 +1,92 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package phpOMS\Localization
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS\Localization;
use phpOMS\Stdlib\Base\Enum;
/**
* Country codes ISO list.
*
* @package phpOMS\Localization
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
class RegionEnum extends Enum
{
public const EUROPE = 'Europe';
public const EU = 'EU';
public const EURO = 'Euro';
public const NORTH_EUROPE = 'North-Europe';
public const SOUTH_EUROPE = 'South-Europe';
public const EAST_EUROPE = 'East-Europe';
public const WEST_EUROPE = 'West-Europe';
public const AMERICA = 'America';
public const SOUTH_AMERICA = 'South-America';
public const NORTH_AMERICA = 'North-America';
public const CENTRAL_AMERICA = 'Central-America';
public const CARIBBEAN = 'Caribbean';
public const ASIA = 'Asia';
public const CENTRAL_ASIA = 'Central-Asia';
public const SOUTH_ASIA = 'South-Asia';
public const SOUTHEAST_ASIA = 'Southeast-Asia';
public const EAST_ASIA = 'East-Asia';
public const WEST_ASIA = 'West-Asia';
public const OCEANIA = 'Oceania';
public const AFRICA = 'Africa';
public const CENTRAL_AFRICA = 'Central-Africa';
public const SOUTH_AFRICA = 'South-Africa';
public const NORTH_AFRICA = 'North-Africa';
public const EAST_AFRICA = 'East-Africa';
public const WEST_AFRICA = 'West-Africa';
public const MIDDLE_EAST = 'Middle-East';
public const AUSTRALIA = 'Australia';
public const POLYNESIA = 'Polynesia';
public const MELANESIA = 'Melanesia';
public const MICRONESIA = 'Micronesia';
public const ANTARCTICA = 'Antarctica';
public const CONTINENTS = 'Continents';
}

View File

@ -51,7 +51,7 @@ abstract class ResponseAbstract implements \JsonSerializable, MessageInterface
*
* @since 1.0.0
*/
public function get(mixed $key, string $type = null) : mixed
public function getData(mixed $key, string $type = null) : mixed
{
if ($key === null) {
return $this->data;

View File

@ -70,9 +70,8 @@ abstract class Enum
{
$reflect = new \ReflectionClass(static::class);
$constants = $reflect->getConstants();
$keys = \array_keys($constants);
return $constants[$keys[\mt_rand(0, \count($constants) - 1)]];
return $constants[\array_rand($constants, 1)];
}
/**

View File

@ -119,8 +119,6 @@ abstract class EnumArray
*/
public static function getRandom() : mixed
{
$keys = \array_keys(static::$constants);
return static::$constants[$keys[\mt_rand(0, \count(static::$constants) - 1)]];
return static::$constants[\array_rand(static::$constants, 1)];
}
}

View File

@ -158,17 +158,15 @@ class FloatInt implements SerializableInterface
*/
public function getAmount(?int $decimals = 2) : string
{
$isNegative = $this->value < 0 ? 1 : 0;
$value = $this->value === 0
? \str_repeat('0', self::MAX_DECIMALS)
: (string) \round($this->value, -self::MAX_DECIMALS + $decimals);
$left = \substr($value, 0, -self::MAX_DECIMALS + $isNegative);
$left = \substr($value, 0, -self::MAX_DECIMALS);
/** @var string $left */
$left = $left === false ? '0' : $left;
$right = \substr($value, -self::MAX_DECIMALS + $isNegative);
$right = \substr($value, -self::MAX_DECIMALS);
if ($right === false) {
throw new \Exception(); // @codeCoverageIgnore

View File

@ -359,4 +359,48 @@ class SmartDateTime extends \DateTime
return $days;
}
public static function startOfYear(int $month = 1) : \DateTime
{
return new \DateTime(\date('Y') . '-' . \sprintf('%02d', $month) . '-01');
}
public static function endOfYear(int $month = 1) : \DateTime
{
return new \DateTime(\date('Y') . '-' . self::calculateMonthIndex(13 - $month, $month) . '-31');
}
public static function startOfMonth() : \DateTime
{
return new \DateTime(\date('Y-m') . '-01');
}
public static function endOfMonth() : \DateTime
{
return new \DateTime(\date('Y-m-t'));
}
public static function monthDiff(\DateTime $d1, \DateTime $d2) : int
{
$interval = $d1->diff($d2);
return ($interval->y * 12) + $interval->m;
}
/**
* Calculates the current month index based on the start of the fiscal year.
*
* @param int $month Current month
* @param int $start Start of the fiscal year (01 = January)
*
* @return int
*
* @since 1.0.0;
*/
public static function calculateMonthIndex(int $month, int $start = 1) : int
{
$mod = ($month - $start);
return \abs(($mod < 0 ? 12 + $mod : $mod) % 12) + 1;
}
}

View File

@ -1091,7 +1091,7 @@ class QR extends TwoDAbstract
$howManuOut = 8 - (self::QR_FIND_FROM_RANDOM % 9);
for ($i = 0; $i < $howManuOut; ++$i) {
// @note: This is why the same content can result in different QR codes
$remPos = \mt_rand(0, \count($checked_masks) - 1);
$remPos = \array_rand($checked_masks, 1);
unset($checked_masks[$remPos]);
$checked_masks = \array_values($checked_masks);
}

View File

@ -291,7 +291,7 @@ class Markdown
if (\strpos($Excerpt['text'], '>') !== false
&& \preg_match("/^<((mailto:)?{$commonMarkEmail})>/i", $Excerpt['text'], $matches)
){
$url = $matches[1];
$url = UriFactory::build($matches[1]);
if (!isset($matches[2]))
{
@ -496,7 +496,7 @@ class Markdown
if (\strpos($Excerpt['context'], 'http') !== false
&& \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, \PREG_OFFSET_CAPTURE)
) {
$url = $matches[0][0];
$url = UriFactory::build($matches[0][0]);
return [
'extent' => \strlen($matches[0][0]),
@ -521,7 +521,7 @@ class Markdown
if (\strpos($Excerpt['text'], '>') !== false && \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches))
{
$url = $matches[1];
$url = UriFactory::build($matches[1]);
return [
'extent' => \strlen($matches[0]),
@ -3727,7 +3727,7 @@ class Markdown
if (\preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*+"|\'[^\']*+\'))?\s*+[)]/', $remainder, $matches))
{
$Element['attributes']['href'] = $matches[1];
$Element['attributes']['href'] = UriFactory::build($matches[1]);
if (isset($matches[2]))
{

View File

@ -59,8 +59,8 @@ class File
$source = self::$extensions;
}
$key = \mt_rand(0, \count($source) - 1);
$key = \array_rand($source, 1);
return $source[$key][\mt_rand(0, \count($source[$key]) - 1)];
return $source[$key][\array_rand($source[$key], 1)];
}
}

View File

@ -493,8 +493,8 @@ class Name
*/
public static function generateName(array $type, string $origin = 'western') : string
{
$rndType = \mt_rand(0, \count($type) - 1);
$rndType = \array_rand($type, 1);
return self::$names[$origin][$type[$rndType]][\mt_rand(0, \count(self::$names[$origin][$type[$rndType]]) - 1)];
return self::$names[$origin][$type[$rndType]][\array_rand(self::$names[$origin][$type[$rndType]], 1)];
}
}

View File

@ -52,7 +52,7 @@ class Phone
$numberString = \str_replace(
'$1',
(string) $countries[\array_keys($countries)[\mt_rand(0, \count($countries) - 1)]],
(string) $countries[\array_rand($countries, 1)],
$numberString
);
}

View File

@ -58,7 +58,7 @@ final class HttpResponseTest extends \PHPUnit\Framework\TestCase
public function testResponseInputOutput() : void
{
$this->response->setResponse(['a' => 1]);
self::assertEquals(1, $this->response->get('a'));
self::assertEquals(1, $this->response->getData('a'));
}
/**

View File

@ -53,7 +53,7 @@ final class ResponseAbstractTest extends \PHPUnit\Framework\TestCase
*/
public function testDefault() : void
{
self::assertNull($this->response->get('asdf'));
self::assertNull($this->response->getData('asdf'));
self::assertEquals('', $this->response->getBody());
}
@ -75,6 +75,6 @@ final class ResponseAbstractTest extends \PHPUnit\Framework\TestCase
public function testDataInputOutput() : void
{
$this->response->set('asdf', false);
self::assertFalse($this->response->get('asdf'));
self::assertFalse($this->response->getData('asdf'));
}
}

View File

@ -258,7 +258,7 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
'message' => 'Test Message!',
'response' => [1, 'test string', 'bool' => true],
],
$response->get('')
$response->getData('')
);
}
@ -276,7 +276,7 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
self::assertEquals(
[1, 'test string', 'bool' => true],
$response->get('')
$response->getData('')
);
}