Commit 124d7563 authored by Dainis Abols's avatar Dainis Abols
Browse files

Randomizer fix and helper updates

parent 81402ace
......@@ -34,10 +34,12 @@ class QRCodeGenerator
*
* @param string $url
*/
static public function create()
static public function create($return = false)
{
// Sdet headers
header('Content-type: image/png');
if (!$return) {
header('Content-type: image/png');
}
// Read url from request
$r = $_REQUEST['r'];
......@@ -50,28 +52,32 @@ class QRCodeGenerator
$QR = imagecreatefrompng('https://chart.googleapis.com/chart?cht=qr&chld=H|1&chs='.self::$size.'&chl='.urlencode($url));
// Add logo
$logoPath = Environment::getPublicPath().'/typo3conf/ext/lu_api/Resources/Public/Icons/'.self::$logo;
$logo = imagecreatefromstring(file_get_contents($logoPath));
if (!$return) {
$logoPath = Environment::getPublicPath().'/typo3conf/ext/lu_api/Resources/Public/Icons/'.self::$logo;
$logo = imagecreatefromstring(file_get_contents($logoPath));
// Set image sizes
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
// Set image sizes
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
// Set logo width (this is capped)
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Set logo width (this is capped)
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width / 3;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width / 3;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
// Combine images and create output
imagecopyresampled($QR, $logo, $QR_width / 3, $QR_height / 3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
// Combine images and create output
imagecopyresampled($QR, $logo, $QR_width / 3, $QR_height / 3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
}
imagepng($QR);
imagedestroy($QR);
// Stop script, to prevent excessive output
exit();
if (!$return) {
exit();
}
}
}
......@@ -19,11 +19,13 @@ class Randomizer
/**
* Generate random string for passwords or other usage
*
* @param int $length
* @param int $length
* @param bool $onlyAlphabet
* @param bool $onlyString
*
* @return string
*/
public function getRandomString(int $length = 16, $onlyAlphabet = false, $onlyString = false)
public function getRandomString(int $length = 16, bool $onlyAlphabet = false, bool $onlyDigits = false): string
{
$string = '!@#$%*&abcdefghijklmnpqrstuwxyzABCDEFGHJKLMNPQRSTUWXYZ23456789';
......@@ -33,12 +35,12 @@ class Randomizer
}
// Check if only digits required
if ($onlyAlphabet) {
if ($onlyDigits) {
$string = '0123456789';
}
$data = substr(str_shuffle($string), 0, $length);
$data = substr(str_shuffle(str_repeat($x = $string, ceil($length / strlen($x)))), 1, $length);
return ReturnPretty::json($data, 200);
return ReturnPretty::json($data);
}
}
......@@ -39,12 +39,17 @@ class DataHelper
$month = $this->months($date->format('n'));
$year = $date->format('Y');
$result = $weekday.' '.$year.'. gada '.$day.'. '.$month;
return (string)$result;
return $weekday.' '.$year.'. gada '.$day.'. '.$month;
}
private function weeks($week)
/**
* Return day names by number
*
* @param $week
*
* @return string
*/
private function weeks($week): string
{
$weeks = [
1 => 'Pirmdiena',
......@@ -110,14 +115,12 @@ class DataHelper
$result = $queryBuilder->execute()->fetchAllAssociative();
// Read and load groups
foreach($result as $row) {
foreach ($result as $row) {
$return[] = $row['name'];
}
// Implode and return
$return = implode(", ", $return);
return (string)$return;
return implode(", ", $return);
}
/**
......@@ -158,7 +161,7 @@ class DataHelper
{
// Decode result
$decoded = json_decode($jsonData, true);
if ($decoded['code'] == 200) {
if (isset($decoded['code']) && $decoded['code'] == 200) {
$data = $decoded['data'];
} else {
$data = null;
......
......@@ -19,7 +19,7 @@ class FlexFormReader
*
* @var string
*/
private $defaultLanguage = 'lDEF';
private string $defaultLanguage = 'lDEF';
/**
* Decode FlexForm
......@@ -36,15 +36,15 @@ class FlexFormReader
// Get each sheet
if (!empty($piFlexForm->data)) {
foreach ($piFlexForm->data as $sKey => $sheet) {
foreach ($piFlexForm->data as $sheet) {
// Get each language
foreach ($sheet->sheet->language as $lKey => $language) {
foreach ($sheet->sheet->language as $language) {
$langKey = (string)$language->attributes();
// Get current lang field data
if ($langKey == $currentLanguage) {
foreach ($language->field as $fKey => $field) {
foreach ($language->field as $field) {
$name = (string)$field->attributes();
$value = (string)$field->value;
$result[$name] = $value;
......@@ -54,8 +54,8 @@ class FlexFormReader
}
}
// Return back to the request
return ReturnPretty::json($result, 200);
// Return to the request
return ReturnPretty::json($result);
}
}
\ No newline at end of file
......@@ -40,7 +40,7 @@ class ReturnPretty
*
* @return string
*/
static public function json($data = null, $code = 200, $plain = false)
static public function json($data = null, int $code = 200, bool $plain = false)
{
// return the encoded json
if ($plain) {
......@@ -57,7 +57,5 @@ class ReturnPretty
/**
* @TODO: Implement other return formats. Also can add another main method that returns in default request
*/
static public function xml()
{
}
static public function xml() {}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment