Skip to content

Commit

Permalink
splits out classes, properly renames files, cleans up README
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Nov 9, 2010
1 parent cd13e0e commit 7732e13
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 240 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ field.

Define our model that uses the behavior included.

[php]
class LocalizableUnit extends Doctrine_Record
{
public function setTableDefinition()
Expand All @@ -33,7 +32,6 @@ This essentially creates a field on your model, and specifies the unit of measur
to save to the database. This is important for precision reasons.
Now we can use it like the following.

[php]
$unit = new LocatableUnit();
$article->name = "Testing this out";
$article->length = 15; // sets unit to 15 KM
Expand Down Expand Up @@ -61,7 +59,6 @@ Now that article would output the following.
As many fields can act as localizable as you need. You can also pass a conversion array in your declaration to add
unsupported conversions

[php]
class LocalizableUnit extends Doctrine_Record
{
public function setTableDefinition()
Expand Down Expand Up @@ -91,8 +88,7 @@ The conversion array passed to the behavior supports conversion from miles and k

You can also use the `LocalizableConverter` object to perform conversions outside of the extension

[php]
$converter = new LocalizableConverter($additional_conversions);
$converter = new LocalizableConverter();
$celcius = $converter->convert($fahrenheit, 'F', 'C');
$gallons = $liters * $converter->getConversion('L', 'GAL');

File renamed without changes.
45 changes: 45 additions & 0 deletions lib/template/Localizable.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

class Doctrine_Template_Localizable extends Doctrine_Template
{
/**
* Array of locatable options
*/
protected $_options = array('fields' => array(), 'columns' => array(), 'conversions' => array());

/**
* Constructor for Locatable Template
*
* @param array $options
* @return void
* @author Brent Shaffer
*/
public function __construct(array $options = array())
{
$this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
}


public function setup()
{
}


/**
* Set table definition for contactable behavior
* (borrowed from Sluggable in Doctrine core)
*
* @return void
* @author Brent Shaffer
*/
public function setTableDefinition()
{
foreach ($this->_options['fields'] as $field => $unit)
{
$name = Doctrine_Inflector::tableize($field.'_'.strtolower($unit));
$this->_options['columns'][$field] = $name;
$this->hasColumn($name, 'float');
}
$this->_table->unshiftFilter(new Doctrine_Record_Filter_Localizable($this->_options));
}
}
Original file line number Diff line number Diff line change
@@ -1,236 +1,5 @@
<?php

class Doctrine_Template_Localizable extends Doctrine_Template
{
/**
* Array of locatable options
*/
protected $_options = array('fields' => array(), 'columns' => array(), 'conversions' => array());

/**
* Constructor for Locatable Template
*
* @param array $options
* @return void
* @author Brent Shaffer
*/
public function __construct(array $options = array())
{
$this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
}


public function setup()
{
}


/**
* Set table definition for contactable behavior
* (borrowed from Sluggable in Doctrine core)
*
* @return void
* @author Brent Shaffer
*/
public function setTableDefinition()
{
foreach ($this->_options['fields'] as $field => $unit)
{
$name = Doctrine_Inflector::tableize($field.'_'.strtolower($unit));
$this->_options['columns'][$field] = $name;
$this->hasColumn($name, 'float');
}
$this->_table->unshiftFilter(new Doctrine_Record_Filter_Localizable($this->_options));
}
}

/**
* Localizable Unit
*/
class LocalizableUnit implements ArrayAccess
{
protected $_unit, $_value,
$_converter;

function __construct($value, $unit, $conversions = array())
{
$this->setValue($value, $unit);
$this->_converter = new LocalizableConverter($conversions);
}

public function setValue($value, $unit = null)
{
$this->_value = $value;
if ($unit)
{
$this->_unit = strtolower($unit);
}
}

function __toString()
{
return (string)$this->_value;
}

/**
* Set key and value to data
*
* @see set, offsetSet
* @param $name
* @param $value
* @return void
*/
public function __set($name, $value)
{
$this->set($name, $value);
}

/**
* Get key from data
*
* @see get, offsetGet
* @param mixed $name
* @return mixed
*/
public function __get($name)
{
return $this->get($name);
}

/**
* Check if key exists in data
*
* @param string $name
* @return boolean whether or not this object contains $name
*/
public function __isset($name)
{
return $this->contains($name);
}

/**
* Remove key from data
*
* @param string $name
* @return void
*/
public function __unset($name)
{
return $this->remove($name);
}

/**
* Check if an offset axists
*
* @param mixed $offset
* @return boolean Whether or not this object contains $offset
*/
public function offsetExists($offset)
{
return $this->contains($offset);
}

/**
* An alias of get()
*
* @see get, __get
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->get($offset);
}

/**
* Sets $offset to $value
*
* @see set, __set
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
if ( ! isset($offset)) {
$this->add($value);
} else {
$this->set($offset, $value);
}
}

/**
* Unset a given offset
*
* @see set, offsetSet, __set
* @param mixed $offset
*/
public function offsetUnset($offset)
{
return $this->remove($offset);
}

/**
* Remove the element with the specified offset
*
* @param mixed $offset The offset to remove
* @return boolean True if removed otherwise false
*/
public function remove($offset)
{
throw new Doctrine_Exception('Remove is not supported for ' . get_class($this));
}

/**
* Return the element with the specified offset
*
* @param mixed $offset The offset to return
* @return mixed
*/
public function get($offset)
{
return $this->_converter->convert($this->_value, $this->_unit, strtolower($offset));
}

/**
* Set the offset to the value
*
* @param mixed $offset The offset to set
* @param mixed $value The value to set the offset to
*
*/
public function set($offset, $value)
{
$this->_value = $this->_converter->convert($value, strtolower($offset), $this->_unit);
}

/**
* Check if the specified offset exists
*
* @param mixed $offset The offset to check
* @return boolean True if exists otherwise false
*/
public function contains($offset)
{
try {
$conversion = $this->_converter->getConversion(strtolower($offset), $this->_unit);
return true;
} catch (Exception $e) {}
return false;
}

/**
* Add the value
*
* @param mixed $value The value to add
* @return void
*/
public function add($value)
{
throw new Doctrine_Exception('Add is not supported for ' . get_class($this));
}
}

/**
* Converter Class
*/
Expand Down Expand Up @@ -303,7 +72,6 @@ class LocalizableConverter
'ft' => .08333,
'in' => 1),
// WEIGHT

'kg' => array('kg' => 1,
'g' => 1000,
'mg' => 1000000,
Expand All @@ -329,8 +97,8 @@ class LocalizableConverter
'mg' => 28349.5231,
'lb' => .0625,
'oz' => 1),
// TEMPURATURE


// TEMPURATURE
'c' => array('c' => 1,
'k' => array('factor' => 1, 'deltha' => 273.15),
'f' => array('factor' => 1.8, 'deltha' => 32)),
Expand All @@ -340,8 +108,8 @@ class LocalizableConverter
'f' => array('c' => array('factor' => .55555555, 'deltha' => -17.777778),
'k' => array('factor' => .55555555, 'deltha' => 255.372222),
'f' => 1),
// VOLUME

// VOLUME
'ml' => array('ml' => 1,
'l' => .001,
'ft3' => .000035314667,
Expand Down Expand Up @@ -407,6 +175,7 @@ class LocalizableConverter
'cup' => .125,
'floz' => 1),
);

function __construct($conversions = array())
{
$this->_conversions = Doctrine_Lib::arrayDeepMerge($this->_conversions, $conversions);
Expand All @@ -415,6 +184,7 @@ function __construct($conversions = array())
public function convert($value, $from, $to)
{
$conversion = $this->getConversion($from, $to);

if (is_array($conversion))
{
/* ORDER OF OPERATIONS IS KEY HERE */
Expand Down Expand Up @@ -449,6 +219,7 @@ public function getConversion($from, $to)
{
return $this->_conversions[$from][$to];
}

throw new Exception("Conversion not found for '$from' to '$to'");
}
}
Loading

0 comments on commit 7732e13

Please sign in to comment.