Twitter\Intents\Tweet::fromArray PHP Method

fromArray() public static method

Construct a new Tweet intent object from an options array
Since: 1.0.0
public static fromArray ( array $values ) : __CLASS__
$values array options array { @type string option name @type string|int|bool option value }
return __CLASS__ object initialized based on passed array values
    public static function fromArray($values)
    {
        if (!is_array($values)) {
            $values = array();
        }
        $class = get_called_class();
        $intent = new $class();
        unset($class);
        if (isset($values['validate'])) {
            if (false == $values['validate'] || 'false' === $values['validate'] || 0 == $values['validate']) {
                $intent->disableValidation();
            }
        }
        // remove values which evaluate to false
        $values = array_filter($values);
        // intent parameters
        if (isset($values['in_reply_to'])) {
            $intent->setInReplyTo($values['in_reply_to']);
        }
        if (isset($values['text'])) {
            $intent->setText($values['text']);
        }
        if (isset($values['url'])) {
            $intent->setURL($values['url']);
        }
        if (isset($values['hashtags'])) {
            $hashtags = array();
            if (is_array($values['hashtags'])) {
                $hashtags = $values['hashtags'];
            } else {
                $hashtags = explode(',', $values['hashtags']);
            }
            if (!empty($hashtags)) {
                array_walk($hashtags, array($intent, 'addHashtag'));
            }
            unset($hashtags);
        }
        if (isset($values['via'])) {
            $intent->setVia($values['via']);
        }
        if (isset($values['related'])) {
            $related = array();
            if (is_array($values['related'])) {
                $related = $values['related'];
            } elseif (is_string($values['related'])) {
                $related_accounts = explode(',', $values['related']);
                foreach ($related_accounts as $related_account) {
                    // extract the label
                    $account_pieces = explode(':', $related_account, 2);
                    $related[$account_pieces[0]] = isset($account_pieces[1]) ? rawurldecode($account_pieces[1]) : '';
                    unset($account_pieces);
                }
            }
            if (!empty($related)) {
                foreach ($related as $username => $label) {
                    if (!(is_string($username) && $username)) {
                        continue;
                    }
                    $intent->addRelated($username, $label);
                }
            }
            unset($related);
        }
        return $intent;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Sanitize an expected comma-separated list of hashtags into an array
  *
  * @since 1.0.0
  *
  * @param string $hashtag_string comma-separated list of hashtags
  *
  * @return array list of hashtags
  */
 public static function sanitizeCommaSeparatedHashtags($hashtag_string)
 {
     if (!is_string($hashtag_string)) {
         return array();
     }
     $hashtag_string = trim($hashtag_string);
     if (!$hashtag_string) {
         return array();
     }
     $intent = \Twitter\Intents\Tweet::fromArray(array('hashtags' => $hashtag_string));
     if (!$intent) {
         return array();
     }
     return $intent->getHashtags();
 }
All Usage Examples Of Twitter\Intents\Tweet::fromArray