Programming Standards: Difference between revisions
(→PHP) |
|||
| (18 intermediate revisions by the same user not shown) | |||
| Line 56: | Line 56: | ||
=== PHP === | === PHP === | ||
Every PHP function: | == PHP Service Function Guidelines == | ||
Every PHP service function should follow these rules: | |||
* It must be in its own file. | |||
* The file name and the function name should be the same. | |||
* It should return: <code>Closure::fromCallable('fileName')</code> | |||
* The function must include the standard service header. | |||
<pre> | |||
/** | |||
* fileName.php | |||
* @author [FirstName LastName] <firstName.lastName@seedburysquare.com> | |||
* @copyright Seedbury Square, LLC. All Rights Reserved. | |||
* | |||
* @version YYYY-MM-DD Initial Version | |||
*/ | |||
</pre> | |||
* Inside the function, include the following structure: | |||
** A validation section | |||
<pre> | |||
$validation = validate[FileName]Params($params); | |||
if (!$validation['success']) return $validation; | |||
</pre> | |||
:* The service logic (body) | |||
:* A return statement that returns a <code>uniformReturnObject</code> | |||
Additionally, everything inside the function should be wrapped in a <code>try-catch</code> block. | |||
In the <code>catch</code> block: | |||
* Log the error using <code>error_log()</code> | |||
* Return a <code>uniformReturnObject</code> where: | |||
** <code>success</code> is set to <code>false</code> | |||
** <code>errorMsg</code> and <code>displayDialog</code> contain the value of <code>$e->getMessage()</code> | |||
<pre> | |||
catch (\Throwable $e) { | |||
error_log($e); | |||
return uniformReturnObject(false, null, $e->getMessage(), $e->getMessage()); | |||
} | |||
</pre> | |||
Here is a full example of how a getExampleDescriptionByExampleId would look like: | |||
<pre> | <pre> | ||
| Line 62: | Line 104: | ||
/** | /** | ||
* | * getExampleDescriptionByExampleId.php | ||
* @author [ | * @author [FirstName LastName] <firstName.lastName@seedburysquare.com> | ||
* @copyright Seedbury Square, LLC. All Rights Reserved. | * @copyright Seedbury Square, LLC. All Rights Reserved. | ||
* | * | ||
* @version YYYY-MM-DD Initial Version | * @version YYYY-MM-DD Initial Version | ||
*/ | */ | ||
require_once __DIR__ . DIRECTORY_SEPARATOR . '../../databaseConnection/DatabaseDriver.php'; | |||
return Closure::fromCallable(' | require_once __DIR__ . DIRECTORY_SEPARATOR . '../../utilities/constants/databaseConstants.php'; | ||
function | require_once __DIR__ . DIRECTORY_SEPARATOR . '../../utilities/helpers/helpers.php'; | ||
return Closure::fromCallable('getExampleDescriptionByExampleId'); | |||
function getExampleDescriptionByExampleId($params, $dbDriver = null) | |||
{ | { | ||
try { | try { | ||
$validation = | $validation = validateGetExampleDescriptionByExampleIdParams($params); | ||
if (!$validation['success']) return $validation; | if (!$validation['success']) return $validation; | ||
$dbDriver = $dbDriver ?? new DatabaseDriver([DATABASE_CONNECTION_CONSTANT]); | |||
$response = $dbDriver->( | |||
SELECT | |||
ex.description | |||
FROM examples ex WITH(NOLOCK) | |||
WHERE ex.explampleId = ? | |||
AND ex.isDeleted = 0;', | |||
[$params['exampleId']] | |||
); | |||
return uniformReturnObject( | return uniformReturnObject( | ||
$response['success'], | $response['success'], | ||
| Line 88: | Line 137: | ||
} catch (\Throwable $e) { | } catch (\Throwable $e) { | ||
error_log($e); | error_log($e); | ||
return | return uniformReturnObject(false, null, $e->getMessage(), $e->getMessage()); | ||
} | } | ||
}; | }; | ||
function | function validateGetExampleDescriptionByExampleIdParams($params) | ||
{ | { | ||
$errors = []; | $errors = []; | ||
$mandatory = ['exampleId']; | |||
foreach ($mandatory as $prop) { | foreach ($mandatory as $prop) { | ||
if (!isset($params[$prop])) { | if (!isset($params[$prop])) { | ||
| Line 102: | Line 150: | ||
} | } | ||
$success = empty($errors); | $success = empty($errors); | ||
$errorMsg = ' | $errorMsg = 'getExampleDescriptionByExampleId<br>Errors: <br><ol>' . implode('', $errors) . '</ol>'; | ||
return uniformReturnObject( | return uniformReturnObject( | ||
$success, | $success, | ||
| Line 110: | Line 158: | ||
); | ); | ||
} | } | ||
</pre> | </pre> | ||
Latest revision as of 20:34, 31 March 2025
Naming Convention
Constants
ROW_VALUE_DESCRIPTION_COLUMN_NAME_TABLE_NAME
CFI
- ObjectType
- - DESCRIPTION_EN_OBJECT_TYPE_CODE
- [Table]Type
- DESCRIPTION_EN_[TABLENAME]_TYPE_CODE
- i.e (FileType - MotionType - Code MOTION_FILE_TYPE_CODE)
Handling Service Response
Standards how to handle service response. Every main in any app will have algo asi:
async request(params, { transition = false, showMsg = true, setStatus = undefined, fullResponse = false } = {}) {
const serverResponse = await httpRequest(params, setStatus, REQUEST_BRIDGE_SERVICE);
if (!serverResponse?.success) console.error(serverResponse?.errorMsg ?? 'Backend service NULL');
return serverResponse?.success
? Promise.resolve(fullResponse
? serverResponse
: serverResponse.response)
: Promise.reject(serverResponse);
}
Por eso in the services should be handled like this:
[service]
.then((response) => {
// Handle successful service
})
.catch((err) => {
console.log("Error: ", err);
// Handle not successful service
});
Parameters
Javascript
Function, classes and methods...
/**
* @param {AppProps} parentProps
* @param {object} settings
**/
name(parentProps, {} = {}) {
}
Where settings means all other props.
PHP
PHP Service Function Guidelines
Every PHP service function should follow these rules:
- It must be in its own file.
- The file name and the function name should be the same.
- It should return:
Closure::fromCallable('fileName') - The function must include the standard service header.
/** * fileName.php * @author [FirstName LastName] <firstName.lastName@seedburysquare.com> * @copyright Seedbury Square, LLC. All Rights Reserved. * * @version YYYY-MM-DD Initial Version */
- Inside the function, include the following structure:
- A validation section
$validation = validate[FileName]Params($params); if (!$validation['success']) return $validation;
- The service logic (body)
- A return statement that returns a
uniformReturnObject
Additionally, everything inside the function should be wrapped in a try-catch block.
In the catch block:
- Log the error using
error_log() - Return a
uniformReturnObjectwhere:successis set tofalseerrorMsganddisplayDialogcontain the value of$e->getMessage()
catch (\Throwable $e) {
error_log($e);
return uniformReturnObject(false, null, $e->getMessage(), $e->getMessage());
}
Here is a full example of how a getExampleDescriptionByExampleId would look like:
<?php
/**
* getExampleDescriptionByExampleId.php
* @author [FirstName LastName] <firstName.lastName@seedburysquare.com>
* @copyright Seedbury Square, LLC. All Rights Reserved.
*
* @version YYYY-MM-DD Initial Version
*/
require_once __DIR__ . DIRECTORY_SEPARATOR . '../../databaseConnection/DatabaseDriver.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '../../utilities/constants/databaseConstants.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '../../utilities/helpers/helpers.php';
return Closure::fromCallable('getExampleDescriptionByExampleId');
function getExampleDescriptionByExampleId($params, $dbDriver = null)
{
try {
$validation = validateGetExampleDescriptionByExampleIdParams($params);
if (!$validation['success']) return $validation;
$dbDriver = $dbDriver ?? new DatabaseDriver([DATABASE_CONNECTION_CONSTANT]);
$response = $dbDriver->(
SELECT
ex.description
FROM examples ex WITH(NOLOCK)
WHERE ex.explampleId = ?
AND ex.isDeleted = 0;',
[$params['exampleId']]
);
return uniformReturnObject(
$response['success'],
$response['success'] ? $response['result'] : null,
$response['success'] ? false : $response['errorMsg'],
$response['success'] ? false : $response['errorMsg']
);
} catch (\Throwable $e) {
error_log($e);
return uniformReturnObject(false, null, $e->getMessage(), $e->getMessage());
}
};
function validateGetExampleDescriptionByExampleIdParams($params)
{
$errors = [];
$mandatory = ['exampleId'];
foreach ($mandatory as $prop) {
if (!isset($params[$prop])) {
$errors[] = "<li>Missing: $prop</li>";
}
}
$success = empty($errors);
$errorMsg = 'getExampleDescriptionByExampleId<br>Errors: <br><ol>' . implode('', $errors) . '</ol>';
return uniformReturnObject(
$success,
null,
$success ? null : $errorMsg,
$success ? null : $errorMsg
);
}