Functional\Exceptions\InvalidArgumentException::assertPropertyName PHP Метод

assertPropertyName() публичный статический Метод

public static assertPropertyName ( string $propertyName, string $callee, integer $parameterPosition )
$propertyName string
$callee string
$parameterPosition integer
    public static function assertPropertyName($propertyName, $callee, $parameterPosition)
    {
        if (!is_string($propertyName) && !is_integer($propertyName) && !is_float($propertyName) && !is_null($propertyName)) {
            throw new static(sprintf('%s() expects parameter %d to be a valid property name or array index, %s given', $callee, $parameterPosition, gettype($propertyName)));
        }
    }

Usage Example

/**
 * Extract a property from a collection of objects.
 *
 * @param Traversable|array $collection
 * @param string $propertyName
 * @return array
 */
function pluck($collection, $propertyName)
{
    InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
    InvalidArgumentException::assertPropertyName($propertyName, __FUNCTION__, 2);
    $aggregation = array();
    foreach ($collection as $index => $element) {
        $value = null;
        if (is_object($element) && isset($element->{$propertyName})) {
            $value = $element->{$propertyName};
        } elseif ((is_array($element) || $element instanceof ArrayAccess) && isset($element[$propertyName])) {
            $value = $element[$propertyName];
        }
        $aggregation[$index] = $value;
    }
    return $aggregation;
}