root/components/library/CorujaStringManipulation.class.php

8691
6
6
7
/**
7
/**
8
 * Class for string manipulations
8
 * Class for string manipulations
9
 * @author Thiago Henrique Ramos da Mata <thiago.henrique.mata@gmail.com>
9
 */
10
 */
10
class CorujaStringManipulation
11
class CorujaStringManipulation
11
{
12
{
...
...
153
		$strText = $strTab . implode( $strTab , $arrText ) . $strTab;
154
		$strText = $strTab . implode( $strTab , $arrText ) . $strTab;
154
		return $strText;
155
		return $strText;
155
	}
156
	}
156
	
157
    /**
158
     * Returns a path to a folder relative from another folder. Both parameters
159
     * must be absolute.
160
     *
161
     * - check for valid parameters
162
     * - in case paths are equal return './'
163
     * - explode parameters using '/'
164
     * - remove similar base folders
165
     * - make final address
166
     *
167
     * @param String $strFileFrom Base from the path. This must be an absolute path.
168
     * @param String $strFileTo Destination of the path. This must be an absolute path.
169
     * @param Boolean $booValidPath Use false if you don't want to check for valid folders.
170
     * @throws InvalidArgumentException In case of invalid values
171
     *
172
     * @example $path = CorujaStringManipulation::getRelativePath( "/www/folder/", "/www/another/big/" ); // "../another/big/"
173
     *
174
     * @assert ( "/www/folder/", "/www/another/big/", false ) == "../another/big/"
175
     * @assert ( "", "" ) throws InvalidArgumentException
176
     * @assert ( "hello", "" ) throws InvalidArgumentException
177
     * @assert ( "", "hello" ) throws InvalidArgumentException
178
     * @assert ( "cool", "hello" ) throws InvalidArgumentException
179
     * @assert ( "/cool/", "hello" ) throws InvalidArgumentException
180
     * @assert ( "cool", "/hello/" ) throws InvalidArgumentException
181
     * @assert ( "/cool/", "/hello/", false ) == "../hello/"
182
     * @assert ( "/cool/", "/hello/", false ) == "../hello/"
183
     * @assert ( "/cool/", "/cool/", false ) == "./"
184
     * @assert ( "/cool/more/", "/other/", false ) == "../../other/"
185
     * @assert ( "/cool/", "/other/more/", false ) == "../other/more/"
186
     */
187
    public static function getRelativePath( $strFileFrom, $strFileTo, $booValidPath = true )
188
    {
189
        // check for valid parameters
190
191
        $strFileFrom = str_replace( "\\" , "/" , $strFileFrom );
192
        $strFileTo = str_replace( "\\" , "/" , $strFileTo );
193
194
        if( $booValidPath
195
            && ( ! is_dir( $strFileFrom ) || ! is_dir( $strFileTo ) )
196
        )
197
        {
198
            throw new InvalidArgumentException("Invalid parameter: strFileFrom: ".$strFileFrom." strFileTo: ".$strFileTo);
199
        }
200
201
        // special case: equal paths
202
        if( $strFileFrom == $strFileTo )
203
        {
204
             $strReturnPath = './';
205
        }
206
        else
207
        {
208
            // explode parameters using '/'
209
            $arrFileFrom = explode( '/', $strFileFrom );
210
            $arrFileTo   = explode( '/', $strFileTo );
211
212
            // remove similar base folders
213
            while(
214
                current( $arrFileFrom ) == current( $arrFileTo )
215
                && count( $arrFileFrom ) > 0
216
            )
217
            {
218
                array_shift( $arrFileFrom );
219
                array_shift( $arrFileTo );
220
            }
221
222
            $arrReturnPath = array();
223
224
            // make final address
225
            foreach( $arrFileFrom as $strFolder )
226
            {
227
                if( $strFolder != "" ) {
228
                    $arrReturnPath[] = "..";
229
                }
230
            }
231
232
            foreach( $arrFileTo as $strFolder )
233
            {
234
                $arrReturnPath[] = $strFolder;
235
            }
236
237
            $strReturnPath = implode( '/', $arrReturnPath );
238
239
        }
240
241
        return $strReturnPath;
242
    }
243
244
}
157
}
245
158
?>
246
?>