<commit_before><?php
/**
 * The file for the Str class
 *
 * @author     <NAME> <<EMAIL>>
 * @copyright  2014 <NAME>
 * @license    MIT License <http://opensource.org/licenses/MIT>
 * @package    Jstewmc/PhpHelpers <https://github.com/jstewmc/php-helpers>
 */
 
namespace Jstewmc\PhpHelpers;
 
/**
 * The string (aka, "str") utility class
 *
 * @since  0.1.0
 */
class Str
{
	/**
	 * Returns true if $haystack ends with $needle (case-sensitive)
	 *
	 * For example:
	 *
	 *     Str::endsWith('foobar', 'bar');  // returns true
	 *     Str::endsWith('foobar', 'baz');  // returns false
	 *     Str::endsWith('foobar', 'BAR');  // returns false
	 *     Str::endsWith('foobar', '');     // returns false
	 *     Str::endsWith('', 'foobar');     // returns false
	 * 
	 * @since   0.1.0
	 * @param   string  $haystack  the string to search 
	 * @param   string  $needle    the substring to search for
	 * @return  bool               true if $haystack ends with $needle
	 * @throws  \BadMethodCallException    if $haystack or $needle is omitted
	 * @throws  \InvalidArgumentException  if $haystack is not a string
	 * @throws  \InvalidArgumentException  if $needle is not a string
	 * @see     \Jstewmc\PhpHelpers\Str::iEndsWith() (case-insensitive version)
	 * @see     <http://stackoverflow.com/questions/834303> (MrHus' answer)
	 */
	public static function endsWith($haystack, $needle) 
	{
		$endsWith = false;
		
		// if $haystack and $needle are passed
		if ($haystack !== null && $needle !== null) {
			// if $haystack is a string
			if (is_string($haystack)) {
				// if $needle is a string
				if (is_string($needle)) {
					// if $haystack is not an empty string
					if (strlen($haystack) > 0) {
						// if $needle is not an empty string
						if (strlen($needle) > 0) {
							$endsWith = substr($haystack, -strlen($needle)) === $needle;
						} else {
							$endsWith = false;
						}
					} else {
						$endsWith = false;
					}
				} else {
					throw new \InvalidArgumentException(
						__METHOD__." expects the second parameter, the needle, to be a string"
					);
				}
			} else {
				throw new \InvalidArgumentException(
					__METHOD__." expects the first parameter, the haystack, to be a string"
				);
			}
		} else {
			throw new \BadMethodCallException(
				__METHOD__." expects two string parameters"
			);
		}

		return $endsWith;
	}

	/**
	 * Returns true if $haystack ends with $needle (case-insensitive)
	 *
	 *     Str::endsWith('foobar', 'bar');  // returns true
	 *     Str::endsWith('foobar', 'baz');  // returns false
	 *     Str::endsWith('foobar', 'BAR');  // returns true
	 * 
	 * @since   0.1.0
	 * @param   string  $haystack  str  the string to search 
	 * @param   string  $needle    str  the substring to search for
	 * @return  bool
	 * @throws  \BadMethodCallException    if $haystack or $needle is omitted
	 * @throws  \InvalidArgumentException  if $haystack is not a string
	 * @throws  \InvalidArgumentException  if $needle is not a string
	 * @see     \Jstewmc\PhpHelpers\Str::iEndsWith() (case-sensitive version)
	 */
	public static function iEndsWith($haystack, $needle) 
	{
		return self::endsWith(strtolower($haystack), strtolower($needle));
	}
	
	/**
	 * Alias for the isBool() method
	 */
	 public static function is_bool($string)
	 {
	 	return self::isBool($string);
	 }

