PHP Classes

File: classes/Parser/Parser.php

Recommend this page to a friend!
  Classes of Bogdan   Simple Tags   classes/Parser/Parser.php   Download  
File: classes/Parser/Parser.php
Role: Class source
Content type: text/plain
Description: General parser class
Class: Simple Tags
Template engine that replaces tags within brackets
Author: By
Last change:
Date: 15 years ago
Size: 2,714 bytes
 

Contents

Class file image Download
<?php
/**
 * SimpleTags
 *
 * This class implements a simple and transparent replacement tags in the text.
 * Template: [tag].
 *
 * @package SimpleTags
 * @author Bogdan Thehansky <kirweb@gmail.com>
 */
require_once 'classes/Parser/IParser.php';

if (!
class_exists('Parser', FALSE)) {

/*
 * Find and replacement tags in the data
 *
 */

class Parser implements IParser {
   
   
/**
     * @var string
     */
   
protected $_parsingData = NULL;
   
   
/**
     * @var array
     */
   
private $_convertedTags;
   
    public function
__construct() {
       
    }
   
   
/**
     * Sets the array of data
     *
     * @param string $data
     * @return array $this->_parsingData
     */
   
public function setData($data) {
        if (
$this->processingErrors($data) ) {
            return
$this->_parsingData = $data;
        }
    }
   
   
/**
     * Return search result
     *
     * @return array foundTags
     */
   
public function getFoundTags() {
        return
$this->search();
    }
   
   
/**
     * Return array of the values after tags was convert
     *
     * @return array convertedTags
     */
   
public function getConvertedTags() {
        return
$this->tagsConverter();
    }
   
   
/**
     * Processing errors in the data
     *
     * @param string data
     * @throws Exception $e
     * @return boolean
     */
   
protected function processingErrors($data) {
            if ( empty(
$data) ) {
                throw new
Exception('Parser::$data must be not empty');
            }
            else if (!
is_string($data) ) {
                throw new
Exception('Parser::$data must be a string format');
            }
           
        return
true;
    }
   
   
/**
     * Finds tags in a row on the template
     *
     * @return array $matches[0]
     */
   
private function search() {
       
$tagsPatern = "/(\[([a-zA-Z0-9])+\])/i";
       
        try {
           
preg_match_all($tagsPatern, $this->_parsingData, $matches);
        } catch(
Exception $e) {
            return
$this->_parsingData;
        }
       
        return
$matches[0];
    }
   
   
/**
     * Converting value from the tag
     *
     * @return array $this->_convertedTags
     */
   
private function tagsConverter() {
       
$this->_convertedTags = array();
       
$foundResult = $this->getFoundTags();
       
        foreach(
$foundResult as $tags) {
           
$tags = str_replace('[', '', $tags);
           
$tags = str_replace(']', '', $tags);
           
$this->_convertedTags[] = $tags;
        }
       
        return
$this->_convertedTags;
    }
   
   
/**
     * Return modify data after parsing tags
     *
     * @return string $this->_parsingData
     */
   
public function parseResult() {
       
$key = $this->getConvertedTags();
       
$list = $this->parseTagsList();
       
        foreach (
$key as $tag){
            if(
array_key_exists($tag, $list)) {
               
$this->_parsingData = str_replace('[' . $tag . ']', $list[$tag], $this->_parsingData);
            }
        }
       
        return
$this->_parsingData;
    }

    protected function
parseTagsList() {
        require_once
'classes/Parser/TagsConfig.php';
        return
TagsConfig::$tagsValue;
    }
}

}
?>