Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Письменов Дмитрий Иванович
/
yourroomads
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 823bc499
authored
May 21, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#19463 Реализация методов обработки полей РК с заменой переменных
1 parent
39a6af69
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
40 deletions
app/Models/Variable.php
app/Service/Requests/Direct/AddCampaigns.php
app/Service/StrReplaceByVariables.php
tests/Unit/ReplaceByVariablesTest.php
app/Models/Variable.php
View file @
823bc49
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
App\Models
;
namespace
App\Models
;
use
App\Models\Pivots\DictionaryCampaign
;
use
App\Models\Pivots\DictionaryCampaignVariable
;
use
App\Models\Pivots\DictionaryCampaignVariable
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Builder
;
...
@@ -35,6 +36,25 @@ class Variable extends Model
...
@@ -35,6 +36,25 @@ class Variable extends Model
'default_value'
,
'default_value'
,
];
];
public
static
function
getListVariablesByDictionaryCampaign
(
DictionaryCampaign
$dictionaryCampaign
)
{
$variable_list
=
[];
foreach
(
Variable
::
all
()
as
$variable
)
{
$dictionaryCampaignVariable
=
$variable
->
findValue
(
$dictionaryCampaign
->
getKey
());
$value
=
(
$dictionaryCampaignVariable
?
$dictionaryCampaignVariable
->
value
:
$variable
->
default_value
);
$variable_list
[
$variable
->
name
]
=
$value
;
}
return
$variable_list
;
}
public
function
scopeDefaultOrderBy
(
Builder
$query
)
public
function
scopeDefaultOrderBy
(
Builder
$query
)
{
{
return
$query
->
orderBy
(
'name'
);
return
$query
->
orderBy
(
'name'
);
...
...
app/Service/Requests/Direct/AddCampaigns.php
View file @
823bc49
...
@@ -65,22 +65,8 @@ class AddCampaigns extends DirectRequest
...
@@ -65,22 +65,8 @@ class AddCampaigns extends DirectRequest
'Campaigns'
=>
$this
->
dictionaryCampaigns
->
map
(
function
(
$dictionaryCampaign
)
{
'Campaigns'
=>
$this
->
dictionaryCampaigns
->
map
(
function
(
$dictionaryCampaign
)
{
/* @var DictionaryCampaign $dictionaryCampaign */
/* @var DictionaryCampaign $dictionaryCampaign */
$variable_list
=
[];
foreach
(
Variable
::
all
()
as
$variable
)
{
$dictionaryCampaignVariable
=
$variable
->
findValue
(
$dictionaryCampaign
->
getKey
());
$value
=
(
$dictionaryCampaignVariable
?
$dictionaryCampaignVariable
->
value
:
$variable
->
default_value
);
$variable_list
[
$variable
->
name
]
=
$value
;
}
$data
=
[
$data
=
[
'Name'
=>
StrReplaceByVariables
::
getInstance
(
$dictionaryCampaign
->
name
,
$
variable_list
)
->
get
(),
'Name'
=>
StrReplaceByVariables
::
getInstance
(
$dictionaryCampaign
->
name
,
$
dictionaryCampaign
)
->
get
(),
'StartDate'
=>
Carbon
::
now
()
->
format
(
'Y-m-d'
),
'StartDate'
=>
Carbon
::
now
()
->
format
(
'Y-m-d'
),
'TextCampaign'
=>
[
'TextCampaign'
=>
[
'BiddingStrategy'
=>
[
'BiddingStrategy'
=>
[
...
...
app/Service/StrReplaceByVariables.php
View file @
823bc49
...
@@ -2,31 +2,38 @@
...
@@ -2,31 +2,38 @@
namespace
App\Service
;
namespace
App\Service
;
use
App\Models\Pivots\DictionaryCampaign
;
use
App\Models\Variable
;
class
StrReplaceByVariables
class
StrReplaceByVariables
{
{
private
$delimer
=
'%'
;
private
$delimer
=
'%'
;
private
$str
;
private
$str
;
private
$
variables
;
private
$
list
;
/**
/**
* @param string $str
* @param string $str
* @param array
$variables
* @param array
|DictionaryCampaign $list
*/
*/
public
function
__construct
(
$str
,
$
variables
)
public
function
__construct
(
$str
,
$
list
)
{
{
foreach
(
$variables
as
$key
=>
$variable
)
{
if
(
$list
instanceof
DictionaryCampaign
)
{
$variables
[
"
{
$this
->
delimer
}{
$key
}{
$this
->
delimer
}
"
]
=
$variable
;
$dictionaryCampaign
=
$list
;
unset
(
$variables
[
$key
]);
$list
=
Variable
::
getListVariablesByDictionaryCampaign
(
$dictionaryCampaign
);
}
foreach
(
$list
as
$key
=>
$variable
)
{
$list
[
"
{
$this
->
delimer
}{
$key
}{
$this
->
delimer
}
"
]
=
$variable
;
unset
(
$list
[
$key
]);
}
}
$this
->
str
=
$str
;
$this
->
str
=
$str
;
$this
->
variables
=
$variables
;
$this
->
list
=
$list
;
}
}
static
public
function
getInstance
(
$str
,
$
variables
)
static
public
function
getInstance
(
$str
,
$
list
)
{
{
return
new
static
(
$str
,
$
variables
);
return
new
static
(
$str
,
$
list
);
}
}
/**
/**
...
@@ -34,7 +41,7 @@ class StrReplaceByVariables
...
@@ -34,7 +41,7 @@ class StrReplaceByVariables
*/
*/
public
function
get
()
public
function
get
()
{
{
return
strtr
(
$this
->
str
,
$this
->
variables
);
return
strtr
(
$this
->
str
,
$this
->
list
);
}
}
}
}
tests/Unit/ReplaceByVariablesTest.php
View file @
823bc49
...
@@ -91,22 +91,10 @@ class ReplaceByVariablesTest extends TestCase
...
@@ -91,22 +91,10 @@ class ReplaceByVariablesTest extends TestCase
$this
->
assertEquals
(
2
,
Variable
::
count
());
$this
->
assertEquals
(
2
,
Variable
::
count
());
$this
->
assertEquals
(
1
,
$this
->
campaign
->
dictionaryCampaignVariables
->
count
());
$this
->
assertEquals
(
1
,
$this
->
campaign
->
dictionaryCampaignVariables
->
count
());
$
variable_list
=
[]
;
$
list
=
Variable
::
getListVariablesByDictionaryCampaign
(
$this
->
dictionaryCampaign
)
;
foreach
(
Variable
::
all
()
as
$variable
)
{
$this
->
assertEquals
(
'String "custom var value"'
,
StrReplaceByVariables
::
getInstance
(
'String "%var%"'
,
$list
)
->
get
());
$dictionaryCampaignVariable
=
$variable
->
findValue
(
$this
->
dictionaryCampaign
->
getKey
());
$this
->
assertEquals
(
'String "vartwo value"'
,
StrReplaceByVariables
::
getInstance
(
'String "%vartwo%"'
,
$list
)
->
get
());
$value
=
(
$dictionaryCampaignVariable
?
$dictionaryCampaignVariable
->
value
:
$variable
->
default_value
);
$variable_list
[
$variable
->
name
]
=
$value
;
}
$this
->
assertEquals
(
'String "custom var value"'
,
StrReplaceByVariables
::
getInstance
(
'String "%var%"'
,
$variable_list
)
->
get
());
$this
->
assertEquals
(
'String "vartwo value"'
,
StrReplaceByVariables
::
getInstance
(
'String "%vartwo%"'
,
$variable_list
)
->
get
());
}
}
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment