<?php
abstract class DefaultTransformer 
{
    public function transform($rules) 
    {
        return array_map(array(&$this, 'mapTransform'), $rules);
    }
    
    protected function mapTransform($rule)
    {
        return $this->transformRule($rule);
    }
    
    public function  transformRule($rule)
    {
        $newRule = new stdClass();
        $newRule->selectors = $this->transformSelectors($rule->selectors);
        $newRule->properties = $this->transformProperties($rule->properties);
        $newRule->parent = $rule->parent;
        return $newRule;
    }
    
    protected function transformSelectors($selectors)        
    {
        return $selectors;
    }
    
    protected function transformProperties($properties)        
    {
        return $properties;
    }
}

class PrintCssTransformer  extends DefaultTransformer
{
      public function  transformSelectors($selectors)
      {
          return array_filter(array_map(
              array(&$this, 'mapTransformSelectors'), $selectors), 
                  array(&$this, 'filterTransformSelectors'));
      }
      
      public function mapTransformSelectors($value)
      {
          $selectorsValidChrs = '-?[_a-zA-Z]+[_a-zA-Z0-9-]*';
          $collapsed = '/\.' . $selectorsValidChrs . 'postcontent' . $selectorsValidChrs . '/';
          return trim(preg_replace($collapsed, '', $value));              
      }
      
      public function filterTransformSelectors($value)
      {
          $excludedSelectors = array('slider', 'arrow',
                'loading', 'close', 'cw', 'ccw', 'preview-cms-logo',
                'lightbox', 'reset');
          $re = '/(' . implode('|', $excludedSelectors) . ')/';
          
          if (false !== strpos($value, 'body')) {
              $r = '';
          } 
          
          return '' !== $value && !preg_match($re, $value);
      }
      
      public function transformProperties($properties) 
      {
            $result = array();
            foreach($properties as $property => $data){
                if (!preg_match('/(border-|color|background-)/', $property)){
                    $result[$property] = $data;
                }
            }
            return $result;
      }
}

class EditorCssTransformer  extends DefaultTransformer
{
    public function  transformSelectors($selectors)
    {
          
          return array_filter(array_map(
              array(&$this, 'mapTransformSelectors'), $selectors), 
                  array(&$this, 'filterTransformSelectors'));
    }
    
    public function mapTransformSelectors($value)
    {
        $selectorsValidChrs = '-?[_a-zA-Z]+[_a-zA-Z0-9-]*';
        $collapsed = '/(\.' . $selectorsValidChrs . 'postcontent' . $selectorsValidChrs . ')/';
        $value = preg_replace('/^\.' . $selectorsValidChrs . 'postcontent' . $selectorsValidChrs . ' (\w*)/', 'body $1', $value);
        $value = trim(preg_replace($collapsed, '', $value));
        return '' !== $value ? $value : 'body';              
    }

    public function filterTransformSelectors($value)
    {
      $excludedSelectors = array('slider', 'arrow',
                'loading', 'close', 'cw', 'ccw', 'preview-cms-logo',
                'lightbox', 'reset', '\.cleared');
      $re = '/(' . implode('|', $excludedSelectors) . ')/';
      
      return !preg_match($re, $value);
    }
  
    public function transformRule($rule)
    {
         $selectors = $this->transformSelectors($rule->selectors);
         $newRule = new stdClass();
         $newRule->selectors = $selectors;
         $newRule->properties = $this->transformProperties($rule->properties);
         $newRule->parent = $rule->parent;
         if (false !== in_array('body', $selectors)){
                $properties = array();
                foreach($rule->properties as $property => $data){
                     switch ($property) {
                        case 'overflow':
                        case 'position':
                        case 'width':
                        case 'min-width':
                            break;
                        default:
                            $properties[$property] = $data;
                            break;
                    }
                }
                $newRule->properties = $properties; 
         }
         return $newRule;           
    }

}