Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Accept arbitrary attributes on countrySelector #7

Open
manticorp opened this issue Dec 18, 2015 · 2 comments
Open

Suggestion: Accept arbitrary attributes on countrySelector #7

manticorp opened this issue Dec 18, 2015 · 2 comments

Comments

@manticorp
Copy link

An example call would be:

<?php
echo Iso3166\Codes::countrySelector('name', 'FR', array(
    'class' => 'classname',
    'id'    => 'idname',
    'role'  => 'select'
);`

or, in keeping with the previous classname implementation:

<?php
echo Iso3166\Codes::countrySelector('classname', 'name', 'FR', array(
    'id'    => 'idname',
    'role'  => 'select'
);`
@manticorp
Copy link
Author

Example implementation:

public static function countrySelector($class = '', $name = 'country', $selected = null, $attributes = null)
{
    $attstring = '';
    if($attributes !== null && is_array($attributes)){
        foreach($attributes as $name => $value) {
            $attstring .= sprintf(' %s="%s"',htmlentities($name),htmlentities($value));
        }
    }
    return sprintf('<select class="%s" name="%s"%s>', $class, $name, $attstring)
        . implode(array_map(function($country, $code) use($selected) {
            return ($code == $selected)
                ? sprintf('<option value="%s" selected>%s</option>', $code, $country)
                : sprintf('<option value="%s">%s</option>', $code, $country);
        }, static::$countries, array_keys(static::$countries)))
        . '</select>';
}

@manticorp
Copy link
Author

Or, even better:

public static function countrySelector($class = null, $name = 'country', $selected = null, $attributes = null)
{
    $attstring = '';
    if(!is_null($class) && is_string($class)) {
        if(is_array($attributes)){
            $attributes['class'] = $class;
        }
    }
    if($attributes !== null && is_array($attributes)){
        foreach($attributes as $name => $value) {
            $attstring .= sprintf(' %s="%s"',htmlentities($name),htmlentities($value));
        }
    }
    return sprintf('<select name="%s"%s>', $name, $attstring)
        . implode(array_map(function($country, $code) use($selected) {
            return ($code == $selected)
                ? sprintf('<option value="%s" selected>%s</option>', $code, $country)
                : sprintf('<option value="%s">%s</option>', $code, $country);
        }, static::$countries, array_keys(static::$countries)))
        . '</select>';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant