/**
  * Constroleerd op verschillende opties.
  *
  * @param array  $data   Dit array wordt gevult met de verschillen
  * @param string $module Naam van de betreffende module
  * @param string $file   Type bestand (constants.ini / database.ini)
  * @param array  $source Array met opties van de bron environment
  * @param array  $target Array met opties van de doel environment
  */
 public function formatted_diff($source, $target)
 {
     $data = [];
     $missing_in_source = array_diff_key($target, $source);
     if (count($missing_in_source) > 0) {
         foreach ($missing_in_source as $key => $null) {
             $data[] = array('setting' => $key, 'source' => '<span style="color:red">Missing</span>', 'target' => htmlentities($target[$key]));
         }
     }
     foreach ($source as $key => $value) {
         if (!isset($target[$key])) {
             $data[] = array('setting' => $key, 'source' => htmlentities($value), 'target' => '<span style="color:red">Missing</span>');
         } elseif (!\Sledgehammer\equals($source[$key], $target[$key])) {
             $data[] = array('setting' => $key, 'source' => htmlentities($value), 'target' => htmlentities($target[$key]));
         }
     }
     return $data;
 }
Beispiel #2
0
 protected function renderElement()
 {
     $type = strtolower($this->getAttribute('type'));
     if (in_array($type, ['select', 'textarea'])) {
         $this->tag = $type;
         unset($this->attributes['type']);
     }
     $attributes = $this->attributes;
     switch ($this->tag) {
         case 'select':
             $options = $attributes['options'];
             unset($attributes['options'], $attributes['value']);
             echo Html::element($this->tag, $attributes, true);
             $selected = $this->getAttribute('value');
             $isIndexed = \Sledgehammer\is_indexed($options);
             foreach ($options as $value => $label) {
                 $option = array();
                 if ($isIndexed) {
                     $value = $label;
                 } else {
                     $option['value'] = $value;
                 }
                 if (\Sledgehammer\equals($value, $selected)) {
                     $option['selected'] = 'selected';
                 }
                 echo Html::element('option', $option, Html::escape($label));
             }
             echo '</select>';
             break;
         case 'textarea':
             unset($attributes['value']);
             echo Html::element($this->tag, $attributes, Html::escape($this->getAttribute('value')));
             break;
         default:
             echo Html::element($this->tag, $attributes);
             break;
     }
 }
Beispiel #3
0
 /**
  * Build a closure which validates an item with the gives $conditions.
  *
  * @param mixed $conditions array|Closure|expression  See Collection::where() for condition options
  *
  * @return callable
  */
 protected function buildFilter($conditions)
 {
     if (\Sledgehammer\is_closure($conditions)) {
         return $conditions;
     }
     if (is_array($conditions)) {
         // Create filter that checks all conditions
         $logicalOperator = \Sledgehammer\extract_logical_operator($conditions);
         if ($logicalOperator === false) {
             if (count($conditions) > 1) {
                 \Sledgehammer\notice('Conditions with multiple conditions require a logical operator.', "Example: array('AND', 'x' => 1, 'y' => 5)");
             }
             $logicalOperator = 'AND';
         } else {
             unset($conditions[0]);
         }
         $operators = [];
         foreach ($conditions as $path => $expectation) {
             if (preg_match('/^(.*) (' . \Sledgehammer\COMPARE_OPERATORS . ')$/', $path, $matches)) {
                 unset($conditions[$path]);
                 $conditions[$matches[1]] = $expectation;
                 $operators[$matches[1]] = $matches[2];
             } else {
                 $operators[$path] = false;
             }
         }
         // @todo Build an optimized closure for when a single conditions is given.
         if ($logicalOperator === 'AND') {
             return function ($item) use($conditions, $operators) {
                 foreach ($conditions as $path => $expectation) {
                     $actual = PropertyPath::get($path, $item);
                     $operator = $operators[$path];
                     if ($operator) {
                         if (\Sledgehammer\compare($actual, $operator, $expectation) === false) {
                             return false;
                         }
                     } elseif (\Sledgehammer\equals($actual, $expectation) === false) {
                         return false;
                     }
                 }
                 return true;
                 // All conditions are met.
             };
         } elseif ($logicalOperator === 'OR') {
             return function ($item) use($conditions, $operators) {
                 foreach ($conditions as $path => $expectation) {
                     $actual = PropertyPath::get($path, $item);
                     $operator = $operators[$path];
                     if ($operator) {
                         if (\Sledgehammer\compare($actual, $operator, $expectation) !== false) {
                             return true;
                         }
                     } elseif (\Sledgehammer\equals($actual, $expectation) !== false) {
                         return true;
                     }
                 }
                 return false;
                 // None of conditions are met.
             };
         } else {
             throw new Exception('Unsupported logical operator "' . $logicalOperator . '", expecting "AND" or "OR"');
         }
     }
     //'<= 5' or '10'
     // Compare the item directly with value given as $condition.
     if (is_string($conditions) && preg_match('/^(' . \Sledgehammer\COMPARE_OPERATORS . ') (.*)$/', $conditions, $matches)) {
         $operator = $matches[1];
         $expectation = $matches[2];
     } else {
         $expectation = $conditions;
         $operator = '==';
     }
     return function ($value) use($expectation, $operator) {
         return \Sledgehammer\compare($value, $operator, $expectation);
     };
 }