You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WF-Website/src/Model/CharacterModel.php

99 lines
3.6 KiB

<?php
namespace Model;
use Entity\CharacterEntity;
use Gateway\CharacterGateway;
use Gateway\Gateway;
class CharacterModel extends Model {
/**
* Creates a character by calling the gateway's create method.
*
* @param int $id_character The unique identifier for the character.
* @param string $name The name of the character.
* @param string $img_char The file path of the image associated with the character.
*
* @return bool Returns true if the character was successfully created, otherwise false.
*/
public function createCharacter(int $id_character, string $name, string $img_char) : bool {
return $this->gateway->create($id_character, $name, $img_char);
}
/**
* Retrieves a character by its unique identifier.
*
* @param int $id_character The unique identifier of the character to retrieve.
*
* @return CharacterEntity|null Returns a CharacterEntity object if found, otherwise null.
*/
public function getCharacterById(int $id_character) : ?CharacterEntity {
// Attempt to find the character by ID using the gateway's findById method.
$c = $this->gateway->findById($id_character);
// If the character is found, create and return a new CharacterEntity object.
if ($c) {
return new CharacterEntity(
$c[0]['id_caracter'], // Character ID
$c[0]['caracter'], // Character name
$c[0]['id_img'] // Image ID associated with the character
);
}
// If no character is found, return null.
return null;
}
/**
* Retrieves all characters from the database and returns them as an array of CharacterEntity objects.
*
* @return CharacterEntity[] Returns an array of CharacterEntity objects representing all characters.
*/
public function getAllCharacters() : array {
// Fetch all characters using the gateway's findAll method.
$res = $this->gateway->findAll();
// Initialize an empty array to store the CharacterEntity objects.
$charac = [];
// Loop through each character in the result and create a CharacterEntity for each.
foreach ($res as $c) {
$charac[] = new CharacterEntity(
$c['id_caracter'], // Character ID
$c['caracter'], // Character name or description
$c['id_img'] // Image ID associated with the character
);
}
// Return the array of CharacterEntity objects.
return $charac;
}
/**
* Retrieves a character by its name.
*
* @param string $name The name of the character to retrieve.
*
* @return CharacterEntity|null Returns a CharacterEntity object if a character with the given name is found, otherwise null.
*/
public function getCharacterByName(string $name) : ?CharacterEntity {
// Attempt to find the character by name using the gateway's findByName method.
$c = $this->gateway->findByName($name);
// If the character is found, create and return a new CharacterEntity object.
if ($c) {
return new CharacterEntity(
$c[0]['id_caracter'], // Character ID
$c[0]['caracter'], // Character name or description
$c[0]['id_img'] // Image ID or path associated with the character
);
}
// If no character is found, return null.
return null;
}
}
?>