combine() public static method

Indexes an array depending on the values it contains.
public static combine ( array $data, callable $cb, boolean $overwrite = true ) : array
$data array Data.
$cb callable Function to combine values.
$overwrite boolean Should duplicate keys be overwritten ?
return array Indexed values.
 /**
  *
  */
 public function testCombine()
 {
     $users = [['id' => 1, 'name' => 'a'], ['id' => 2, 'name' => 'b'], ['id' => 3, 'name' => 'b']];
     $closure = function ($user) {
         (yield $user['name'] => $user['id']);
     };
     $expected = ['a' => 1, 'b' => 3];
     // overwriting existing names
     $this->assertEquals($expected, Transform::combine($users, $closure));
     $expected = ['a' => 1, 'b' => 2];
     // not overwriting existing names
     $this->assertEquals($expected, Transform::combine($users, $closure, false));
 }
Exemplo n.º 2
0
 /**
  *	Builds a media from the given meta tags.
  *
  *	@param array $metas Meta tags.
  *	@return Media Media.
  */
 protected function _media(array $metas)
 {
     $metas = Transform::combine($metas, function ($Meta) {
         (yield $Meta->get('property') => $Meta->get('content'));
     });
     return new Media($metas);
 }
Exemplo n.º 3
0
 /**
  *	Builds a media from the given meta tags.
  *
  *	@param array $metas Meta tags.
  *	@return Media Media.
  */
 protected function _media(array $metas)
 {
     $metas = Transform::combine($metas, function ($Meta) {
         (yield $Meta->get($this->_metaAttribute) => $Meta->get('content'));
     });
     return new Media($metas);
 }
 /**
  *	Fetches informations about the given URLs.
  *
  *	@param array $urls An array of URLs to fetch informations from.
  *	@param array $options Custom options to be interpreted by a provider.
  *	@return array An array of informations, indexed by URL.
  */
 public function extractAll(array $urls, array $options = [])
 {
     return Transform::combine($urls, function ($url) use($options) {
         (yield $url => $this->extract($url, $options));
     });
 }