migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Db;
/**
* Interface IQueryRow
*
* @package OCA\RelatedResources\Tools\Db
*/
interface IQueryRow {
/**
* import data to feed the model.
*
* @param array $data
*
* @return IQueryRow
*/
public function importFromDatabase(array $data): self;
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Exceptions;
use Exception;
/**
* Class ArrayNotFoundException
*
* @package OCA\RelatedResources\Tools\Exceptions
*/
class ArrayNotFoundException extends Exception {
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Exceptions;
use Exception;
/**
* Class DateTimeException
*
* @package OCA\RelatedResources\Tools\Exceptions
*/
class DateTimeException extends Exception {
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Exceptions;
use Exception;
/**
* Class InvalidItemException
*
* @package OCA\RelatedResources\Tools\Exceptions
*/
class InvalidItemException extends Exception {
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Exceptions;
use Exception;
/**
* Class ItemNotFoundException
*
* @package OCA\RelatedResources\Tools\Exceptions
*/
class ItemNotFoundException extends Exception {
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Exceptions;
use Exception;
/**
* Class MalformedArrayException
*
* @package OCA\RelatedResources\Tools\Exceptions
*/
class MalformedArrayException extends Exception {
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Exceptions;
use Exception;
/**
* Class RowNotFoundException
*
* @package OCA\RelatedResources\Tools\Exceptions
*/
class RowNotFoundException extends Exception {
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Exceptions;
use Exception;
/**
* Class UnknownTypeException
*
* @package OCA\RelatedResources\Tools\Exceptions
*/
class UnknownTypeException extends Exception {
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools;
interface IDeserializable {
/**
* @param array $data
*
* @return self
*/
public function import(array $data): self;
}
@@ -0,0 +1,432 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Traits;
use Exception;
use JsonSerializable;
use OCA\RelatedResources\Tools\Exceptions\ArrayNotFoundException;
use OCA\RelatedResources\Tools\Exceptions\ItemNotFoundException;
use OCA\RelatedResources\Tools\Exceptions\MalformedArrayException;
use OCA\RelatedResources\Tools\Exceptions\UnknownTypeException;
trait TArrayTools {
public static $TYPE_NULL = 'Null';
public static $TYPE_STRING = 'String';
public static $TYPE_ARRAY = 'Array';
public static $TYPE_BOOLEAN = 'Boolean';
public static $TYPE_INTEGER = 'Integer';
public static $TYPE_SERIALIZABLE = 'Serializable';
/**
* @param string $k
* @param array $arr
* @param string $default
*
* @return string
*/
protected function get(string $k, array $arr, string $default = ''): string {
if (!array_key_exists($k, $arr)) {
$subs = explode('.', $k, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
return $default;
}
$r = $arr[$subs[0]];
if (!is_array($r)) {
return $default;
}
return $this->get($subs[1], $r, $default);
} else {
return $default;
}
}
if ($arr[$k] === null || !is_string($arr[$k]) && (!is_int($arr[$k]))) {
return $default;
}
return (string)$arr[$k];
}
/**
* @param string $k
* @param array $arr
* @param int $default
*
* @return int
*/
protected function getInt(string $k, array $arr, int $default = 0): int {
if (!array_key_exists($k, $arr)) {
$subs = explode('.', $k, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
return $default;
}
$r = $arr[$subs[0]];
if (!is_array($r)) {
return $default;
}
return $this->getInt($subs[1], $r, $default);
} else {
return $default;
}
}
if ($arr[$k] === null) {
return $default;
}
return intval($arr[$k]);
}
/**
* @param string $k
* @param array $arr
* @param float $default
*
* @return float
*/
protected function getFloat(string $k, array $arr, float $default = 0): float {
if (!array_key_exists($k, $arr)) {
$subs = explode('.', $k, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
return $default;
}
$r = $arr[$subs[0]];
if (!is_array($r)) {
return $default;
}
return $this->getFloat($subs[1], $r, $default);
} else {
return $default;
}
}
if ($arr[$k] === null) {
return $default;
}
return intval($arr[$k]);
}
/**
* @param string $k
* @param array $arr
* @param bool $default
*
* @return bool
*/
protected function getBool(string $k, array $arr, bool $default = false): bool {
if (!array_key_exists($k, $arr)) {
$subs = explode('.', $k, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
return $default;
}
return $this->getBool($subs[1], $arr[$subs[0]], $default);
} else {
return $default;
}
}
if ($arr[$k] === null) {
return $default;
}
if (is_bool($arr[$k])) {
return $arr[$k];
}
$sk = (string)$arr[$k];
if ($sk === '1' || strtolower($sk) === 'true') {
return true;
}
if ($sk === '0' || strtolower($sk) === 'false') {
return false;
}
return $default;
}
/**
* @param string $k
* @param array $arr
* @param JsonSerializable|null $default
*
* @return mixed
*/
protected function getObj(string $k, array $arr, ?JsonSerializable $default = null): ?JsonSerializable {
if (!array_key_exists($k, $arr)) {
$subs = explode('.', $k, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
return $default;
}
return $this->getObj($subs[1], $arr[$subs[0]], $default);
} else {
return $default;
}
}
return $arr[$k];
}
/**
* @param string $k
* @param array $arr
* @param array $default
*
* @return array
*/
protected function getArray(string $k, array $arr, array $default = []): array {
if (!array_key_exists($k, $arr)) {
$subs = explode('.', $k, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
return $default;
}
$r = $arr[$subs[0]];
if (!is_array($r)) {
return $default;
}
return $this->getArray($subs[1], $r, $default);
} else {
return $default;
}
}
$r = $arr[$k];
if (!is_array($r) && !is_string($r)) {
return $default;
}
if (is_string($r)) {
$r = json_decode($r, true);
}
if (!is_array($r)) {
return $default;
}
return $r;
}
/**
* @param string $k
* @param array $arr
*
* @return bool
*/
public function validKey(string $k, array $arr): bool {
if (array_key_exists($k, $arr)) {
return true;
}
$subs = explode('.', $k, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
return false;
}
$r = $arr[$subs[0]];
if (!is_array($r)) {
return false;
}
return $this->validKey($subs[1], $r);
}
return false;
}
/**
* @param string $k
* @param array $arr
* @param array $import
* @param array $default
*
* @return array
*/
protected function getList(string $k, array $arr, array $import, array $default = []): array {
$list = $this->getArray($k, $arr, $default);
$r = [];
[$obj, $method] = $import;
foreach ($list as $item) {
try {
$o = new $obj();
$o->$method($item);
$r[] = $o;
} catch (Exception $e) {
}
}
return $r;
}
/**
* @param string $k
* @param string $value
* @param array $list
*
* @return mixed
* @throws ArrayNotFoundException
*/
protected function extractArray(string $k, string $value, array $list) {
foreach ($list as $arr) {
if (!array_key_exists($k, $arr)) {
continue;
}
if ($arr[$k] === $value) {
return $arr;
}
}
throw new ArrayNotFoundException();
}
/**
* @param string $key
* @param array $arr
* @param bool $root
*
* @return string
* @throws ItemNotFoundException
* @throws UnknownTypeException
*/
public function typeOf(string $key, array $arr, bool $root = true): string {
if (array_key_exists($key, $arr)) {
$item = $arr[$key];
if (is_null($item)) {
return self::$TYPE_NULL;
}
if (is_string($item)) {
return self::$TYPE_STRING;
}
if (is_array($item)) {
return self::$TYPE_ARRAY;
}
if (is_bool($item)) {
return self::$TYPE_BOOLEAN;
}
if (is_int($item)) {
return self::$TYPE_INTEGER;
}
if ($item instanceof JsonSerializable) {
return self::$TYPE_SERIALIZABLE;
}
throw new UnknownTypeException();
}
$subs = explode('.', $key, 2);
if (sizeof($subs) > 1) {
if (!array_key_exists($subs[0], $arr)) {
throw new ItemNotFoundException();
}
$r = $arr[$subs[0]];
if (is_array($r)) {
return $this->typeOf($subs[1], $r);
}
}
throw new ItemNotFoundException();
}
/**
* @param array $keys
* @param array $arr
*
* @throws MalformedArrayException
*/
protected function mustContains(array $keys, array $arr): void {
foreach ($keys as $key) {
if (!array_key_exists($key, $arr)) {
throw new MalformedArrayException(
'source: ' . json_encode($arr) . ' - missing key: ' . $key
);
}
}
}
/**
* @param array $arr
*/
protected function cleanArray(array &$arr): void {
$arr = array_filter(
$arr,
function ($v) {
if (is_string($v)) {
return ($v !== '');
}
if (is_array($v)) {
return !empty($v);
}
return true;
}
);
}
}
@@ -0,0 +1,159 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Traits;
use Exception;
use JsonSerializable;
use OCA\RelatedResources\Tools\Exceptions\InvalidItemException;
use OCA\RelatedResources\Tools\IDeserializable;
use ReflectionClass;
trait TDeserialize {
/**
* @param JsonSerializable $model
*
* @return array
*/
public function serialize(JsonSerializable $model): array {
return json_decode(json_encode($model), true);
}
/**
* @param array $data
*
* @return array
*/
public function serializeArray(array $data): array {
return json_decode(json_encode($data), true);
}
/**
* @param array $data
* @param string $class
*
* @return IDeserializable
* @throws InvalidItemException
*/
public function deserialize(array $data, string $class): IDeserializable {
try {
$test = new ReflectionClass($class);
} catch (\ReflectionException $e) {
throw new InvalidItemException('cannot ReflectionClass ' . $class);
}
if (!in_array(IDeserializable::class, $test->getInterfaceNames())) {
throw new InvalidItemException($class . ' does not implement IDeserializable');
}
/** @var IDeserializable $item */
$item = new $class;
$item->import($data);
return $item;
}
/**
* force deserialize without checking for implementation of IDeserializable.
* quickest solution to deserialize model from other apps.
*
* @param string $json
* @param string $class
*
* @return array
*/
public function forceDeserializeArrayFromJson(string $json, string $class): array {
$data = json_decode($json, true);
if (!is_array($data)) {
return [];
}
$arr = [];
foreach ($data as $entry) {
try {
$item = new $class;
$arr[] = $item->import($entry);
} catch (Exception $e) {
}
}
return $arr;
}
/**
* @param string $json
* @param string $class
*
* @return IDeserializable[]
*/
public function deserializeArrayFromJson(string $json, string $class): array {
$data = json_decode($json, true);
if (!is_array($data)) {
return [];
}
return $this->deserializeArray($data, $class);
}
/**
* @param array $data
* @param string $class
*
* @return array
*/
public function deserializeArray(array $data, string $class): array {
$arr = [];
foreach ($data as $entry) {
try {
$arr[] = $this->deserialize($entry, $class);
} catch (InvalidItemException $e) {
}
}
return $arr;
}
/**
* @param string $json
* @param string $class
*
* @return IDeserializable
* @throws InvalidItemException
*/
public function deserializeJson(string $json, string $class): IDeserializable {
$data = json_decode($json, true);
return $this->deserialize($data, $class);
}
}
@@ -0,0 +1,258 @@
<?php
declare(strict_types=1);
/**
* Nextcloud - Related Resources
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2022
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\RelatedResources\Tools\Traits;
use DateTime;
use Exception;
trait TStringTools {
use TArrayTools;
/**
* @param int $length
*
* @return string
*/
protected function token(int $length = 15): string {
$chars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
$str = '';
$max = strlen($chars);
for ($i = 0; $i < $length; $i++) {
try {
$str .= $chars[random_int(0, $max - 2)];
} catch (Exception $e) {
}
}
return $str;
}
/**
* Generate uuid: 2b5a7a87-8db1-445f-a17b-405790f91c80
*
* @param int $length
*
* @return string
*/
protected function uuid(int $length = 0): string {
$uuid = sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
if ($length > 0) {
if ($length <= 16) {
$uuid = str_replace('-', '', $uuid);
}
$uuid = substr($uuid, 0, $length);
}
return $uuid;
}
/**
* @param string $line
* @param int $length
*
* @return string
*/
protected function cut(string $line, int $length): string {
if (strlen($line) < $length) {
return $line;
}
return substr($line, 0, $length - 5) . ' (..)';
}
/**
* @param string $str1
* @param string $str2
* @param bool $cs case sensitive ?
*
* @return string
*/
protected function commonPart(string $str1, string $str2, bool $cs = true): string {
for ($i = 0; $i < strlen($str1) && $i < strlen($str2); $i++) {
$chr1 = $str1[$i];
$chr2 = $str2[$i];
if (!$cs) {
$chr1 = strtolower($chr1);
$chr2 = strtolower($chr2);
}
if ($chr1 !== $chr2) {
break;
}
}
return substr($str1, 0, $i);
}
/**
* @param string $line
* @param array $params
*
* @return string
*/
protected function feedStringWithParams(string $line, array $params): string {
$ak = array_keys($params);
foreach ($ak as $k) {
$line = str_replace('{' . $k . '}', (string)$params[$k], $line);
}
return $line;
}
/**
* @param int $words
*
* @return string
*/
public function generateRandomSentence(int $words = 5): string {
$sentence = [];
for ($i = 0; $i < $words; $i++) {
$sentence[] = $this->generateRandomWord(rand(2, 12));
}
return implode(' ', $sentence);
}
/**
* @param int $length
*
* @return string
*/
public function generateRandomWord(int $length = 8): string {
$c = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v'];
$v = ['a', 'e', 'i', 'o', 'u', 'y'];
$word = [];
for ($i = 0; $i <= ($length / 2); $i++) {
$word[] = $c[array_rand($c)];
$word[] = $v[array_rand($v)];
}
return implode('', $word);
}
/**
* @param int $bytes
*
* @return string
*/
public function humanReadable(int $bytes): string {
if ($bytes == 0) {
return '0.00 B';
}
$s = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$e = floor(log($bytes, 1024));
return round($bytes / pow(1024, $e), 2) . ' ' . $s[$e];
}
/**
* @param int $first
* @param int $second
* @param bool $short
*
* @return string
* @throws Exception
*/
public function getDateDiff(
int $first,
int $second = 0,
bool $short = false,
array $words = []
): string {
if ($second === 0) {
$first = time() - $first;
$second = time();
}
$f = new DateTime('@' . $first);
$s = new DateTime('@' . $second);
$duration = $second - $first;
if ($short) {
$minutes = $this->get('minutes', $words, 'M');
$hours = $this->get('hours', $words, 'H');
$days = $this->get('days', $words, 'D');
if ($duration < 60) {
return $f->diff($s)->format('<1' . $minutes);
}
if ($duration < 3600) {
return $f->diff($s)->format('%i' . $minutes);
}
if ($duration < 86400) {
return $f->diff($s)->format('%h' . $hours . ', %i' . $minutes);
}
return $f->diff($s)->format('%a' . $days . ', %h' . $hours . ', %i' . $minutes);
}
$seconds = $this->get('seconds', $words, 'seconds');
$minutes = $this->get('minutes', $words, 'minutes');
$hours = $this->get('hours', $words, 'hours');
$days = $this->get('days', $words, 'days');
if ($duration < 60) {
return $f->diff($s)->format('%s ' . $seconds);
}
if ($duration < 3600) {
return $f->diff($s)->format('%i ' . $minutes . ' and %s ' . $seconds);
}
if ($duration < 86400) {
return $f->diff($s)->format('%h ' . $hours . ', %i ' . $minutes . ' and %s ' . $seconds);
}
return $f->diff($s)->format(
'%a ' . $days .
', %h ' . $hours .
', %i ' . $minutes .
' and %s ' . $seconds
);
}
}