Tools - WaZeR-Adrien/Lightwork-api GitHub Wiki

All detailed classes here are positioned in /kernel/Tools

File

You can easier upload files with the File class.

  • The signature of the constructor is :
/**
 * @param array $file
 * @param int $size in Byte
 * @param array $extensions
 * @param array $dim [width, height]
 * @param string $dir
 */
public function __construct($file, $size, $extensions, $dim, $dir)
  • To upload a file, you must create a new instance of File and use the upload() method :
// Create new file
$file = new File($request->getFiles()->picture, 2000000, ["JPG", "PNG", "GIF"], [2000, 2000], "pictures/profile");

// If there is no error, $upload contain the name of the file uploaded
// Else $upload contain an array of errors
$upload = $file->upload();

// Set the result of the upload in data of the response
$response->setData($upload);

// File is uploaded
if (empty($upload["error"]) {
    return $response->render("S_PO001")->toJson();
} else {
    return $response->render("ERROR CODE HERE LIKE E_...")->toJson();
}

Mail

You can easier send a mail with the Mail class.

  • The signature of the constructor is :
/**
 * @param array $from
 * @param array $to
 * @param array $reply
 * @param $subject
 * @param $body
 * @param null $altbody
 */
public function __construct($from = [], $to = [], $reply = [], $subject, $body, $altbody = null)
  • To send a mail, you must create a new instance of Mail and use the send() method :
// Create new mail
$mail= new Mail([
    "email" => "[email protected]",
    "name" => "Foo"
], [
    [
        "email" => "[email protected]",
        "name" => "User1"
    ],[
        "email" => "[email protected]",
        "name" => "User2"
    ],[
        "email" => "[email protected]",
        "name" => "User3"
    ]
], [
    "email" => "[email protected]",
    "name" => "Foo"
], "Information", "My information with <strong>HTML</strong>", "My information without HTML");

// Send the mail
$mail->send();

// Mail sent
return $response->render("S_PO001")->toJson();

Captcha

Don't forget to fill the config before calling the Captcha class. This class allows you to check if the code of response of the captcha is valid. You must use the static method check() :

if (Captcha::check($request->getBodies()->codeCaptcha) {
    // Success ...
} else {
    // Error ...
}

Features of Utils :

  • public static createToken($withIp = true) realize a random token, with or without ip hashed (sha1)
$token = Utils::createToken();
  • public static between($needle, $min = null, $max = null) check if the needle is between the min and the max
$num = 24
if (Utils::between($num, 10, 30)) {
    echo 'success';
}
  • public static dateUs($date, $key = 'date') convert the french date to us date (if is not a french date, generate an error)
$dateUs = Utils::dateUs($date);
or
$dateUs = Utils::dateUs($dateLogin, 'date_login');
  • public static toTimestamp($date) convert us date to timestamp
$dateUs = Utils::dateUs($date);
$timestamp = Utils::toTimestamp($dateUs);
  • public static setInterval($callback, $seconds) it is the equivalent to the setInterval in JS. To stop the interval, return true. Example :
Utils::setInterval(function($time) {
    echo $time;
    return ($time == 30); // Stop the interval when the time is 30 seconds
}, 10);
  • public static getHeader($header = null) get the content of an header with its key
$token = Utils::getHeader('token');
  • private static _getallheaders() get all headers
  • public static match($pattern, $subject) check if subject match with pattern previously enter in the kernel/Config.php file
if (
    Utils::match(Config::getReg()['email'], $email)
) {
    echo 'success';
}
  • public static removeAttrs($array = [], $attrs = [], $type = 'obj') remove all attributes of an array. The attributes is pass in an array like ['email', 'password']...
$users = User::getAll();
$users = Utils::removeAttrs($users, ['email', 'password'];
  • public static in_multi_array($array, $needle) the same function that in_array but for multidimensional array
$key = Utils::in_multi_array($users, '[email protected]');
if ($key) {
    var_dump($users[$key];
}
  • public static setValuesInObject(&$haystack, $needle, $escape = []) set the values of existing properties in object ($haystack) with the other object $needle which contain the same keys
public static function addUser($post) {
    $user = new User();

    // Set all values sent in post in the user object
    Utils::setValuesInObject($user, $post, ['id'];
    
    $user->store();
}
  • public static checkPropsInObject($haystack, $needle, $type = []) check the existing properties in object ($haystack) with the $needle array which contain the keys to check. Check also the type if necessary (if the $type contain the key)
public static function editUserBio($put) {
    // Render an error E_A005 if the property biography is not in the put object
    Utils::checkPropsInObject($put, ['biography'], ['String']);
}
  • public static setRegex($type) set the regex with the type (like : String => '\w+')
$regex = Utils::setRegex('String');

echo $regex;
// Output : \w+
  • public static parse_http_put() parse and get all data sent in put
⚠️ **GitHub.com Fallback** ⚠️