Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
ConditionalExpression example
class
DefaultFilter
extends
FilterExpression
{
public
function
__construct
(
Node
$node
, ConstantExpression
$filterName
, Node
$arguments
, int
$lineno
, string
$tag
= null
)
{
$default
=
new
FilterExpression
(
$node
,
new
ConstantExpression
(
'default',
$node
->
getTemplateLine
(
)
)
,
$arguments
,
$node
->
getTemplateLine
(
)
)
;
if
(
'default' ===
$filterName
->
getAttribute
(
'value'
)
&&
(
$node
instanceof NameExpression ||
$node
instanceof GetAttrExpression
)
)
{
$test
=
new
DefinedTest
(
clone
$node
, 'defined',
new
Node
(
)
,
$node
->
getTemplateLine
(
)
)
;
$false
= \
count
(
$arguments
)
?
$arguments
->
getNode
(
0
)
:
new
ConstantExpression
(
'',
$node
->
getTemplateLine
(
)
)
;
$node
=
new
ConditionalExpression
(
$test
,
$default
,
$false
,
$node
->
getTemplateLine
(
)
)
;
}
else
{
$node
=
$default
;
}
parent::
__construct
(
$node
,
$filterName
,
$arguments
,
$lineno
,
$tag
)
;
}
public
function
compile
(
Compiler
$compiler
)
: void
{
$compiler
->
subcompile
(
$this
->
getNode
(
'node'
)
)
;
}
}
$expr2
=
$this
->
parseExpression
(
)
;
if
(
$this
->parser->
getStream
(
)
->
nextIf
(
/* Token::PUNCTUATION_TYPE */ 9, ':'
)
)
{
$expr3
=
$this
->
parseExpression
(
)
;
}
else
{
$expr3
=
new
ConstantExpression
(
'',
$this
->parser->
getCurrentToken
(
)
->
getLine
(
)
)
;
}
}
else
{
$expr2
=
$expr
;
$expr3
=
$this
->
parseExpression
(
)
;
}
$expr
=
new
ConditionalExpression
(
$expr
,
$expr2
,
$expr3
,
$this
->parser->
getCurrentToken
(
)
->
getLine
(
)
)
;
}
return
$expr
;
}
private
function
isUnary
(
Token
$token
)
: bool
{
return
$token
->
test
(
/* Token::OPERATOR_TYPE */ 8
)
&&
isset
(
$this
->unaryOperators
[
$token
->
getValue
(
)
]
)
;
}
private
function
isBinary
(
Token
$token
)
: bool
{
$expr2
=
$this
->
unwrapConditional
(
$expr2
,
$env
,
$type
)
;
}
else
{
$expr2
=
$this
->
escapeInlinePrintNode
(
new
InlinePrint
(
$expr2
,
$expr2
->
getTemplateLine
(
)
)
,
$env
,
$type
)
;
}
$expr3
=
$expression
->
getNode
(
'expr3'
)
;
if
(
$expr3
instanceof ConditionalExpression &&
$this
->
shouldUnwrapConditional
(
$expr3
,
$env
,
$type
)
)
{
$expr3
=
$this
->
unwrapConditional
(
$expr3
,
$env
,
$type
)
;
}
else
{
$expr3
=
$this
->
escapeInlinePrintNode
(
new
InlinePrint
(
$expr3
,
$expr3
->
getTemplateLine
(
)
)
,
$env
,
$type
)
;
}
return
new
ConditionalExpression
(
$expression
->
getNode
(
'expr1'
)
,
$expr2
,
$expr3
,
$expression
->
getTemplateLine
(
)
)
;
}
private
function
escapeInlinePrintNode
(
InlinePrint
$node
, Environment
$env
, string
$type
)
: Node
{
$expression
=
$node
->
getNode
(
'node'
)
;
if
(
$this
->
isSafeFor
(
$type
,
$expression
,
$env
)
)
{
return
$node
;
}
return
new
InlinePrint
(
$this
->
getEscaperFilter
(
$type
,
$expression
)
,
$node
->
getTemplateLine
(
)
)
;
}