	/**
	 * Returns true if $string is a bool string
	 * 
	 * I'll return true if $string is a bool string like 'true', 'false', 'yes', 'no', 
	 * 'on' or 'off'. Keep in mind, I only handle strings. I will return false if you
	 * test an actual bool value (because it's not a string).
	 *
	 *     is_bool(true);        // returns true
	 *     Str::is_bool(true);   // returns false
	 *
	 *     is_bool('true');      // returns false
	 *     Str::isBool('true');  // returns true
	 *
	 *     is_bool('yes');       // returns false
	 *     Str::isBool('yes');   // returns true
	 * 
	 * @param   string  $string  the string to test
	 * @return  bool
	 */
	public static function isBool($string)
	{
		return is_string($string) 
			&& in_array(strtolower($string), array('true', 'false', 'yes', 'no', 'on', 'off'));
	}

	/**
	 * Returns true if $haystack starts with $needle (case-insensitive)
	 *
	 * For example:
	 *
	 *     Str::iStartsWith('foobar', 'bar');  // returns false
	 *     Str::iStartsWith('foobar', 'foo');  // returns true
	 *     Str::iStartsWith('foobar', 'FOO');  // returns true
	 *     Str::iStartsWith('', 'foobar');     // returns false
	 *     Str::iStartsWith('foobar', '');     // returns false
	 *
	 * @since   0.1.0
	 * @param   $haystack  str   the case-insensitive string to search
	 * @param   $needle    str   the case-insensitive substring to search for
	 * @return             bool  true if $haystack ends with $needle
	 * @throws  \BadMethodCallException    if $haystack or $needle is omitted
	 * @throws  \InvalidArgumentException  if $haystack is not a string
	 * @throws  \InvalidArgumentException  if $needle is not a string
	 * @see     <http://stackoverflow.com/questions/834303> (MrHus' answer)
	 * @see     \Jstewmc\PhpHelpers\Str::startsWith() (case-sensitive version)
	 */ 
	public static function iStartsWith($haystack, $needle) 
	{
		return self::startsWith(strtolower($haystack), strtolower($needle));
	}
	
	/**
	 * Returns a random string of $length that follows the charset $rules
	 *
	 * Oftetimes, standards (like PCI) require passwords with one upper-case letter, one 
	 * lower-case letter, one number, and one symbol. I can do that.
	 *
	 * For example:
	 *
	 *     $rules = ['upper' => 12];
	 *     $a = Str::password(12, $rules);
	 *
	 *     $rules = ['lower' => 6, 'upper' => 6];
	 *     $b = Str::password(12, $rules);
	 *
	 *     $rules = ['lower' => 4, 'upper' => 4, 'number' => 4];
	 *     $c = Str::password(12, $rules);
	 *
	 *     echo $a;  // example 'KNVHYUIDGVDS'
	 *     echo $b;  // example 'jNhGFkLekOfV'
	 *     echo $c;  // example 'la9Uh7BH4Bc3'
	 *
	 * @since   0.1.0
	 * @param   int    $length  the length of the password (optional; if omitted, 
	 *     defaults to 8)
	 * @param   int[]  $rules   an array of character counts indexed by charset name
	 *     (possible charset names are 'lower', 'upper', 'number', 'alpha', and 'symbol') 
	 *     (optional; if omitted, defaults to ['lower' => 1, 'upper' => 1, 'number' => 1, 
	 *     'symbol' => 1])<commit_msg>Update Str::iEndsWith() and Str::iStartsWith()<commit_after><?php
/**
 * The file for the Str class
 *
 * @author     <NAME> <<EMAIL>>
 * @copyright  2014 <NAME>
 * @license    MIT License <http://opensource.org/licenses/MIT>
 * @package    Jstewmc/PhpHelpers <https://github.com/jstewmc/php-helpers>
 */
 
namespace Jstewmc\PhpHelpers;
 
/**
 * The string (aka, "str") utility class
 *
 * @since  0.1.0
 */
class Str
{
	/**
	 * Returns true if $haystack ends with $needle (case-sensitive)
	 *
	 * For example:
	 *
	 *     Str::endsWith('foobar', 'bar');  // returns true
	 *     Str::endsWith('foobar', 'baz');  // returns false
	 *     Str::endsWith('foobar', 'BAR');  // returns false
	 *     Str::endsWith('foobar', '');     // returns false
	 *     Str::endsWith('', 'foobar');     // returns false
	 * 
	 * @since   0.1.0
	 * @param   string  $haystack  the string to search 
	 * @param   string  $needle    the substring to search for
	 * @return  bool               true if $haystack ends with $needle
	 * @throws  \BadMethodCallException    if $haystack or $needle is omitted
	 * @throws  \InvalidArgumentException  if $haystack is not a string
	 * @throws  \InvalidArgumentException  if $needle is not a string
	 * @see     \Jstewmc\PhpHelpers\Str::iEndsWith() (case-insensitive version)
	 * @see     <http://stackoverflow.com/questions/834303> (MrHus' answer)
	 */
	public static function endsWith($haystack, $needle) 
	{
		$endsWith = false;
		
		// if $haystack and $needle are passed
		if ($haystack !== null && $needle !== null) {
			// if $haystack is a string
			if (is_string($haystack)) {
				// if $needle is a string
				if (is_string($needle)) {
					// if $haystack is not an empty string
					if (strlen($haystack) > 0) {
						// if $needle is not an empty string
						if (strlen($needle) > 0) {
							$endsWith = substr($haystack, -strlen($needle)) === $needle;
						} else {
							$endsWith = false;
						}
					} else {
						$endsWith = false;
					}
				} else {
					throw new \InvalidArgumentException(
						__METHOD__." expects the second parameter, the needle, to be a string"
					);
				}
			} else {
				throw new \InvalidArgumentException(
					__METHOD__." expects the first parameter, the haystack, to be a string"
				);
			}
		} else {
			throw new \BadMethodCallException(
				__METHOD__." expects two string parameters"
			);
		}

		return $endsWith;
	}

	/**
	 * Returns true if $haystack ends with $needle (case-insensitive)
	 *
	 *     Str::endsWith('foobar', 'bar');  // returns true
	 *     Str::endsWith('foobar', 'baz');  // returns false
	 *     Str::endsWith('foobar', 'BAR');  // returns true
	 * 
	 * @since   0.1.0
	 * @param   string  $haystack  str  the string to search 
	 * @param   string  $needle    str  the substring to search for
	 * @return  bool
	 * @throws  \BadMethodCallException    if $haystack or $needle is omitted
	 * @throws  \InvalidArgumentException  if $haystack is not a string
	 * @throws  \InvalidArgumentException  if $needle is not a string
	 * @see     \Jstewmc\PhpHelpers\Str::iEndsWith() (case-sensitive version)
	 */
	public static function iEndsWith($haystack, $needle) 
	{
		$endsWith = null;
		
		// if $haystack and $needle are given
		if ($haystack !== null && $needle !== null) {
			// if $haystack is a string
			if (is_string($haystack)) {
				// if $needle is a string
				if (is_string($needle)) {
					$endsWith = self::endsWith(strtolower($haystack), strtolower($needle));		
				} else {
					throw new \InvalidArgumentException(
						__METHOD__."() expects parameter two, needle, to be a string"
					);
				}
			} else {
				throw new \InvalidArgumentException(
					__METHOD__."() expects parameter one, haystack, to be a string"
				);
			}
		} else {
			throw new \BadMethodCallException(
				__METHOD__."() expects two string parameters, haystack and needle"
			);
		}
		
		return $endsWith;
	}
	
	/**
	 * Alias for the isBool() method
	 */
	 public static function is_bool($string)
	 {
	 	return self::isBool($string);
	 }

