PHP code example of ionews / light-service-php

1. Go to this page and download the library: Download ionews/light-service-php 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/ */

    

ionews / light-service-php example snippets


class GenerateRandomPassword extends Action {
  protected function perform() {
    $length = 8;
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $password = '';

    for ($i = 0; $i < $length; $i++) {
      $password .= $chars[rand(0, strlen($chars) - 1)];
    }

    $this->context->password = $password;
  }
}

class UpdateUserPassword extends Action {
  protected function perform() {
    $user_id = $this->context->user_id;
    $password = $this->context->password;
    // access the database using the method of your choice and update the password
  }
}

class ResetUserPassword extends Organizer {
  protected $organize = ['GenerateRandomPassword', 'UpdateUserPassword', 'EmailUserWithPassword'];
}

class UserController extends BaseController {
  public function resetPassword() {
    $result = ResetUserPassword::execute(['user_id' => $this->request->id]);

    if ($result->success()) {
      // use $result->getContext() to access the results and redirect the app
    } else {
      // error, use $result->getFailureMessage() to access any failure message
    }
  }
}

class SomeAction extends Action {
  protected function perform() {
    $this->fail('Oh noes');
  }
}

$result = SomeAction::execute([]);
$result->success(); // false
$result->failure(); // true
$result->halted(); // false
$result->getFailureMessage(); // 'Oh noes'

class UpdateUserPassword extends Action {
  protected function perform() {
    $user_id = $this->context->user_id;
    $password = $this->context->password;
    // access the database using the method of your choice and update the password
  }

  protected function rollback() {
    // undo the update password
  }
}

class SomeAction extends Action {
  protected function perform() {
    $this->halt();
  }
}

$result = SomeAction::execute([]);
$result->success(); // true
$result->failure(); // false
$result->halted(); // true

class SomeAction extends Action {
  protected function before() {
    // any setup
  }

  protected function perform() {
    // perform
  }

  protected function after() {
    // cleanup
  }
}

class UpdateUserPassword extends Action {
  protected $expects = ['user_id', 'password'];

  protected function perform() {
    $user_id = $this->context->user_id;
    $password = $this->context->password;
    // access the database using the method of your choice and update the password
  }
}

$result = UpdateUserPassword::execute(['user_id' => 1]);
$result->success(); // false
$result->getFailureMessage(); // 'Expectations were not met'

class GenerateRandomPassword extends Action {
  protected $promises = ['password'];

  protected function perform() {
    $length = 8;
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $password = '';

    for ($i = 0; $i < $length; $i++) {
      $password .= $chars[rand(0, strlen($chars) - 1)];
    }

    //$this->context->password = $password;
  }
}

$result = GenerateRandomPassword::execute([]);
$result->success(); // false
$result->getFailureMessage(); // 'Promises were not met'

class SomeAction extends IteratorAction {
  protected $over = 'key_of_the_array_in_context'

  protected function each($key, $value) {
    ...
  }
}