Skip to content

Instantly share code, notes, and snippets.

@kejyun
Created January 26, 2015 14:43
Show Gist options
  • Save kejyun/7030c4b5b79a31b7094a to your computer and use it in GitHub Desktop.
Save kejyun/7030c4b5b79a31b7094a to your computer and use it in GitHub Desktop.
<?php namespace Services\Pokemon;
// model/Services/Pokemon/PokemonService.php
use Repositories\Pokemon\PokemonInterface;
/**
* Our PokemonService, containing all useful methods for business logic around Pokemon
* 我們的 PokemonService 服務,包含所有在 Pokemon 的商業邏輯中有用的方法
*/
class PokemonService
{
// Containing our pokemonRepository to make all our database calls to
// 包含我們的 pokemonRepository 資源庫,去讓我們的資料庫去呼叫他
protected $pokemonRepo;
/**
* Loads our $pokemonRepo with the actual Repo associated with our pokemonInterface
* 使用真正的 Repo 資源庫去載入到我們的 $pokemonRepo,並關聯到我們的 pokemonInterface 介面
*
* @param pokemonInterface $pokemonRepo
* @return PokemonService
*/
public function __construct(pokemonInterface $pokemonRepo)
{
$this->pokemonRepo = $pokemonRepo;
}
/**
* Method to get pokemon based either on name or ID
* 透過 ID 或名稱取得 pokemon 資料
*
* @param mixed $pokemon
* @return string
*/
public function getPokemon($pokemon)
{
// If pokemon variable is numeric, assume ID
// 假如變數是整數
if (is_numeric($pokemon))
{
// Get pokemon based on ID
// 使用 ID 去取得 Pokemon
$pokemon = $this->pokemonRepo->getPokemonById($pokemon);
}
else
{
// Since not numeric, lets try get the pokemon based on Name
// 非整數,嘗試使用名稱去取得 pokemon
$pokemon = $this->pokemonRepo->getPokemonByName($pokemon);
}
// If Eloquent Object returned (rather than null) return the name of the pokemon
// 若 Eloquent 物件有回傳資料 (非 null),則回傳 pokemon 的名稱
if ($pokemon != null)
{
return $pokemon->name;
}
// If nothing found, return this simple string
// 如果沒有找到任何資料,則回傳這串簡單的字串
return 'Pokemon Not Found';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment