Created
January 26, 2015 14:28
-
-
Save kejyun/d21d24c200c716b2052b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Repositories\Pokemon; | |
// model/Repositories/Pokemon/PokemonRepository.php | |
use Illuminate\Database\Eloquent\Model; | |
use \stdClass; | |
/** | |
* Our pokemon repository, containing commonly used queries | |
* 我們的 pokemon 資源庫,包含一些常用的查詢 | |
*/ | |
class PokemonRepository implements PokemonInterface | |
{ | |
// Our Eloquent pokemon model | |
// 我們的 pokemon Eloquent 模型 | |
protected $pokemonModel; | |
/** | |
* Setting our class $pokemonModel to the injected model | |
* 設定我們的模型,將它注入到 $pokemonModel中 | |
* | |
* @param Model $pokemon | |
* @return PokemonRepository | |
*/ | |
public function __construct(Model $pokemon) | |
{ | |
$this->pokemonModel = $pokemon; | |
} | |
/** | |
* Returns the pokemon object associated with the passed id | |
* 回傳傳入 ID 相關的 pokemon 物件 | |
* | |
* @param mixed $pokemonId | |
* @return Model | |
*/ | |
public function getPokemonById($pokemonId) | |
{ | |
return $this->convertFormat($this->pokemonModel->find($pokemonId)); | |
} | |
/** | |
* Returns the pokemon object associated with the pokemonName | |
* 回傳傳入 pokemonName 相關的 pokemon 物件 | |
* | |
* @param string $pokemonName | |
*/ | |
public function getPokemonByName($pokemonName) | |
{ | |
// Search by name | |
$pokemon = $this->pokemonModel->where('name', strtolower($pokemonName)); | |
if ($pokemon) | |
{ | |
// Return first found row | |
return $this->convertFormat($pokemon->first()); | |
} | |
return null; | |
} | |
/** | |
* Converting the Eloquent object to a standard format | |
* 轉換 Eloquent 物件為標準格式 | |
* | |
* @param mixed $pokemon | |
* @return stdClass | |
*/ | |
protected function convertFormat($pokemon) | |
{ | |
if ($pokemon == null) | |
{ | |
return null; | |
} | |
$object = new stdClass(); | |
$object->id = $pokemon->id; | |
$object->name = $pokemon->name; | |
return $object; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment