PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.19
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.19
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Services / ShortCodeParser / Parsers / BaseParser.php

BaseParser.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.19, at app/Services/ShortCodeParser/Parsers/BaseParser.php

155 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\ShortCodeParser\Parsers;
4
5 use FluentCart\App\Services\ShortCodeParser\Contracts\ParserContract;
6 use FluentCart\App\Services\ShortCodeParser\ValueTransformer;
7 use FluentCart\Framework\Support\Arr;
8 use FluentCart\Framework\Support\Str;
9
10 abstract class BaseParser implements ParserContract
11 {
12
13 use ValueTransformer;
14
15 protected $data = null;
16
17 /**
18 * @var array $instances
19 *
20 * This static property is used to cache the results of method and attribute lookups.
21 *
22 * The `BaseParser` class uses this array to store the values retrieved by the `get` method.
23 * When a value is requested for a specific code, the `get` method first checks if the value
24 * is already cached in this array. If it is, the cached value is returned, avoiding redundant
25 * lookups or method calls.
26 *
27 * This caching mechanism improves performance by reducing the number of times the same
28 * data needs to be retrieved or computed.
29 */
30 protected static array $instances = [];
31
32 /**
33 * @var array $methodMap
34 *
35 * This property maps codes to their corresponding methods.
36 *
37 * The `BaseParser` class uses this array to determine which method to call
38 * when a specific code is requested. The keys in this array are the codes,
39 * and the values are the names of the methods to be called.
40 *
41 * For example, if the `methodMap` contains an entry `'affiliate_url' => 'getAffiliateUrl'`,
42 * calling `get('affiliate_url')` will invoke the `getAffiliateUrl` method.
43 */
44 protected array $methodMap = [];
45
46 /**
47 * @var array $attributeMap
48 *
49 * This property maps codes to their corresponding attributes in the data array.
50 *
51 * The `BaseParser` class uses this array to determine which attribute to retrieve
52 * when a specific code is requested. The keys in this array are the codes, and the values
53 * are the keys in the data array where the corresponding values can be found.
54 *
55 * For example, if the `attributeMap` contains an entry `'affiliate_name' => 'user_details.full_name'`,
56 * calling `get('affiliate_name')` will retrieve the value stored in `$data['user_details']['full_name']`.
57 */
58 protected array $attributeMap = [];
59
60 public function __construct($data)
61 {
62 $this->data = $data;
63 }
64
65 /**
66 * Abstract method to parse the given code.
67 *
68 * This method must be implemented by any subclass to define
69 * how the parsing of the given code should be handled.
70 *
71 * @param null|string $accessor
72 * @param null|string $template
73 * @return string The parsed result.
74 */
75 abstract public function parse($accessor = null, $template = null): ?string;
76
77 /**
78 * Retrieves the value for the given code.
79 *
80 * This method first checks if the value for the given code is already cached in the `static::$instances` array.
81 * If it is, the cached value is returned. If not, it checks if the code corresponds to a method or an attribute.
82 * If the code corresponds to a method, the `getByMethod` method is called to retrieve the value.
83 * If the code corresponds to an attribute, the `getAttribute` method is called to retrieve the value.
84 * If the code does not correspond to any method or attribute, a placeholder string with the code is returned.
85 *
86 * @param string|null $accessor The code representing the method or attribute to be retrieved.
87 * @param string|null $template The code representing the method or attribute to be retrieved.
88 * @return string The value associated with the given code, or a placeholder string if the code is not found.
89 */
90 public function get(?string $accessor, ?string $template = null): ?string
91 {
92
93
94 $conditions = $this->evaluateCondition($template);
95
96 //update the template after parsing $conditions
97 //order.payment_method||title_case -> to order.payment_method
98 $template = $conditions['accessor'];
99
100 if (isset(static::$instances[$accessor])) {
101 return static::$instances[$accessor];
102 }
103
104 if (isset($this->methodMap[$accessor])) {
105 return $this->getByMethod($accessor, $template, $conditions);
106 }
107
108 if (isset($this->attributeMap[$accessor])) {
109 return $this->getAttribute($accessor);
110 }
111
112 if (method_exists($this, 'get' . Str::studly($accessor))) {
113 $methodName = 'get' . Str::studly($accessor);
114 return $this->{$methodName}($accessor, $template, $conditions);
115 }
116
117
118 return Arr::get($this->data, $accessor) ??
119 Arr::get($this->data, $template);
120 }
121
122 /**
123 * Retrieves the value for the given code from the data array.
124 *
125 * This method uses the `attributeMap` array to find the corresponding key in the data array
126 * and retrieves its value. The result is then stored in the `static::$instances` array
127 * to avoid redundant lookups in the future.
128 *
129 * @param string $code The code representing the attribute to be retrieved.
130 * @return string The value of the attribute associated with the given code.
131 */
132 protected function getAttribute(string $code): string
133 {
134 static::$instances[$code] = Arr::get($this->data, $this->attributeMap[$code]);
135 return static::$instances[$code];
136 }
137
138 /**
139 * Retrieves the value for the given code by invoking the corresponding method.
140 *
141 * This method checks the `methodMap` array for the provided code and calls the
142 * associated method. The result is then stored in the `static::$instances` array
143 * to avoid redundant method calls in the future.
144 *
145 * @param string $accessor The code representing the method to be called.
146 * @param ?string $template The full shortcode.
147 * @return string The result of the method call associated with the given code.
148 */
149 protected function getByMethod(string $accessor, ?string $template, $conditions = []): ?string
150 {
151 static::$instances[$accessor] = $this->{$this->methodMap[$accessor]}($accessor, $template, $conditions);
152 return static::$instances[$accessor];
153 }
154 }
155