tlmarker1968

One of my code files

Jan 20th, 2021
1,001
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     /**
  3.      * Phoenix Hospitality Management System
  4.      * Phoenix REST API - API Department Repository file
  5.      * Written in PHP using the Slim micro-framework
  6.      *
  7.      * @author Troy L. Marker
  8.      * @version 1.0.0
  9.      * @since 0.5.0
  10.      * @copyright Copyright (c) 2020-2021 By Troy Marker Enterprises
  11.      *
  12.      * Declare name space and Import needed functions/classes
  13.      */
  14.     namespace App\Domain\Repository;
  15.  
  16.     use PDO;
  17.    
  18.     /**
  19.      * This class provides a repository to the departments Database table
  20.      */
  21.     class Department {
  22.  
  23.         protected $connection;
  24.  
  25.         /**
  26.          * Class constructor
  27.          * @param PDO $connection - The database connection
  28.          */
  29.         public function __construct(PDO $connection) {
  30.             $this->connection = $connection;
  31.         }
  32.  
  33.         /**
  34.          * This method inserts a new department into the database
  35.          * @param array $Params - Method Parameters
  36.          * @return array $AddRetval - Method Return value
  37.          */
  38.         public function add(array $Params): string {
  39.             $SQLVars = ['department' => $Params['department']];
  40.             $SQLStatement = "SELECT id FROM " . strtolower($Params['db']) . "_departments WHERE department=:department";
  41.             $SQLResult = $this->connection->prepare($SQLStatement);
  42.             $SQLResult->execute($SQLVars);
  43.             if ($SQLResult->rowCount() > 0) {
  44.                 return false;
  45.             }
  46.             $SQLStatement = "INSERT INTO " . strtolower($Params['db']) . "_departments SET department=:department;";
  47.             $this->connection->prepare($SQLStatement)->execute($SQLVars);
  48.             return $Params['department'];
  49.         }
  50.  
  51.         /**
  52.          * This method returns a list of departments
  53.          * @param array $Params - Method Parameters
  54.          * @return array $ListsRetval - Method Return value
  55.          */
  56.         public function lists(array $Params): array {
  57.             if (isset($Params['id'])) {
  58.                 $SQLVars = ['id' => $Params['id']];
  59.                 $SQLStatement = "SELECT department FROM " . strtolower($Params['db']) . "_departments WHERE id=:id";
  60.                 $SQLResult = $this->connection->prepare($SQLStatement);
  61.                 $SQLResult->execute($SQLVars);
  62.                 return $SQLResult->fetch(PDO::FETCH_ASSOC);
  63.             }
  64.             $SQLStatement = "SELECT * FROM " . strtolower($Params['db']) . "_departments ORDER BY id";
  65.             $SQLResult = $this->connection->prepare($SQLStatement);
  66.             $SQLResult->execute();
  67.             return $SQLResult->fetchAll(PDO::FETCH_ASSOC);    
  68.         }
  69.  
  70.         /**
  71.          * This method will remove a department from the database
  72.          * @param array $Params - Method parameters
  73.          * @return string $RemoveRetval - The name of the department removed.
  74.          */
  75.         public function remove(array $Params): string {
  76.             $SQLVars = ['id' => $Params['id']];
  77.             $SQLStatement = "SELECT department FROM " . strtolower($Params['db']) . "_departments WHERE id=:id";
  78.             $SQLResult = $this->connection->prepare($SQLStatement);
  79.             $SQLResult->execute($SQLVars);
  80.             $SQLReturn = $SQLResult->fetch(PDO::FETCH_ASSOC);
  81.             $SQLStatement = "DELETE FROM " . strtolower($Params['db']) . "_departments WHERE id=:id";
  82.             $SQLResult = $this->connection->prepare($SQLStatement);
  83.             $SQLResult->execute($SQLVars);
  84.             return $SQLReturn['department'];
  85.         }
  86.        
  87.  
  88.        /**
  89.          * The method update all fields in a department record
  90.          * @param array $Params - Method parameters
  91.          * @return array $ReplaceRetval - The method results
  92.          */
  93.         public function replace(array $Params): array {
  94.             $SQLVars = ['id' => $Params['id'], 'department' => $Params['department']];
  95.             $SQLStatement = "UPDATE " . strtolower($Params['db']) . "_departments SET department=:department WHERE id=:id";
  96.             $SQLResult = $this->connection->prepare($SQLStatement);
  97.             $SQLResult->execute($SQLVars);
  98.             return $this->lists($Params);
  99.         }
  100.  
  101.         /**
  102.          * This method will change the name of a department in the database
  103.          * @param array $Params - Method parameters
  104.          * @return array - Call success status with message
  105.          */
  106.         public function update(array $Params): array {
  107.             $SQLVars = ['id' => $Params['id'], 'department' => $Params['department']];
  108.             $SQLStatement = "UPDATE " . strtolower($Params['db']) . "_departments SET department=:department WHERE id=:id";
  109.             $SQLResult = $this->connection->prepare($SQLStatement);
  110.             $SQLResult->execute($SQLVars);
  111.             return $this->lists($Params);
  112.         }
  113.        
  114.         /**
  115.          * This method will check if a department is in the database
  116.          * @param array $Params - Method parameters
  117.          * @return boolean Method result
  118.          */
  119.         public function check(array $Params): bool {
  120.             $SQLVars = ['department' => $Params['department']];
  121.             $SQLStatement = "SELECT * FROM " . strtolower($Params['db']) . "_departments WHERE department=:department";
  122.             $SQLResult = $this->connection->prepare($SQLStatement);
  123.             $SQLResult->execute($SQLVars);
  124.             if ($SQLResult->rowCount() != 0) {
  125.                 return true;
  126.             } else {
  127.                 return false;
  128.             }
  129.         }
  130.     }
RAW Paste Data