Skip to content

IEnumerable.cast() method

Marcel Kloubert edited this page Oct 4, 2015 · 6 revisions

IEnumerable->cast($type [, $provider]) method

Casts all elements of the sequence to a new type (s. Cast()).

Syntax

public function cast(string $type
                     [ IFormatProvider $provider = null ]) : IEnumerable;

Parameters

Name Type Description
$type string The name of the target type.
$provider [[IFormatProvider System.IFormatProvider interface]]

$type

Valid values are:

  • Anything that is supported by PHP (type casting).
  • callable will cast any item to a callable. If an item is NO callable, it is wrapped into a closure that returns the value itself.
  • function is an alias for callable.
  • lazy wraps the input value to an ILazy object.
  • null is an alias for unset.

$provider

The custom format provider to use.

Result

The new sequence.

Examples

Array

use \System\Linq\Enumerable;

$item1 = new stdClass();
$item1->a = 1;
$item2 = new stdClass();
$item2->b = 2;
$item3 = new stdClass();
$item3->c = 3;

$seq = Enumerable::fromValues($item1, $item2, $item3);

foreach ($seq->cast('array') as $item) {
    // [0] array('a' => 1)
    // [1] array('b' => 2)
    // [2] array('c' => 3)
}

Boolean

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(0, 1, null);

foreach ($seq->cast('bool') as $item) {
    // [0] (false)
    // [1] (true)
    // [2] (false)
}

Callable / function

use \System\Linq\Enumerable;

class MyCallableClass {
    public function __invoke() {
        return '3';
    }
}

$seq = Enumerable::fromValues('\trim',
                              1,
                              null,
                              2.0,
                              false,
                              new MyCallableClass(),
                              new stdClass(),
                              function() {
                                  return 5;
                              },
                              '() => 6 + 0.78');

foreach ($seq->cast('callable') as $item) {
    // [0] '\trim'
    // [1] \Closure that returns 1
    // [2] \Closure that returns (null)
    // [3] \Closure that returns 2.0
    // [4] \Closure that returns (false)
    // [5] \MyCallableClass
    // [6] \Closure that returns \the stdClass instance
    // [7] \Closure that returns 5
    // [8] \Closure that returns 6.78
}

Float

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues('1', '2', '3');

foreach ($seq->cast('float') as $item) {
    // [0] 1.0
    // [1] 2.0
    // [2] 3.0
}

Integer

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues('1', '2', '3');

foreach ($seq->cast('int') as $item) {
    // [0] 1
    // [1] 2
    // [2] 3
}

Null / unset

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues('1', false, 2, new stdClass(), 3.0);

foreach ($seq->cast('unset') as $item) {
    // all are (null)
}

Object

use \System\Linq\Enumerable;

$item1 = ['a' => 1];
$item2 = ['b' => 2];
$item3 = ['c' => 3];

$seq = Enumerable::fromValues($item1, $item2, $item3);

foreach ($seq->cast('object') as $item) {
    // [0] stdClass ($item->a = 1)
    // [1] stdClass ($item->b = 2)
    // [2] stdClass ($item->c = 3)
}

String

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(1, 2, 3);

foreach ($seq->cast('string') as $item) {
    // [0] '1'
    // [1] '2'
    // [2] '3'
}
Clone this wiki locally