Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
airatvibe committed Jul 25, 2020
1 parent 462fb08 commit cc7ade6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 69 deletions.
4 changes: 2 additions & 2 deletions ad-generator-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ function ad_generator_cli( $max_res, $filename ) {
$ad_text = str_replace( "\\'", "'", $ad_text );

if ( $ad_text ) {
require_once 'includes/Natty/TextRandomizer.php';
$tRand = new Natty_TextRandomizer( $ad_text );
require_once 'includes/Randomizer.php';
$tRand = new Randomizer( $ad_text );
$num_var = $tRand->numVariant();

if ( $num_var > 1 ) {
Expand Down
4 changes: 2 additions & 2 deletions ad-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ static function ad_generator_func( $atts ) {
if ( $ad_text ) {
$result_text .= '<br /><a href=' . $_SERVER['REQUEST_URI'] . ' id="ad_text_clear_btn">' . __( 'Очистить и начать заново', self::$mydomain ) . '</a><br /><br />';

require_once plugin_dir_path( __FILE__ ) . '/includes/Natty/TextRandomizer.php';
require_once plugin_dir_path( __FILE__ ) . '/includes/Randomizer.php';

$tRand = new Natty_TextRandomizer( $ad_text );
$tRand = new Randomizer( $ad_text );
$num_var = $tRand->numVariant();

if ( $num_var > 1 ) {
Expand Down
30 changes: 3 additions & 27 deletions includes/Natty/TextRandomizer/Node.php → includes/Node.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
<?php
/**
* Project: Natty CMS: a PHP-based Content Management System
* File: Natty/TextRandomizer/Node.php
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @link http://xbb.uz/
* @author Dmitriy Skorobogatov <info at xbb dot uz>
* @version 0.21
* @copyright 2006-2009 Dmitriy Skorobogatov
* @package Natty
*/

class Natty_TextRandomizer_Node
class Node
{
private $_parent = null;

Expand All @@ -40,7 +16,7 @@ class Natty_TextRandomizer_Node

private $_isSeparator = false;

public function __construct(Natty_TextRandomizer_Node $parent = null)
public function __construct(Node $parent = null)
{
$this->_parent = $parent;
if ($parent) {
Expand Down Expand Up @@ -129,7 +105,7 @@ public function concat($str)
$this->_str .= $str;
return $this;
}
$currentNode = new Natty_TextRandomizer_Node($this);
$currentNode = new Node($this);
$currentNode->setType('string');
return $currentNode->concat($str);
}
Expand Down
52 changes: 14 additions & 38 deletions includes/Natty/TextRandomizer.php → includes/Randomizer.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
<?php
/**
* Project: Natty CMS: a PHP-based Content Management System
* File: Natty/TextRandomizer.php
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @link http://xbb.uz/
* @author Dmitriy Skorobogatov <info at xbb dot uz>
* @version 0.21
* @copyright 2006-2009 Dmitriy Skorobogatov
* @package Natty
*/

require_once dirname(__FILE__) . '/TextRandomizer/Node.php';
require_once dirname(__FILE__) . '/Node.php';

class Natty_TextRandomizer
class Randomizer
{
private $_text = '';

Expand All @@ -36,7 +12,7 @@ public function __construct($text = '')
{
$text = (string) $text;
$this->_text = $text;
$this->_tree = new Natty_TextRandomizer_Node;
$this->_tree = new Node;
$preg = '/
\\\\\\\ | # мнемонизированный слэш
\\\\\+ | # мнемонизированный +
Expand All @@ -56,7 +32,7 @@ public function __construct($text = '')
[^\\\+\{\}\[\]\|]+ # все прочее
/xu';
$currentNode = $this->_tree;
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
$currentNode->setType('series');
$currentNode = $currentNode->concat('');
while (preg_match($preg, $text, $match)) {
Expand Down Expand Up @@ -85,16 +61,16 @@ public function __construct($text = '')
break;
case '[+':
if ('string' == $currentNode->type) {
$currentNode = new Natty_TextRandomizer_Node($currentNode->parent);
$currentNode = new Node($currentNode->parent);
} else {
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
}
$currentNode->isSeparator = true;
break;
case '+':
if ($currentNode->isSeparator) {
$currentNode->isSeparator = false;
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
$currentNode->setType('series');
$currentNode = $currentNode->concat('');
} else {
Expand All @@ -103,12 +79,12 @@ public function __construct($text = '')
break;
case '{':
if ('string' == $currentNode->type) {
$currentNode = new Natty_TextRandomizer_Node($currentNode->parent);
$currentNode = new Node($currentNode->parent);
} else {
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
}
$currentNode->setType('synonyms');
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
$currentNode->setType('series');
$currentNode = $currentNode->concat('');
break;
Expand All @@ -123,11 +99,11 @@ public function __construct($text = '')
break;
case '[':
if ('string' == $currentNode->type) {
$currentNode = new Natty_TextRandomizer_Node($currentNode->parent);
$currentNode = new Node($currentNode->parent);
} else {
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
}
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
$currentNode->setType('series');
$currentNode = $currentNode->concat('');
break;
Expand All @@ -144,7 +120,7 @@ public function __construct($text = '')
$is = $currentNode->parent;
if ($is && 'series' == $is->type) {
$currentNode = $is->parent;
$currentNode = new Natty_TextRandomizer_Node($currentNode);
$currentNode = new Node($currentNode);
$currentNode->setType('series');
$currentNode = $currentNode->concat('');
} else {
Expand Down

0 comments on commit cc7ade6

Please sign in to comment.