| 1 |
<?php |
| 2 |
/** |
| 3 |
* Disco Attribute Factory |
| 4 |
* |
| 5 |
* @package Disco |
| 6 |
* @subpackage Disco\App\Attributes |
| 7 |
* @category MyCategory |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Disco\App\Attributes; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class AttributeFactory |
| 14 |
* |
| 15 |
* @package Disco |
| 16 |
* @subpackage Disco\App\Attributes |
| 17 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 18 |
* @link https://webappick.com |
| 19 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 20 |
* @category MyCategory |
| 21 |
*/ |
| 22 |
class AttributeFactory { |
| 23 |
|
| 24 |
/** |
| 25 |
* AttributeFactory constructor. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
// class constructor |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get Attribute Type, Initialize Class and Return Value. |
| 33 |
* If Method is Not Found, Return Empty String. |
| 34 |
* |
| 35 |
* @param string $method_name Method Name. |
| 36 |
* @param array $info Class Object. |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public static function get_value( $method_name, $info ) { |
| 40 |
$class_details = self::get_class_and_method( $method_name, $info ); |
| 41 |
|
| 42 |
if ( ! $class_details ) { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
list( $class_instance, $method, $key ) = $class_details; |
| 47 |
|
| 48 |
if ( ! method_exists( $class_instance, $method ) ) { |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
if ( $key ) { |
| 53 |
return $class_instance->$method( $key ); |
| 54 |
} |
| 55 |
|
| 56 |
return $class_instance->$method(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get Attribute Type, Initialize Class and Return Value. |
| 61 |
* |
| 62 |
* @param string $method_name Method Name. |
| 63 |
* @param array $info Class Object. |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
public static function get_class_and_method( $method_name, $info ) {// phpcs:ignore |
| 67 |
$prefix_mappings = array( |
| 68 |
'customer_history' => array( |
| 69 |
CustomerHistory::class, |
| 70 |
null, |
| 71 |
), |
| 72 |
'product_history' => array( |
| 73 |
ProductHistory::class, |
| 74 |
null, |
| 75 |
), |
| 76 |
'customer' => array( |
| 77 |
Customer::class, |
| 78 |
null, |
| 79 |
), |
| 80 |
'cart' => array( |
| 81 |
Cart::class, |
| 82 |
null, |
| 83 |
), |
| 84 |
'products_in_cart' => array( |
| 85 |
Cart::class, |
| 86 |
'products_in_cart', |
| 87 |
), |
| 88 |
'global_attribute_' => array( |
| 89 |
Product::class, |
| 90 |
'get_attribute', |
| 91 |
), |
| 92 |
'product_brand' => array( |
| 93 |
Product::class, |
| 94 |
'product_brand', |
| 95 |
), |
| 96 |
'custom_attribute_' => array( |
| 97 |
Product::class, |
| 98 |
'get_attribute', |
| 99 |
), |
| 100 |
'product_meta_' => array( |
| 101 |
Product::class, |
| 102 |
'get_product_meta', |
| 103 |
), |
| 104 |
// Acf Fields. |
| 105 |
'acf_fields_' => array( |
| 106 |
Product::class, |
| 107 |
'get_product_meta', |
| 108 |
), |
| 109 |
); |
| 110 |
|
| 111 |
foreach ( $prefix_mappings as $prefix => $class_and_method ) { |
| 112 |
if ( strpos( $method_name, $prefix ) !== 0 ) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
$class_name = $class_and_method[0]; |
| 117 |
|
| 118 |
if ( $class_and_method[1] ) { |
| 119 |
$method = $class_and_method[1]; |
| 120 |
} else { |
| 121 |
$method = $method_name; |
| 122 |
} |
| 123 |
|
| 124 |
if ( $method === $method_name ) { |
| 125 |
$key = null; |
| 126 |
} else { |
| 127 |
$key = str_replace( $prefix, '', $method_name ); |
| 128 |
} |
| 129 |
|
| 130 |
// If the class is Cart or Customer, we don't need to pass any info. |
| 131 |
if ( $class_name === Cart::class || $class_name === Customer::class ) { |
| 132 |
return array( |
| 133 |
new $class_name, |
| 134 |
$method, |
| 135 |
$key, |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $class_name === CustomerHistory::class || $class_name === ProductHistory::class ) { |
| 140 |
// remove prefix from method name. |
| 141 |
$method = str_replace( $prefix . '_', '', $method_name ); |
| 142 |
} |
| 143 |
|
| 144 |
if ( $class_name === ProductHistory::class ) { |
| 145 |
$product = $info['product']; |
| 146 |
\assert( $product instanceof \WC_Product ); |
| 147 |
|
| 148 |
return array( |
| 149 |
new $class_name( $info, $product ), |
| 150 |
$method, |
| 151 |
$key, |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
if ( $class_name === Product::class ) { |
| 156 |
$product = $info['product']; |
| 157 |
\assert( $product instanceof \WC_Product ); |
| 158 |
|
| 159 |
return array( |
| 160 |
new $class_name( $product ), |
| 161 |
$method, |
| 162 |
$key, |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
return array( |
| 167 |
new $class_name( $info ), |
| 168 |
$method, |
| 169 |
$key, |
| 170 |
); |
| 171 |
}//end foreach |
| 172 |
|
| 173 |
$product = $info['product']; |
| 174 |
\assert( $product instanceof \WC_Product ); |
| 175 |
|
| 176 |
return array( |
| 177 |
new Product( $product ), |
| 178 |
$method_name, |
| 179 |
null, |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|