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; } } ?>