Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / Helper.php
1 <?php
2
3 /**
4 * Instagram PHP
5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
7 */
8
9 namespace Instagram;
10
11 /**
12  * Helper class
13  *
14  * Example:
15  *
16  * $tags_closure = function($m){
17  *     return sprintf( '<a href="?example=tag.php&tag=%s">%s</a>', $m[1], $m[0] );
18  * };
19  *
20  * $mentions_closure = function($m){
21  *    return sprintf( '<a href="?example=user.php&user=%s">%s</a>', $m[1], $m[0] );
22  * };
23  *
24  * echo \Instagram\Helper::parseTagsAndMentions( $media->getCaption(), $tags_closure, $mentions_closure )
25  */
26 class Helper {
27
28     /**
29      * Parse mentions in a string
30      *
31      * Finds mentions in a string (@mention) and applies a callback function each one
32      *
33      * @param string $text Text to parse
34      * @param \Closure $callback Function to apply to each mention
35      * @return string Returns the text after the callback have been applied to each mention
36      * @access public
37      */
38     public static function parseMentions( $text, \Closure $callback ) {
39         return preg_replace_callback( '~@(.+?)(?=\b)~', $callback, $text );
40     }
41
42     /**
43      * Parse tags in a string
44      *
45      * Finds tags in a string (#username) and applies a callback function each one
46      *
47      * @param string $text Text to parse
48      * @param \Closure $callback Function to apply to each tag
49      * @return string Returns the text after the callback have been applied to each tag
50      * @access public
51      */
52     public static function parseTags( $text, \Closure $callback ) {
53         return preg_replace_callback( '~#(.+?)(?=\b)~', $callback, $text );
54     }
55
56     /**
57      * Parse mentions and tags in a string
58      *
59      * Finds mentions and tags in a string (@mention, #tag) and applies a callback function each one
60      *
61      * @param string $text Text to parse
62      * @param \Closure $tags_callback Function to apply to each tag
63      * @param \Closure $mentions_callback Function to apply to each mention
64      * @return string Returns the text after the callbacks have been applied to tags and mentions
65      * @access public
66      */
67     public static function parseTagsAndMentions( $text, \Closure $tags_callback, \Closure $mentions_callback ) {
68         $text = self::parseTags( $text, $tags_callback );
69         $text = self::parseMentions( $text, $mentions_callback );
70         return $text;
71     }
72
73     /**
74      * Is the comment deletable
75      *
76      * Checks if a comment is deletable by checking if the current user posted the comment
77      * or if the comment was added to one of the current user's media
78      * 
79      * @param  \Instagram\Comment $comment The comment
80      * @param  \Instagram\Media $media The media the comment was added to
81      * @param  \Instagram\CurrentUser $current_user Current user
82      * @access public
83      */
84     public static function commentIsDeletable( \Instagram\Comment $comment, \Instagram\Media $media, \Instagram\CurrentUser $current_user ) {
85         return
86             $comment->getUser()->getId() == $current_user->getId() ||
87             $media->getUser()->getId() == $current_user->getId();
88     }
89
90 }