Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #297
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
/** | ||
*------------------------------------------------------------------------------ | ||
* @package T3 Framework for Joomla! | ||
*------------------------------------------------------------------------------ | ||
* @copyright Copyright (C) 2004-2013 JoomlArt.com. All Rights Reserved. | ||
* @license GNU General Public License version 2 or later; see LICENSE.txt | ||
* @authors JoomlArt, JoomlaBamboo, (contribute to this project at github | ||
* & Google group to become co-author) | ||
* @Google group: https://groups.google.com/forum/#!forum/t3fw | ||
* @Link: http://t3-framework.org | ||
*------------------------------------------------------------------------------ | ||
*/ | ||
|
||
namespace Joomla\Registry\Format; | ||
|
||
use Joomla\Registry\AbstractRegistryFormat; | ||
use stdClass; | ||
|
||
/** | ||
* PHP class format handler for Registry | ||
* | ||
* @since 1.0 | ||
*/ | ||
class Less extends AbstractRegistryFormat | ||
{ | ||
/** | ||
* Converts a name/value pairs object into an LESS variables declaration | ||
*/ | ||
public function objectToString($object, $params = array()) | ||
{ | ||
// Initialize variables. | ||
$result = array(); | ||
|
||
// Iterate over the object to set the properties. | ||
foreach (get_object_vars($object) as $key => $value) | ||
{ | ||
// If the value is an object then we need to put it in a local section. | ||
$result[] = $this->getKey($key) . ': ' . $this->getValue($value); | ||
} | ||
|
||
return implode("\n", $result); | ||
} | ||
|
||
/** | ||
* Converts an LESS variables string to name/value pair object | ||
*/ | ||
public function stringToObject($data, array $options = array()) | ||
{ | ||
// If no lines present just return the object. | ||
if (empty($data)) | ||
{ | ||
return new stdClass; | ||
} | ||
|
||
// Initialize variables. | ||
$obj = new stdClass; | ||
$lines = explode("\n", $data); | ||
|
||
// Process the lines. | ||
foreach ($lines as $line) | ||
{ | ||
// Trim any unnecessary whitespace. | ||
$line = trim($line); | ||
|
||
// Ignore empty lines and comments. | ||
if (empty($line) || (substr($line, 0, 1) == '/') || (substr($line, 0, 1) == '*')) | ||
{ | ||
continue; | ||
} | ||
|
||
// Check that an equal sign exists and is not the first character of the line. | ||
if (!strpos($line, ':')) | ||
{ | ||
// Maybe throw exception? | ||
continue; | ||
} | ||
|
||
// Get the key and value for the line. | ||
list ($key, $value) = explode(':', $line, 2); | ||
|
||
// Validate the key. | ||
if (preg_match('/@[^A-Z0-9_]/i', $key)) | ||
{ | ||
// Maybe throw exception? | ||
continue; | ||
} | ||
|
||
// Validate the value. | ||
//if (preg_match('/[^\(\)A-Z0-9_-];$/i', $value)) | ||
//{ | ||
// Maybe throw exception? | ||
// continue; | ||
//} | ||
|
||
// If the value is quoted then we assume it is a string. | ||
|
||
$key = str_replace('@', '', $key); | ||
$value = str_replace(';', '', $value); | ||
$value = preg_replace('/\/\/(.*)/', '', $value); | ||
$value = trim($value); | ||
$obj->$key = $value; | ||
} | ||
|
||
// Cache the string to save cpu cycles -- thus the world :) | ||
|
||
return $obj; | ||
} | ||
|
||
/** | ||
* Method to get a value in an INI format. | ||
* | ||
* @param mixed $value The value to convert to INI format. | ||
* | ||
* @return string The value in INI format. | ||
* | ||
* @since 11.1 | ||
*/ | ||
protected function getValue($value) | ||
{ | ||
return $value . ';'; | ||
} | ||
|
||
/** | ||
* Method to get a value in an INI format. | ||
* | ||
* @param mixed $key | ||
* | ||
* @return string The value in INI format. | ||
* | ||
* @since 11.1 | ||
*/ | ||
protected function getKey($key) | ||
{ | ||
return '@' . $key; | ||
} | ||
} |