/** * use an input item's data type to work out which method we should * call * * @param mixed $item * the item we want to dispatch * @param array $dispatchTable * the list of methods that are available * @param string $fallback * the value to return if there's no suitable entry for $item * in $dispatchTable * @return string * the name of the method to call */ public static function using($item, array $dispatchTable, $fallback = TypeMapper::FALLBACK_RESULT) { $itemTypes = GetDuckTypes::from($item); // do any of these types appear in our dispatch table? foreach ($itemTypes as $itemType) { if (isset($dispatchTable[$itemType])) { return $dispatchTable[$itemType]; } } // if we get here, then there's no entry in the dispatch table // for $item return $fallback; }
/** * return a practical list of data types for any value or variable * * @param mixed $item * the item to examine * @return string[] * the list of type(s) that this item can be */ function get_duck_types($item) { return \GanbaroDigital\MissingBits\TypeInspectors\GetDuckTypes::from($item); }
/** * @covers ::fromString */ public function testDetectsInterfaces() { // ---------------------------------------------------------------- // setup your test $data = 'Traversable'; $expectedResult = ['Traversable' => 'Traversable', 'interface' => 'interface', 'string' => 'string']; // ---------------------------------------------------------------- // perform the change $actualResult = GetDuckTypes::from($data); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }