/** * Breakdown * * Breakdown the the foreach, set the parts. * * @return void */ public function breakdown() { // Full Expression $full = $this->getFullExpression($this->structure); // Get array expression if (preg_match('/^%\\w+/', $full, $matches)) { $this->parts['expression'] = SH::remove('%', $matches[0]); } else { return; } // Get value if (preg_match('/%\\w+$/', $full, $matches)) { $this->parts['value'] = SH::remove('%', $matches[0]); } else { return; } // Is there a key/value pair? if (preg_match('/\\s?\\=\\>\\s?/', $full)) { if (preg_match('/%\\w+\\s?\\=\\>\\s?/', $full, $matches)) { // Remove => $this->parts['key'] = SH::remove('/\\s?\\=\\>\\s?/', $matches[0]); } } else { $this->parts['key'] = null; } // Get Statement $this->parts['statement'] = SH::remove('/\\t*\\?foreach\\(.+\\):|\\t*\\?endforeach/', $this->structure); }
/** * Get File Name * * @return string */ public function getFileName($string) { return preg_match('/\\(.+\\)/', $string, $matches) ? SH::remove('/\\(|\\)/', $matches[0]) : ''; }
/** * Get Full Expression * * Returns a structure's expression excluding * the opening and closing parenthesis. * * Ex: people as person * * @param string $structure * @return string */ public function getFullExpression($structure) { if (preg_match('/\\(.+\\)/', $structure, $matches)) { return SH::remove('/^\\(|\\)$/', $matches[0]); } }
/** * Get Attributes * * Return attributes from a given line. * * @param string $line * @return array */ public function getAttributes($line) { $pattern = '/(".+?")\\s?(=>)\\s?(".+?")/'; $attributes = []; // Get unparsed attributes from line if (preg_match_all($pattern, $line, $matches)) { foreach ($matches[0] as $attribute) { // Get properties and their values if (preg_match_all('/".+?"/', $attribute, $values)) { $att = SH::remove('"', $values[0][0]); $val = SH::remove('"', $values[0][1]); $attributes[$att] = $val; } } return $attributes; } }
/** * Update Variable * * @param string $variable * @param string $value * @return void */ public function updateVariable($variable, $value) { if (preg_match('/\\+\\+|\\-\\-/', $variable)) { $shouldAdd = preg_match('/\\+\\+/', $variable); $variable = SH::remove('/\\+\\+|\\-\\-/', $variable); if ($shouldAdd) { $value = $this->data[$variable] + 1; } else { $value = $this->data[$variable] - 1; } } // Update $this->data[$variable] = $value; }
<?php use Aah\Helpers\StringHelper as SH; describe("String Helper", function () { describe("::remove()", function () { it("passes if string is removed", function () { $string = 'Let us remove the word test!'; $expected = 'Let us remove the word !'; $result = SH::remove('test', $string); expect($result)->toBe($expected); }); it("passes if regex is removed from string", function () { $string = 'Let us remove the word test and dog!'; $expected = 'Let us remove the word and !'; $result = SH::remove('/test|dog/', $string); expect($result)->toBe($expected); }); }); });
/** * Replace Variables * * Replace variables in the expression with * actual data. * * @param string $expression * @return string */ public function replaceVariables($expression) { preg_match('/%\\w+/', $expression, $matches); $originalVar = $matches[0]; $replacementVar = '"' . $this->data[SH::remove('%', $matches[0])] . '"'; return preg_replace("/{$originalVar}/", $replacementVar, $this->parts['expression']); }