Created
December 9, 2011 17:27
-
-
Save razbakov/1452481 to your computer and use it in GitHub Desktop.
Templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeClass { | |
public function somefunction() | |
{ | |
$orderId = 1; // some order id for example | |
$order->load($orderId); | |
$vars['order_id'] = $order->getIncrementId(); | |
$vars['order_date'] = $order->getCreatedAt(); | |
$vars['order_day'] = date('d', strtotime($order->getCreatedAt())); | |
$vars['order_month'] = date('m', strtotime($order->getCreatedAt())); | |
$vars['order_year'] = date('Y', strtotime($order->getCreatedAt())); | |
$vars['prefix'] = $order->getShippingAddress()->getPrefix(); | |
$vars['first_name'] = $order->getShippingAddress()->getFirstname(); | |
$vars['last_name'] = $order->getShippingAddress()->getLastname(); | |
$vars['city'] = $order->getShippingAddress()->getCity(); | |
$vars['street'] = $order->getShippingAddress()->getStreet1(); | |
$vars['house'] = $order->getShippingAddress()->getStreet2(); | |
$vars['zip'] = $order->getShippingAddress()->getPostcode(); | |
$vars['email'] = $order->getShippingAddress()->getEmail(); | |
$vars['country_code'] = $order->getShippingAddress()->getCountryModel()->getIso2Code(); | |
$this->_applyRules($vars, $rules); | |
$result = $this->_applyTemplate($exportTemplate, $vars); | |
} | |
protected function _parseRules(&$string, &$rules) | |
{ | |
preg_match_all('#{([a-z_]+):([0-9]+)}#is', $string, $matches); | |
foreach($matches[1] as $id=>$param) { | |
$rules[$param] = $matches[2][$id]; | |
$string = str_replace($matches[0][$id], '{'.$matches[1][$id].'}', $string); | |
} | |
} | |
protected function _applyRules(&$vars, $rules) | |
{ | |
foreach($rules as $key => $value) { | |
if(isset($vars[$key])) { | |
$vars[$key] = str_pad($vars[$key], $value); | |
} | |
} | |
} | |
protected function _applyTemplate($template, $vars) | |
{ | |
$result = $template; | |
foreach ($vars as $var => $value) { | |
$result = str_replace( | |
'{' . $var . '}', | |
$value, | |
$result | |
); | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment