| 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 |
* Per-instance cache of method and attribute lookups for the record this parser |
| 21 |
* was constructed with. Must stay instance-scoped, not static — a static cache |
| 22 |
* keyed only by accessor bleeds values across different records parsed in the |
| 23 |
* same request (e.g. two orders processed by one cron run). |
| 24 |
*/ |
| 25 |
protected array $instances = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var array $methodMap |
| 29 |
* |
| 30 |
* This property maps codes to their corresponding methods. |
| 31 |
* |
| 32 |
* The `BaseParser` class uses this array to determine which method to call |
| 33 |
* when a specific code is requested. The keys in this array are the codes, |
| 34 |
* and the values are the names of the methods to be called. |
| 35 |
* |
| 36 |
* For example, if the `methodMap` contains an entry `'affiliate_url' => 'getAffiliateUrl'`, |
| 37 |
* calling `get('affiliate_url')` will invoke the `getAffiliateUrl` method. |
| 38 |
*/ |
| 39 |
protected array $methodMap = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var array $attributeMap |
| 43 |
* |
| 44 |
* This property maps codes to their corresponding attributes in the data array. |
| 45 |
* |
| 46 |
* The `BaseParser` class uses this array to determine which attribute to retrieve |
| 47 |
* when a specific code is requested. The keys in this array are the codes, and the values |
| 48 |
* are the keys in the data array where the corresponding values can be found. |
| 49 |
* |
| 50 |
* For example, if the `attributeMap` contains an entry `'affiliate_name' => 'user_details.full_name'`, |
| 51 |
* calling `get('affiliate_name')` will retrieve the value stored in `$data['user_details']['full_name']`. |
| 52 |
*/ |
| 53 |
protected array $attributeMap = []; |
| 54 |
|
| 55 |
public function __construct($data) |
| 56 |
{ |
| 57 |
$this->data = $data; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Abstract method to parse the given code. |
| 62 |
* |
| 63 |
* This method must be implemented by any subclass to define |
| 64 |
* how the parsing of the given code should be handled. |
| 65 |
* |
| 66 |
* @param null|string $accessor |
| 67 |
* @param null|string $template |
| 68 |
* @return string The parsed result. |
| 69 |
*/ |
| 70 |
abstract public function parse($accessor = null, $template = null): ?string; |
| 71 |
|
| 72 |
/** |
| 73 |
* Retrieves the value for the given code. |
| 74 |
* |
| 75 |
* This method first checks if the value for the given code is already cached in the `$this->instances` array. |
| 76 |
* If it is, the cached value is returned. If not, it checks if the code corresponds to a method or an attribute. |
| 77 |
* If the code corresponds to a method, the `getByMethod` method is called to retrieve the value. |
| 78 |
* If the code corresponds to an attribute, the `getAttribute` method is called to retrieve the value. |
| 79 |
* If the code does not correspond to any method or attribute, a placeholder string with the code is returned. |
| 80 |
* |
| 81 |
* @param string|null $accessor The code representing the method or attribute to be retrieved. |
| 82 |
* @param string|null $template The code representing the method or attribute to be retrieved. |
| 83 |
* @return string The value associated with the given code, or a placeholder string if the code is not found. |
| 84 |
*/ |
| 85 |
public function get(?string $accessor, ?string $template = null): ?string |
| 86 |
{ |
| 87 |
|
| 88 |
|
| 89 |
$conditions = $this->evaluateCondition($template); |
| 90 |
|
| 91 |
//update the template after parsing $conditions |
| 92 |
//order.payment_method||title_case -> to order.payment_method |
| 93 |
$template = $conditions['accessor']; |
| 94 |
|
| 95 |
if (isset($this->instances[$accessor])) { |
| 96 |
return $this->instances[$accessor]; |
| 97 |
} |
| 98 |
|
| 99 |
if (isset($this->methodMap[$accessor])) { |
| 100 |
return $this->getByMethod($accessor, $template, $conditions); |
| 101 |
} |
| 102 |
|
| 103 |
if (isset($this->attributeMap[$accessor])) { |
| 104 |
return $this->getAttribute($accessor); |
| 105 |
} |
| 106 |
|
| 107 |
if (method_exists($this, 'get' . Str::studly($accessor))) { |
| 108 |
$methodName = 'get' . Str::studly($accessor); |
| 109 |
return $this->{$methodName}($accessor, $template, $conditions); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
return Arr::get($this->data, $accessor) ?? |
| 114 |
Arr::get($this->data, $template); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Retrieves the value for the given code from the data array. |
| 119 |
* |
| 120 |
* This method uses the `attributeMap` array to find the corresponding key in the data array |
| 121 |
* and retrieves its value. The result is then stored in the `$this->instances` array |
| 122 |
* to avoid redundant lookups in the future. |
| 123 |
* |
| 124 |
* @param string $code The code representing the attribute to be retrieved. |
| 125 |
* @return string The value of the attribute associated with the given code. |
| 126 |
*/ |
| 127 |
protected function getAttribute(string $code): string |
| 128 |
{ |
| 129 |
$this->instances[$code] = Arr::get($this->data, $this->attributeMap[$code]); |
| 130 |
return $this->instances[$code]; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Retrieves the value for the given code by invoking the corresponding method. |
| 135 |
* |
| 136 |
* This method checks the `methodMap` array for the provided code and calls the |
| 137 |
* associated method. The result is then stored in the `$this->instances` array |
| 138 |
* to avoid redundant method calls in the future. |
| 139 |
* |
| 140 |
* @param string $accessor The code representing the method to be called. |
| 141 |
* @param ?string $template The full shortcode. |
| 142 |
* @return string The result of the method call associated with the given code. |
| 143 |
*/ |
| 144 |
protected function getByMethod(string $accessor, ?string $template, $conditions = []): ?string |
| 145 |
{ |
| 146 |
$this->instances[$accessor] = $this->{$this->methodMap[$accessor]}($accessor, $template, $conditions); |
| 147 |
return $this->instances[$accessor]; |
| 148 |
} |
| 149 |
} |
| 150 |
|