April 22, 2018 —John Koster
The mapInto
method is similar to the transform
method in that allows you to quickly convert, or project, the contents of the collection to a new type. However, instead of building up the resulting type instance yourself, the mapInto
method creates a new instance of the specified class and supplies each collection element's value and key as the arguments to the type's constructor.
1public function mapInto(2 $class3);
In the following example, we will take advantage of The Open Movie Database (OMDb) API (learn more about this API and get an API key at https://www.omdbapi.com/) to retrieve information above movie titles. We will do this with the following API wrapper class:
The app/Movie.php
Class:
1class Movie { 2 3 /** 4 * The title of the movie. 5 * 6 * @var string 7 */ 8 protected $title = ''; 9 10 /**11 * The OMDB API key.12 *13 * @var string14 */15 private $omdbApiKey = '';16 17 /**18 * The API base URL.19 *20 * @var string21 */22 private $baseUrl = 'https://www.omdbapi.com/' .23 '?t=<TITLE>&apiKey=<KEY>';24 25 /**26 * Indicates whether or the results27 * have been fetched from the API.28 *29 * @var bool30 */31 private $retrievedResults = false;32 33 /**34 * The results of the API call.35 *36 * @var null|array37 */38 private $apiResults = null;39 40 public function __construct($title)41 {42 $this->title = $title;43 }44 45 /**46 * Gets the API URL for the configured47 * key and movie title.48 *49 * @return string50 */51 private function getUrl() {52 return strtr($this->baseUrl, [53 '<TITLE>' => $this->title,54 '<KEY>' => $this->omdbApiKey55 ]);56 }57 58 /**59 * Retrieves the API results if they60 * have not already been fetched.61 */62 private function checkResults() {63 if ($this->retrievedResults === false) {64 $apiResponse = with(new GuzzleHttp\Client)65 ->get($this->getUrl());66 67 $this->apiResults = json_decode(68 $apiResponse->getBody(), true69 );70 71 $this->retrievedResults = true;72 }73 }74 75 function __get($name)76 {77 // This will let us "lazy-load" the API results.78 if ($this->apiResults == null ||79 array_key_exists(80 $name,81 $this->apiResults) === false) {82 return null;83 }84 85 return $this->apiResults[$name];86 }87 88}
In order to follow along, you will need to sign up for a free API key at https://www.omdbapi.com/apikey.aspx. Once you have an obtained an API key, you will need to set the $omdbApiKey
private property of the Movie
class.
While the Movie
class may look complicated, we are simply issuing a GET
request to the OMDb API and providing a proxy to the API results. We can use the mapInto
collection method to convert a collection of movie titles into a Movie
instance which will let us gather more information about each of the movies:
1$movies = collect([2 'The Great Outdoors',3 'Uncle Buck'4])->mapInto(Movie::class)->each(function ($movie) {5 echo $movie->Title . ' ('. $movie->Year, ')<br />';6});
After the above code has executed in a browser, we would see results similar to the following:
1The Great Outdoors (1988)2Uncle Buck (1989)
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.