Twitter\Widgets\Buttons\Follow::fromArray PHP Method

fromArray() public static method

Build a Follow button object from an associative array
Since: 1.0.0
public static fromArray ( array $options ) : __CLASS__
$options array associative array of options { @type string option name @type string|int|bool option value }
return __CLASS__ support chaining
    public static function fromArray($options)
    {
        if (!isset($options['screen_name']) && $options['screen_name']) {
            return;
        }
        $class = __CLASS__;
        $follow = new $class($options['screen_name']);
        unset($class);
        $follow->setBaseOptions($options);
        if (isset($options['show_count']) && (false === $options['show_count'] || 'false' === $options['show_count'] || 0 == $options['show_count'])) {
            $follow->hideCount();
        }
        if (isset($options['show_screen_name']) && (false === $options['show_screen_name'] || 'false' === $options['show_screen_name'] || 0 == $options['show_screen_name'])) {
            $follow->hideScreenName();
        }
        if (isset($options['size']) && 'medium' !== $options['size']) {
            $follow->setSize($options['size']);
        }
        return $follow;
    }

Usage Example

Example #1
0
 /**
  * Update a widget instance
  *
  * @since 1.0.0
  *
  * @param array $new_instance New settings for this instance as input by the user via form()
  * @param array $old_instance Old settings for this instance
  *
  * @return bool|array settings to save or false to cancel saving
  */
 public function update($new_instance, $old_instance)
 {
     $instance = array();
     $new_instance = (array) $new_instance;
     $title = trim(strip_tags($new_instance['title']));
     if ($title) {
         $instance['title'] = $title;
     }
     unset($new_instance['title']);
     foreach (array('show_screen_name', 'show_count') as $bool_option) {
         if (isset($new_instance[$bool_option]) && $new_instance[$bool_option]) {
             $new_instance[$bool_option] = true;
         } else {
             $new_instance[$bool_option] = false;
         }
     }
     $follow_button = \Twitter\Widgets\Buttons\Follow::fromArray($new_instance);
     if (!$follow_button) {
         return false;
     }
     $filtered_options = $follow_button->toArray(false);
     $screen_name = $follow_button->getScreenName();
     if ($screen_name) {
         $filtered_options['screen_name'] = $screen_name;
     }
     unset($screen_name);
     // convert string to bool equivalent
     if (isset($filtered_options['show-screen-name'])) {
         if ('false' == $filtered_options['show-screen-name']) {
             $filtered_options['show_screen_name'] = false;
         }
         unset($filtered_options['show-screen-name']);
     }
     if (isset($filtered_options['show-count'])) {
         if ('false' == $filtered_options['show-count']) {
             $filtered_options['show_count'] = false;
         }
         unset($filtered_options['show-count']);
     }
     return array_merge($instance, $filtered_options);
 }
All Usage Examples Of Twitter\Widgets\Buttons\Follow::fromArray