{"id":525,"date":"2014-07-24T15:19:34","date_gmt":"2014-07-24T22:19:34","guid":{"rendered":"http:\/\/www.keganv.com\/?p=525"},"modified":"2014-07-24T15:23:09","modified_gmt":"2014-07-24T22:23:09","slug":"real-world-example-use-of-the-symfony-expression-language","status":"publish","type":"post","link":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/","title":{"rendered":"Real World Example Use Of The Symfony Expression Language"},"content":{"rendered":"<p>Recently I&#8217;ve had the pleasure of getting my hands dirty with the Symfony Expression Language. The opportunity arose because I was saddled with a really cool task of developing a custom calculator tailored for the financial advising industry. At <a href=\"http:\/\/www.advisorsexcel.com\" target=\"_blank\">my job<\/a>, my teammates and I <em>mainly<\/em> work with the Symfony framework for the majority of our projects.<br \/>\n<!--more--><\/p>\n<p>Without diving into this large project&#8217;s details and all the code it took to make it, (which would simply take way too long to explain), I want to stick with the topic of how easy it was to use the expression language itself. However, I feel a brief overview of the steps that lead to creating a string that I gets passed to the Expression Language class is probably necessary.<\/p>\n<p>Let&#8217;s first look at the UI of the calculator I created. There is a lot going on here. Basically, the user can select from the menu on the left an operator, number, function, or variable that builds the html on the right (tabbed area). Each row and item in the html calculation itself can be deleted, sorted, or duplicated and certain items like functions can have infinitely nested items. Cool, right!?<\/p>\n<p><a href=\"https:\/\/www.keganv.com\/wp-content\/uploads\/calculation-screenshot.png\"><img decoding=\"async\" src=\"https:\/\/www.keganv.com\/wp-content\/uploads\/calculation-screenshot-800x415.png\" alt=\"Calculator Screenshot\" class=\"aligncenter\" \/><\/a><\/p>\n<p>I then wrote a few jQuery scripts that parse through the calculator&#8217;s html, first building a multidimensional array that is then used to build a JSON object out of it that gets passed to a service class through a controller action.<\/p>\n<p>In a nutshell the service loops through the JSON object and creates a string for the expression language to evaluate. I made it so that the expression language itself didn&#8217;t have to evaluate variables, objects, hashes, booleans, arrays, or methods because I wanted to handle those on my end for accuracy and reliability. However, you can do that with the <a href=\"http:\/\/symfony.com\/doc\/current\/components\/expression_language\/index.html\" target=\"_blank\">Expression Language component<\/a> as well.<\/p>\n<p>I am simply passing operators and numbers concatenated together in a string. The <strong>awesome<\/strong> thing about the component is how reliably it takes care of operator precedence, which is one of two reasons I used it, the other was to avoid the dangerous <code>eval()<\/code> function.<\/p>\n<p>Let&#8217;s take a look at the service that utilizes the Expression Language, more specifically, the method. The <code>$this->looper<\/code> is a private method that, you guessed it, loops through the array and builds the string to pass to the Expression Language. It&#8217;s really a breeze from here, declare a <code>new ExpressionLanguage()<\/code> instance and then I call the evaluate method passing the string. Easy peasy!<\/p>\n<pre>\r\n<?php\r\n\r\nnamespace AE\\CalcBundle\\Services;\r\n\r\nuse Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage;\r\n\r\n\/\/... other use statements\r\n\r\nclass Calculation {\r\n    \/\/... private properties\r\n\r\n    \/\/... public construct \r\n\r\n    public function calculate($json)\r\n    {\r\n        $calcArray = json_decode($json, TRUE)['calc'];\r\n        try {\r\n\r\n            \/\/ Pass the calculation array to the looper to build the expression\r\n            $calcString = $this->looper($calcArray);\r\n\r\n            \/\/ Pass the expression string to the expression object\r\n            $language = new ExpressionLanguage();\r\n            return $language->evaluate($calcString);\r\n\r\n        } catch( \\Exception $e) {\r\n            return $e->getMessage();\r\n        }\r\n    }\r\n<\/pre>\n<p>Just for curiosities sake, let&#8217;s look at an example string that would get passed to the evaluate method. My looper method could create a string that looks like this: <code>65*10+0.04-65\/85*9.54<\/code> and the Expression Engine gives us an answer of <code>642.74470588235<\/code>. Pretty cool if you ask me!<\/p>\n","protected":false},"excerpt":{"rendered":"Recently I&#8217;ve had the pleasure of getting my hands dirty with the Symfony Expression Language. The opportunity arose because I was saddled with a really cool task of developing a custom calculator tailored for the financial advising industry. At my job, my teammates and I mainly work with the Symfony framework for the majority of &#8230; <br\/> <a class=\"view-article btn btn-primary flip pull-left\" href=\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\"><span>View Article<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-525","post","type-post","status-publish","format-standard","hentry","category-coding-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Symfony Expression Language - Real World Use | KeganV<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Symfony Expression Language - Real World Use | KeganV\" \/>\n<meta property=\"og:description\" content=\"Recently I&#8217;ve had the pleasure of getting my hands dirty with the Symfony Expression Language. The opportunity arose because I was saddled with a really cool task of developing a custom calculator tailored for the financial advising industry. At my job, my teammates and I mainly work with the Symfony framework for the majority of ... View Article\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\" \/>\n<meta property=\"og:site_name\" content=\"KeganV\" \/>\n<meta property=\"article:published_time\" content=\"2014-07-24T22:19:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-07-24T22:23:09+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.keganv.com\/wp-content\/uploads\/calculation-screenshot-800x415.png\" \/>\n<meta name=\"author\" content=\"Kegan V.\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kegan V.\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\"},\"author\":{\"name\":\"Kegan V.\",\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"headline\":\"Real World Example Use Of The Symfony Expression Language\",\"datePublished\":\"2014-07-24T22:19:34+00:00\",\"dateModified\":\"2014-07-24T22:23:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\"},\"wordCount\":461,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"articleSection\":[\"Coding &amp; Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\",\"url\":\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\",\"name\":\"Symfony Expression Language - Real World Use | KeganV\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/#website\"},\"datePublished\":\"2014-07-24T22:19:34+00:00\",\"dateModified\":\"2014-07-24T22:23:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.keganv.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Real World Example Use Of The Symfony Expression Language\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.keganv.com\/#website\",\"url\":\"https:\/\/www.keganv.com\/\",\"name\":\"KeganV\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.keganv.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\",\"name\":\"Kegan V.\",\"logo\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.keganv.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Symfony Expression Language - Real World Use | KeganV","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/","og_locale":"en_US","og_type":"article","og_title":"Symfony Expression Language - Real World Use | KeganV","og_description":"Recently I&#8217;ve had the pleasure of getting my hands dirty with the Symfony Expression Language. The opportunity arose because I was saddled with a really cool task of developing a custom calculator tailored for the financial advising industry. At my job, my teammates and I mainly work with the Symfony framework for the majority of ... View Article","og_url":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/","og_site_name":"KeganV","article_published_time":"2014-07-24T22:19:34+00:00","article_modified_time":"2014-07-24T22:23:09+00:00","og_image":[{"url":"http:\/\/www.keganv.com\/wp-content\/uploads\/calculation-screenshot-800x415.png"}],"author":"Kegan V.","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kegan V.","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#article","isPartOf":{"@id":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/"},"author":{"name":"Kegan V.","@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"headline":"Real World Example Use Of The Symfony Expression Language","datePublished":"2014-07-24T22:19:34+00:00","dateModified":"2014-07-24T22:23:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/"},"wordCount":461,"commentCount":0,"publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"articleSection":["Coding &amp; Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/","url":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/","name":"Symfony Expression Language - Real World Use | KeganV","isPartOf":{"@id":"https:\/\/www.keganv.com\/#website"},"datePublished":"2014-07-24T22:19:34+00:00","dateModified":"2014-07-24T22:23:09+00:00","breadcrumb":{"@id":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.keganv.com\/real-world-example-use-of-the-symfony-expression-language\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.keganv.com\/"},{"@type":"ListItem","position":2,"name":"Real World Example Use Of The Symfony Expression Language"}]},{"@type":"WebSite","@id":"https:\/\/www.keganv.com\/#website","url":"https:\/\/www.keganv.com\/","name":"KeganV","description":"","publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.keganv.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276","name":"Kegan V.","logo":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.keganv.com"]}]}},"_links":{"self":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/525"}],"collection":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/comments?post=525"}],"version-history":[{"count":19,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":546,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/525\/revisions\/546"}],"wp:attachment":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/media?parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/categories?post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/tags?post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}