1. Go to this page and download the library: Download jeanisahakyan/motion-dots library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jeanisahakyan / motion-dots example snippets
// src/YourProcessor.php
namespace YourNamespace;
use MotionDots\Processor\Processor;
use MotionDots\Schema\Schema;
use API\Methods\Users;
use MotionDots\Method\System\System;
class YourProcessor {
public function handleRequest() {
// Set headers
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=UTF-8');
try {
// Merge GET, POST, and FILES parameters
$params = array_merge($_GET, $_POST, $_FILES);
// Create a schema and add methods
$schema = Schema::create()->addMethods([
new Users(),
// Add other method classes here
]);
// Instantiate the processor
$processor = new Processor($schema, '.');
// Set initial context values if needed
$processor->getContext()->setMany([
'requestStartTime' => microtime(true),
// Add other context variables here
]);
// Determine the method to invoke
if (preg_match('/\/api\/([a-zA-Z\.]+)/i', $_SERVER['REQUEST_URI'], $matches)) {
[, $method] = $matches;
} else {
$method = 'system.getSchema'; // Default method
}
// Invoke the method and output the response
$response = $processor->invokeProcess($method, $params);
echo json_encode($response);
} catch (\Exception $exception) {
// Handle exceptions and output error response
echo json_encode([
'error' => [
'error_code' => $exception->getCode(),
'error_message' => $exception->getMessage(),
]
]);
}
}
}
YourNamespace\YourProcessor;
$processor = new YourProcessor();
$processor->handleRequest();
// src/API/Methods/Users.php
namespace API\Methods;
use MotionDots\Method\AbstractMethod;
use API\Responses\UserResponse;
use API\Types\EmailType;
use API\Types\PasswordType;
use API\Enums\UserStatus;
class Users extends AbstractMethod {
// Methods will be defined here
}
public function registerUser(EmailType $email, PasswordType $password): UserResponse {
$emailValue = $email->parse();
$passwordValue = $password->parse();
// Business logic here
return UserResponse::create()
->setEmail($emailValue)
->setStatus(UserStatus::ACTIVE);
}
public function registerUser(EmailType $email, PasswordType $password): UserResponse {
// Set data in context
$request_time = $this->context->get('requestTime');
// Rest of the method...
}
public function loginUser(EmailType $email, PasswordType $password): UserResponse {
$emailValue = $email->parse();
$passwordValue = $password->parse();
// Business logic here
// Set user ID in context after successful login
$this->context->set('userId', $userId);
return UserResponse::create()
->setId($userId)
->setEmail($emailValue)
->setStatus(UserStatus::ACTIVE);
}
public function updateUserStatus(int $userId, UserStatus $status): UserResponse {
// Access user ID from context if needed
$currentUserId = $this->context->get('userId');
// Business logic here
return UserResponse::create()
->setId($userId)
->setStatus($status);
}
// src/API/Responses/UserResponse.php
namespace API\Responses;
use MotionDots\Response\AbstractResponse;
use API\Enums\UserStatus;
class UserResponse extends AbstractResponse {
public int $id;
public string $email;
public UserStatus $status;
public function setId(int $id): self {
$this->id = $id;
return $this;
}
public function setEmail(string $email): self {
$this->email = $email;
return $this;
}
public function setStatus(UserStatus $status): self {
$this->status = $status;
return $this;
}
}
// src/API/Types/EmailType.php
namespace API\Types;
use MotionDots\Type\AbstractType;
use MotionDots\Exception\ErrorException;
class EmailType extends AbstractType {
public function parse(): string {
$email = filter_var($this->field, FILTER_VALIDATE_EMAIL);
if ($email === false) {
throw new ErrorException(ErrorException::PARAM_INCORRECT, "`{$this->param_name}` must be a valid email address");
}
return $email;
}
}
// src/API/Enums/UserStatus.php
namespace API\Enums;
enum UserStatus: string {
case ACTIVE = 'active';
case INACTIVE = 'inactive';
case BANNED = 'banned';
}
public function updateUserStatus(int $userId, UserStatus $status): UserResponse {
// Business logic here
return UserResponse::create()
->setId($userId)
->setStatus($status);
}
// Instantiate the processor
$processor = new Processor($schema, '.');
// Invoke the method and output the response
$response = $processor->invokeProcess($method, $params);
use MotionDots\Exception\ErrorException;
if (!$user) {
throw new ErrorException(ErrorException::PARAM_INCORRECT, "User not found");
}
$schema = Schema::create()
->addMethods($methods);
$processor = new Processor($schema, '.');
$files_folder = "./static/api-schema/"; // any existing folder relative to current working directory
Generator::create()
->excludeSpaces('accounts', 'users') // default is 'system'
->setIsVerbose(false) // default is true
->setFilesPath('./static/api-schema') // default is './api-schema'
->generate($processor);