Skip to content

Instantly share code, notes, and snippets.

View vishwac09's full-sized avatar
🐢
Steady

Vishwa Chikate vishwac09

🐢
Steady
  • Srijan Technologies Ltd
  • Pune, India
  • X @vishwac09
View GitHub Profile
@vishwac09
vishwac09 / AuthZeroController.php
Created February 3, 2022 05:21
AuthZero Controller file
<?php
namespace Drupal\authzero\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
@vishwac09
vishwac09 / MatchExpression.php
Created February 17, 2022 11:58
CodingStandard-MatchExpression
<?php
$food = 'cake';
$return_value = match ($food) {
'apple' => 'This food is an apple',
'bar' => 'This food is a bar',
'cake' => 'This food is a cake',
};
@vishwac09
vishwac09 / OrderedClassMethods.php
Last active February 17, 2022 15:19
CodingStandard - OrderedClassMethod
<?php
/**
* Class showing member function defined in ascending order.
*/
class OrdereClassMethods {
public function a() {}
public function aa() {}
@vishwac09
vishwac09 / RestfulValidationBadWay.php
Created February 25, 2022 10:12
RestfulValdiationBadWay
<?php
class VehicleCreateRestResource {
// POST handler
public function post() {
try {
$payload = []json_decode(\Drupal::request()->getContent());
// Check if vehicle name is not empty.
if (empty($payload['name'])) {
@vishwac09
vishwac09 / RestfulValidationFair.php
Created February 25, 2022 10:15
RestfulValidationNotSoBad
<?php
class VehicleCreateRestResource {
// POST handler
public function post() {
try {
$payload = (array)json_decode($this->request->getContent());
$query = $this->request->query->all();
// Vehicle Validator pseudo code.
@vishwac09
vishwac09 / VehicleCreateSchema.php
Created February 25, 2022 10:17
VehicleCreateSchema
<?php
class VehicleCreateSchema {
// Schema file to validate POST request.
public function getCreateSchema() {
return (object) [
"type" => "object",
"properties" => (object) [
"name" => (object) [
"type" => "string",
@vishwac09
vishwac09 / ReftfulValdiationGoodWay.php
Created February 25, 2022 10:19
ReftfulValdiationGoodWay
<?php
use JsonSchema\Validator;
use VehicleCreateSchema;
class VehicleCreateRestResource {
// POST handler
public function post() {
try {
$payload = (array)json_decode($this->request->getContent());
$query = $this->request->query->all();
@vishwac09
vishwac09 / batch_api_example.php
Last active February 25, 2022 16:11
BatchAPIHookUpdate
<?php
use Drupal\node\Entity\Node;
/**
* Implements hook_update_N().
* Patch Content of Type article.
*/
function xyz_update_9001(&$sandbox) {
// Get count only if $sandbox['max'] is not set i.e the first time.
@vishwac09
vishwac09 / RestResourceHandlers.php
Created February 26, 2022 16:43
RestResourceHandlers
<?php
// pseudo code
class VehicleFetchRestResource {
// GET /vehicle
public function get(Request $request) {
try {
// #1 Authentication layer
// Validate if the request is coming from trusted source.
Authenticate($request);
@vishwac09
vishwac09 / ImportOrder.php
Last active February 27, 2022 11:07
CodingStandard-ImportOrder
<?php
use Drupal\my_custom_module_1\Service\UserService;
use Drupal\my_custom_module_1\Entity\UserEntity;
use Drupal\my_custom_module_2\Service\MessageService;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Field\FieldItemList;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;