<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: prohalexey</title>
    <description>The latest articles on DEV Community by prohalexey (@prohalexey).</description>
    <link>https://dev.to/prohalexey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F203828%2F5796623e-2fa0-4bdc-9506-0bdb38c468e5.png</url>
      <title>DEV Community: prohalexey</title>
      <link>https://dev.to/prohalexey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prohalexey"/>
    <language>en</language>
    <item>
      <title>Business rules engine library on PHP</title>
      <dc:creator>prohalexey</dc:creator>
      <pubDate>Tue, 30 Jul 2019 10:26:24 +0000</pubDate>
      <link>https://dev.to/prohalexey/business-rules-engine-library-on-php-eih</link>
      <guid>https://dev.to/prohalexey/business-rules-engine-library-on-php-eih</guid>
      <description>&lt;p&gt;A few months ago, I wrote a library that implements BRE (Business Rules Engine).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is this library for?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This library allows you to simplify the writing of rules for business processes, such as complex discounts calculation or giving bonuses to your customers or any other logic.&lt;/p&gt;

&lt;p&gt;It can be useful for you if you frequently change certain conditions in your code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What features does this library provide?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It allows you to move these conditions to configuration files or create web interface that can edit configurations.&lt;/p&gt;

&lt;p&gt;You can write rules in JSON or YAML format and store them into files or in the some database.&lt;/p&gt;

&lt;p&gt;Example of Yaml config&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node: condition
if:
  node: collection
  type: and
  elements:
  - node: context
    context: withdrawalCount
    operator: equal
    value: 0
  - node: context
    context: inGroup
    operator: arrayContain
    value:
      - testgroup
      - testgroup2
then:
  node: context
  context: getDepositSum
  description: "Giving 10% of deposit's sum as discount for the next order"
  modifiers: 
    - "$context * 0.1"
  params:
    discountType: "VIP client"
else:
  node: value
  description: "Giving 5% for the next order"
  value: 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Process rule in PHP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create a parser &lt;/span&gt;
&lt;span class="nv"&gt;$parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;YamlBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;OperatorFactory&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Load rules from a file or other sources&lt;/span&gt;
&lt;span class="nv"&gt;$node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$parser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;parseFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Yaml/testOneNodeWithRuleGreaterThan.yaml'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Define contexts&lt;/span&gt;
&lt;span class="nv"&gt;$contextFactory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ContextFactory&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="s1"&gt;'getDepositSum'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;InGroup&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'withdrawalCount'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;WithdrawalCount&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'depositCount'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;DepositCount&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'utmSource'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;UtmSource&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="c1"&gt;// Here you can use PSR-11 container to resolve objects, callable or just class names&lt;/span&gt;
&lt;span class="nv"&gt;$contextFactory&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;setContainer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$container&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Instantiating tree(rules) processor&lt;/span&gt;
&lt;span class="nv"&gt;$treeProcessor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;TreeProcessor&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;setContextFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$contextFactory&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// And process the rules&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$treeProcessor&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you know PHP, you have a little time and maybe you are interested in such a library - leave a comment to my code.&lt;/p&gt;

&lt;p&gt;See the full code on the &lt;a href="https://github.com/prohalexey/TheChoice"&gt;https://github.com/prohalexey/TheChoice&lt;/a&gt;&lt;/p&gt;

</description>
      <category>businessrules</category>
      <category>php</category>
    </item>
  </channel>
</rss>