	/**
	 * Returns true if $string is a bool string
	 * 
	 * I'll return true if $string is a bool string like 'true', 'false', 'yes', 'no', 
	 * 'on' or 'off'. Keep in mind, I only handle strings. I will return false if you
	 * test an actual bool value (because it's not a string).
	 *
	 *     is_bool(true);        // returns true
	 *     Str::is_bool(true);   // returns false
	 *
	 *     is_bool('true');      // returns false
	 *     Str::isBool('true');  // returns true
	 *
	 *     is_bool('yes');       // returns false
	 *     Str::isBool('yes');   // returns true
	 * 
	 * @param   string  $string  the string to test
	 * @return  bool
	 */
	public static function isBool($string)
	{
		return is_string($string) 
			&& in_array(strtolower($string), array('true', 'false', 'yes', 'no', 'on', 'off'));
	}

	/**
	 * Returns true if $haystack starts with $needle (case-insensitive)
	 *
	 * For example:
	 *
	 *     Str::iStartsWith('foobar', 'bar');  // returns false
	 *     Str::iStartsWith('foobar', 'foo');  // returns true
	 *     Str::iStartsWith('foobar', 'FOO');  // returns true
	 *     Str::iStartsWith('', 'foobar');     // returns false
	 *     Str::iStartsWith('foobar', '');     // returns false
	 *
	 * @since   0.1.0
	 * @param   $haystack  str   the case-insensitive string to search
	 * @param   $needle    str   the case-insensitive substring to search for
	 * @return             bool  true if $haystack ends with $needle
	 * @throws  \BadMethodCallException    if $haystack or $needle is omitted
	 * @throws  \InvalidArgumentException  if $haystack is not a string
	 * @throws  \InvalidArgumentException  if $needle is not a string
	 * @see     <http://stackoverflow.com/questions/834303> (MrHus' answer)
	 * @see     \Jstewmc\PhpHelpers\Str::startsWith() (case-sensitive version)
	 */ 
	public static function iStartsWith($haystack, $needle) 
	{
		$startsWith = null;
		
		// if $haystack and $needle are given
		if ($haystack !== null && $needle !== null) {
			// if $haystack is a string
			if (is_string($haystack)) {
				// if $needle is a string
				if (is_string($needle)) {
					$startsWith = self::startsWith(strtolower($haystack), strtolower($needle));	
				} else {
					throw new \InvalidArgumentException(
						__METHOD__."() expects parameter two, needle, to be a string"
					);
				}
			} else {
				throw new \InvalidArgumentException(
					__METHOD__."() expects parameter one, haystack, to be a string"
				);
			}
		} else {
			throw new \BadMethodCallException(
				__METHOD__."() expects two string parameters, haystack and needle"
			);
		}
		
		return $startsWith;
	}
	
	/**
	 * Returns a random string of $length that follows the charset $rules
	 *
	 * Oftetimes, standards (like PCI) require passwords with one upper-case letter, one 
	 * lower-case letter, one number, and one symbol. I can do that.
	 *
	 * For example:
	 *
	 *     $rules = ['upper' => 12];
	 *     $a = Str::password(12, $rules);
	 *
	 *     $rules = ['lower' => 6, 'upper' => 6];
	 *     $b = Str::password(12, $rules);
	 *
	 *     $rules = ['lower' => 4, 'upper' => 4, 'number' => 4];
	 *     $c = Str::password(12, $rules);
	 *
	 *     echo $a;  // example 'KNVHYUIDGVDS'
	 *     echo $b;  // example 'jNhGFkLekOfV'
	 *     echo $c;  // example 'la9Uh7BH4Bc3'
	 *
	 * @since   0.1.0
	 * @param   int    $length  the length of the password (optional; if omitted, 
	 *     defaults to 8)
	 * @param   int[]  $rules   an array of character counts indexed by charset name
	 *     (possible charset names are 'lower', 'upper', 'number', 'alpha', and 'symbol') 
	 *     (optional; if omitted, defaults to ['lower' => 1, 'upper' => 1, 'number' => 1, 
	 *     'symbol' => 1])
{'fraction_non_alphanumeric': 0.14324247586598524, 'fraction_numerical': 0.007808063600227144, 'mean_word_length': 2.69305373525557, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5642408', 'n_tokens_mistral': 5267, 'n_tokens_neox': 4472, 'n_words': 1366}