<commit_before><?php
/**
 * Habari Stack Class
 *
 * This class allows Habari to accumulate a group of unique values that
 * can be output using a specific formatting string.
 * This is useful for collecting a set of unique javascript references to output
 * and then insert them at a specific point on the page.
 * 
 * <code>
 * // Add jquery to the javascript stack: 
 * Stack::add( 'template_header_javascript', Site::get_url('scripts') . '/jquery.js', 'jquery' );
 *
 * // Add stylesheet to theme_stylesheet stack with media type
 * Stack::add( 'template_stylesheet', array( Site::get_url('theme') . '/style.css', 'screen' ), 'style' );
 *  
 * // Output the javascript stack:
 * Stack::out( 'template_header_javascript', '<script src="%s" type="text/javascript"></script>' );
 *
 * // Output the theme_stylesheet stack:
 * Stack::out( 'template_stylesheet', '<link rel="stylesheet" type="text/css"  href="%s" media="%s">' );
 * </code>   
 *
 * @package Habari
 */

class Stack
{
	private static $stacks= array();
	
	/**
	 * Private constructor for Stack.
	 * Stack objects should only be created using the static 
	 * method Static::create_stack(), or one of the methods that
	 * adds a value directly to a stack.  This prevents multiple Stack 
	 * objects from being created with the same name.
	 * 
	 * @param mixed $input An array or ArrayObject to create the stack from.
	 * @return array The created stack
	 **/
	private function __construct( $input )
	{
		parent::__construct( $input );
	}

	/**
	 * Retreive a named stack instance
	 * @param string $stack_name The name of the stack to return
	 * @return Stack The requested stack
	 **/	  	 	 	
	public static function get_named_stack( $stack_name )
	{
		if ( isset( self::$stacks[$stack_name] ) ) {
			return self::$stacks[$stack_name];
		}
		else {
			return self::create_stack( $stack_name );
		}
	}
	
	/**
	 * Creates and retreives a named stack instance
	 * @param string $stack_name The name of the stack to create and return
	 * @return array The created stack
	 **/	  	 	 	
	public static function create_stack( $stack_name )
	{
		if ( empty( self::$stacks[$stack_name] ) ) {
			$stack= array();
			self::$stacks[$stack_name] = $stack;
		}
		return self::$stacks[$stack_name];
	}
	
	/**
	 * Add a value to a stack
	 * @param string $stack_name The name of the stack
	 * @param mixed $value The value to add
	 * @param string $value_name The name of the value to add
	 * @return array The stack that was added to	 
	 **/	 
	public static function add( $stack_name, $value, $value_name= null )
	{
		$stack= self::get_named_stack( $stack_name );
		$value_name= $value_name ? $value_name : md5( serialize( $value ) );
		$stack[$value_name]= $value;
		self::$stacks[$stack_name]= $stack;
		return $stack;
	}
	
	/**
	 * Remove a value to a stack
	 * @param string $stack_name The name of the stack
	 * @param string $value_name The name of the value to add
	 * @return array The stack that was added to	 
	 **/	 
	public static function remove( $stack_name, $value_name )
	{
		$stack= self::get_named_stack( $stack_name );
		if ( isset( $stack[$value_name] ) ) {
			unset( $stack[$value_name] );
		}
		self::$stacks[$stack_name]= $stack;
		return $stack;
	}
	
	/**
	 * Returns all of the values of the stack<commit_msg>Perverting the Stack class to be slightly less Stack-y by allowing individual items to be referenced. Also fixed some apparent copy-and-paste PHPDoc comments.<commit_after><?php
/**
 * Habari Stack Class
 *
 * This class allows Habari to accumulate a group of unique values that
 * can be output using a specific formatting string.
 * This is useful for collecting a set of unique javascript references to output
 * and then insert them at a specific point on the page.
 * 
 * <code>
 * // Add jquery to the javascript stack: 
 * Stack::add( 'template_header_javascript', Site::get_url('scripts') . '/jquery.js', 'jquery' );
 *
 * // Add stylesheet to theme_stylesheet stack with media type
 * Stack::add( 'template_stylesheet', array( Site::get_url('theme') . '/style.css', 'screen' ), 'style' );
 *  
 * // Output the javascript stack:
 * Stack::out( 'template_header_javascript', '<script src="%s" type="text/javascript"></script>' );
 *
 * // Output the theme_stylesheet stack:
 * Stack::out( 'template_stylesheet', '<link rel="stylesheet" type="text/css"  href="%s" media="%s">' );
 * </code>   
 *
 * @package Habari
 */

class Stack
{
	private static $stacks= array();
	
	/**
	 * Private constructor for Stack.
	 * Stack objects should only be created using the static 
	 * method Static::create_stack(), or one of the methods that
	 * adds a value directly to a stack.  This prevents multiple Stack 
	 * objects from being created with the same name.
	 * 
	 * @param mixed $input An array or ArrayObject to create the stack from.
	 * @return array The created stack
	 **/
	private function __construct( $input )
	{
		parent::__construct( $input );
	}

	/**
	 * Retreive a named stack instance
	 * @param string $stack_name The name of the stack to return
	 * @return Stack The requested stack
	 **/	  	 	 	
	public static function get_named_stack( $stack_name )
	{
		if ( isset( self::$stacks[$stack_name] ) ) {
			return self::$stacks[$stack_name];
		}
		else {
			return self::create_stack( $stack_name );
		}
	}
	
	/**
	 * Check for the existence of a given stack item.
	 *
	 * @param string $stack_name The name of the stack in which to check.
	 * @param string $value The value to check for.
	 * @return boolean TRUE if the item exists, FALSE otherwise.
	 */
	public static function has ( $stack_name, $value_name ) {
		
		// get the stack
		$stack = self::get_named_stack( $stack_name );
		
		if ( isset( $stack[ $value_name ] ) ) {
			return true;
		}
		else {
			return false;
		}
		
	}
	
	/**
	 * Get a single item from a given stack.
	 *
	 * @param string $stack_name The name of the stack to fetch an item from.
	 * @param string $value The item to fetch.
	 * @param mixed $default_value The default value to return if the item does not exist in the stack.
	 * @return mixed The item, or $default_value if it does not exist.
	 */
	public static function get_item ( $stack_name, $value_name, $default_value = null ) {
		
		// get the stack
		$stack = self::get_named_stack( $stack_name );
		
		if ( isset( $stack[ $value_name ] ) ) {
			return $stack[ $value_name ];
		}
		else {
			return $default_value;
		}
		
	}
	
	/**
	 * Creates and retreives a named stack instance
	 * @param string $stack_name The name of the stack to create and return
	 * @return array The created stack
	 **/	  	 	 	
	public static function create_stack( $stack_name )
	{
		if ( empty( self::$stacks[$stack_name] ) ) {
			$stack= array();
			self::$stacks[$stack_name] = $stack;
		}
		return self::$stacks[$stack_name];
	}
	
	/**
	 * Add a value to a stack
	 * @param string $stack_name The name of the stack
	 * @param mixed $value The value to add
	 * @param string $value_name The name of the value to add
	 * @return array The stack that was added to	 
	 **/	 
	public static function add( $stack_name, $value, $value_name= null )
	{
		$stack= self::get_named_stack( $stack_name );
		$value_name= $value_name ? $value_name : md5( serialize( $value ) );
		$stack[$value_name]= $value;
		self::$stacks[$stack_name]= $stack;
		return $stack;
	}
	
	/**
	 * Remove a value to a stack
	 * @param string $stack_name The name of the stack
	 * @param string $value_name The name of the value to remove
	 * @return array The rest of the stack, post-remove	 
	 **/	 
	public static function remove( $stack_name, $value_name )
	{
		$stack= self::get_named_stack( $stack_name );
		if ( isset( $stack[$value_name] ) ) {
			unset( $stack[$value_name] );
		}
		self::$stacks[$stack_name]= $stack;
		return $stack;
	}
	
	/**
	 * Returns all of the values of the stack
{'fraction_non_alphanumeric': 0.12152157263211641, 'fraction_numerical': 0.0002552974214960429, 'mean_word_length': 3.4977037887485647, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '2466790', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2381, 'n_words': 878}