From: <gem...@li...> - 2012-12-29 19:35:32
|
Revision: 1079 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1079&view=rev Author: matijsdejong Date: 2012-12-29 19:35:24 +0000 (Sat, 29 Dec 2012) Log Message: ----------- Started on 'model needs to have a function to format values' Modified Paths: -------------- trunk/library/classes/MUtil/Version.php Added Paths: ----------- trunk/library/classes/MUtil/Model/Assembler/ trunk/library/classes/MUtil/Model/Assembler/FormAssembler.php trunk/library/classes/MUtil/Model/AssemblerAbstract.php trunk/library/classes/MUtil/Model/AssemblerInterface.php trunk/library/classes/MUtil/Model/Input.php trunk/library/classes/MUtil/Model/Processor/ trunk/library/classes/MUtil/Model/Processor/ChainedProcessor.php trunk/library/classes/MUtil/Model/Processor/Display/ trunk/library/classes/MUtil/Model/Processor/Display/FormatsProcessor.php trunk/library/classes/MUtil/Model/Processor/Display/MultiOptionProcessor.php trunk/library/classes/MUtil/Model/Processor/Element/ trunk/library/classes/MUtil/Model/Processor/Element/TextElementProcessor.php trunk/library/classes/MUtil/Model/Processor/ElementProcessorAbstract.php trunk/library/classes/MUtil/Model/ProcessorInterface.php Added: trunk/library/classes/MUtil/Model/Assembler/FormAssembler.php =================================================================== --- trunk/library/classes/MUtil/Model/Assembler/FormAssembler.php (rev 0) +++ trunk/library/classes/MUtil/Model/Assembler/FormAssembler.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,60 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: FormAssembler.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +class MUtil_Model_Assembler_FormAssembler extends MUtil_Model_AssemblerAbstract +{ + /** + * Create the processor for this name + * + * @param string $name + * @return MUtil_Model_ProcessorInterface or null when it does not exist + */ + protected function _assemble($name) + { + return new MUtil_Model_Processor_Element_TextElementProcessor(); + } + +} Added: trunk/library/classes/MUtil/Model/AssemblerAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/AssemblerAbstract.php (rev 0) +++ trunk/library/classes/MUtil/Model/AssemblerAbstract.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,252 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: AssemblerAbstract.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +abstract class MUtil_Model_AssemblerAbstract implements MUtil_Model_AssemblerInterface +{ + /** + * + * @var mixed + */ + protected $_data = array(); + + /** + * + * @var MUtil_Model_ModelAbstract + */ + protected $_model; + + /** + * + * @var array Of name => MUtil_Model_ProcessorInterface + */ + protected $_processors = array(); + + /** + * + * @var MUtil_Lazy_RepeatableInterface + */ + protected $_repeater; + + /** + * + * @var array + */ + protected $_row = array(); + + /** + * Create the processor for this name + * + * @param string $name + * @return MUtil_Model_ProcessorInterface or null when it does not exist + */ + abstract protected function _assemble($name); + + /** + * Get the processed output of the input or a lazy object if the data is repeated + * or not yet set using setRepeater() or setRow(). + * + * @param string $name + * @return mixed MUtil_Lazy_Call when not using setRow(), actual output otherwise + */ + public function getOutput($name) + { + if (! $this->hasProcessor($name)) { + if ($this->_row) { + if (isset($this->_row[$name])) { + return $this->_row[$name]; + } + + return null; + } + // Fall through and return lazy call + + } else { + // Assembler should be created with current values + $processor = $this->getProcessor($name); + + if ($this->_row) { + $input = new MUtil_Model_Input($this->_model, $name, $this->_row); + + $processor->process($input); + + return $input->getOutput(); + } + } + + return new MUtil_Lazy_Call(array($this, 'output'), array($name)); + } + + /** + * Returns the processor for the name + * + * @param string $name + * @return MUtil_Model_ProcessorInterface or null when it does not exist + */ + public function getProcessor($name) + { + if ($this->hasProcessor($name)) { + return $this->_processors[$name]; + } + } + + /** + * Returns true if a processor exist for $name + * + * @param string $name + * @return boolean + */ + public function hasProcessor($name) + { + if (! array_key_exists($name, $this->_processors)) { + // Default if nothing there + $this->_processors[$name] = false; + + // Try to create one + if ($this->_model->has($name)) { + if ($processor = $this->_assemble($name)) { + $this->setProcessor($name, $processor); + } + } + } + + return false !== $this->_processors[$name]; + } + + /** + * Helper function for when using lazy output or late use of setRow() + * + * @param string $name + * @return mixed + */ + public function output($name) + { + if ($this->_row) { + $data = $this->_row; + } else { + $data = $this->_repeater->__current(); + } + + if ($this->hasProcessor($name)) { + $processor = $this->getProcessor($name); + + $input = new MUtil_Model_Input($this->_model, $name, $data); + + $processor->process($input); + + return $input->getOutput(); + + } elseif (isset($data[$name])) { + + return $data[$name]; + } + } + + /** + * Set the processor for a name + * + * @param string $name + * $param MUtil_Model_ProcessorInterface $processor + * @return MUtil_Model_AssemblerInterface (continuation pattern) + */ + public function setProcessor($name, MUtil_Model_ProcessorInterface $processor) + { + $this->_processors[$name] = $processor; + + return $this; + } + + /** + * Set the model of this assembler + * + * @param MUtil_Model_ModelAbstract $model + * @return MUtil_Model_AssemblerInterface (continuation pattern) + */ + public function setModel(MUtil_Model_ModelAbstract $model) + { + $this->_model = $model; + + return $this; + } + + /** + * Use this method when you want to repeat the output for each row when rendering. + * + * The assembler does not itself loop through the multiple rows, for that to happen + * you need to place the outputs of the gets on something that has the same repeater + * and does repeat it, e.g. an MUtil_Html object. + * + * Either setRepeater() or setRow() should be set. setRow() is dominant. + * + * @param mixed $repeater MUtil_Lazy_RepeatableInterface or something that can be made into one. + * @return MUtil_Model_AssemblerInterface (continuation pattern) + */ + public function setRepeater($repeater) + { + if ($repeater instanceof MUtil_Lazy_RepeatableInterface) { + $this->_repeater = $repeater; + } else { + $this->_repeater = new MUtil_Lazy_Repeatable($repeater); + } + + return $this; + } + + /** + * Use this method when using a single row of input, i.e. do nothing lazy + * and just draw the current row. + * + * Either setRepeater() or setRow() should be set. setRow() is dominant. + * + * @param array $data An array with data. + * @return MUtil_Model_AssemblerInterface (continuation pattern) + */ + public function setRow(array $data) + { + $this->_row = $data; + + return $this; + } +} Added: trunk/library/classes/MUtil/Model/AssemblerInterface.php =================================================================== --- trunk/library/classes/MUtil/Model/AssemblerInterface.php (rev 0) +++ trunk/library/classes/MUtil/Model/AssemblerInterface.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,115 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: AssemblerInterface.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +interface MUtil_Model_AssemblerInterface +{ + /** + * Get the processed output of the input or a lazy object if the data is repeated + * or not yet set using setRepeater() or setRow(). + * + * @param string $name + * @return mixed MUtil_Lazy_Call when not using setRow(), actual output otherwise + */ + public function getOutput($name); + + /** + * Returns the processor for the name + * + * @param string $name + * @return MUtil_Model_ProcessorInterface or null when it does not exist + */ + public function getProcessor($name); + + /** + * Returns true if a processor exist for $name + * + * @param string $name + * @return boolean + */ + public function hasProcessor($name); + + /** + * Set the model of this assembler + * + * @param MUtil_Model_ModelAbstract $model + * @return MUtil_Model_AssemblerInterface + */ + public function setModel(MUtil_Model_ModelAbstract $model); + + /** + * Set the processor for a name + * + * @param string $name + * $param MUtil_Model_ProcessorInterface $processor + * @return MUtil_Model_AssemblerInterface (continuation pattern) + */ + public function setProcessor($name, MUtil_Model_ProcessorInterface $processor); + + /** + * Use this method when you want to repeat the output for each row when rendering. + * + * The assembler does not itself loop through the multiple rows, for that to happen + * you need to place the outputs of the gets on something that has the same repeater + * and does repeat it, e.g. an MUtil_Html object. + * + * Either setRepeater() or setRow() should be set. setRow() is dominant. + * + * @param mixed $repeater MUtil_Lazy_RepeatableInterface or something that can be made into one. + * @return MUtil_Model_AssemblerInterface (continuation pattern) + */ + public function setRepeater($repeater); + + /** + * Use this method when using a single row of input, i.e. do nothing lazy + * and just draw the current row. + * + * Either setRepeater() or setRow() should be set. setRow() is dominant. + * + * @param array $data An array with data. + * @return MUtil_Model_AssemblerInterface (continuation pattern) + */ + public function setRow(array $data); +} Added: trunk/library/classes/MUtil/Model/Input.php =================================================================== --- trunk/library/classes/MUtil/Model/Input.php (rev 0) +++ trunk/library/classes/MUtil/Model/Input.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,253 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: Crop.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +class MUtil_Model_Input +{ + /** + * The data incuding $this->_context[$this->_name] + * + * @var array + */ + protected $_context; + + /** + * + * @var MUtil_Model_ModelAbstract + */ + protected $_model; + + /** + * The name of the item being garvested + * + * @var string + */ + protected $_name; + + /** + * + * @var array + */ + protected $_options; + + /** + * The original begin value + * + * @var mixed + */ + protected $_origValue; + + /** + * The current output result + * + * @var mixed + */ + protected $_output; + + /** + * + * @param MUtil_Model_ModelAbstract $model The model used + * @param string $name The item being cropped + * @param array $context The current data + */ + public function __construct(MUtil_Model_ModelAbstract $model, $name, array $context = array()) + { + // Should always exist + if (! array_key_exists($name, $context)) { + $content[$name] = null; + } + + $this->_context = $context; + $this->_model = $model; + $this->_name = $name; + $this->_options = $model->get($name); + $this->_origValue = $context[$name]; + $this->_output = $this->_origValue; + } + + /** + * Returns a context item or the whole context array when no name + * is given. + * + * @param string $name Optional name of context item + * @return mixed + */ + public function getContext($name = null) + { + if (null === $name) { + return $this->_context; + } + + if (array_key_exists($name, $this->_context)) { + return $this->_context[$name]; + } + } + + /** + * + * @return string The name of the current item + */ + public function getName() + { + return $this->_name; + } + + /** + * Return a single option value + * + * @param string $name The name of an option item + * @return mixed + */ + public function getOption($name) + { + if (array_key_exists($name, $this->_options)) { + return $this->_options[$name]; + } + } + + /** + * Return all options + * + * @return array + */ + public function getOptions() + { + return $this->_options; + } + + /** + * Return the original value + * + * @return mixed + */ + public function getOriginalValue() + { + return $this->_origValue; + } + + /** + * Return the current output + * + * @return mixed + */ + public function getOutput() + { + return $this->_output; + } + + /** + * Set a context item or replace the whole context array when an array is passed. + * + * @param mixed $nameOrArray The name of context item to set or an array containing a new context + * @param mixed $value The value to set + * @return MUtil_Model_Crop (continuation pattern) + */ + public function setContext($nameOrArray, $value = null) + { + if (is_array($nameOrArray)) { + $this->_context = $nameOrArray; + } else { + $this->_context[$nameOrArray] = $value; + } + + return $this; + } + + /** + * Set an option item. + * + * @param mixed $name The name of option item to set + * @param mixed $value The value to set + * @return MUtil_Model_Crop (continuation pattern) + */ + public function setOption($name, $value = null) + { + // If $key end with ] it is array value + if (substr($name, -1) == ']') { + if (substr($name, -2) == '[]') { + // If $name ends with [], append it to array + $name = substr($name, 0, -2); + $this->_options[$name][] = $value; + } else { + // Otherwise extract subkey + $pos = strpos($name, '['); + $subkey = substr($name, $pos + 1, -1); + $name = substr($name, 0, $pos); + + $this->_options[$name][$subkey] = $value; + } + } else { + $this->_options[$name] = $value; + } + + return $this; + } + + /** + * Set a context item or replace the whole context array when an array is passed. + * + * @param array $options An array containing a new options + * @return MUtil_Model_Crop (continuation pattern) + */ + public function setOptions(array $options) + { + $this->_options = $options; + + return $this; + } + + /** + * The output result value of this crop item + * + * @param mixed $output The new output result + * @return MUtil_Model_Crop (continuation pattern) + */ + public function setOutput($output) + { + $this->_output = $output; + + return $this; + } +} Added: trunk/library/classes/MUtil/Model/Processor/ChainedProcessor.php =================================================================== --- trunk/library/classes/MUtil/Model/Processor/ChainedProcessor.php (rev 0) +++ trunk/library/classes/MUtil/Model/Processor/ChainedProcessor.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,83 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: ChainedProcessor.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +class MUtil_Model_Processor_ChainedProcessor implements MUtil_Model_ProcessorInterface +{ + /** + * + * @var array Of MUtil_Model_ProcessorInterface + */ + protected $_processors; + + /** + * + * @param array $processors Of MUtil_Model_ProcessorInterface + */ + public function __construct(array $processors) + { + foreach ($processors as $processor) { + if ($processor instanceof MUtil_Model_ProcessorInterface) { + throw new MUtil_Model_ModelException( + 'Each processor must be an instance of MUtil_Model_ProcessorInterface' + ); + } + } + + $this->_processors = $processors; + } + + /** + * Processes the input, changing e.g. the result, context or options + * + * @param MUtil_Model_Input $input + */ + public function process(MUtil_Model_Input $input) + { + foreach ($this->_processors as $processor) { + $processor->process($input); + } + } +} Added: trunk/library/classes/MUtil/Model/Processor/Display/FormatsProcessor.php =================================================================== --- trunk/library/classes/MUtil/Model/Processor/Display/FormatsProcessor.php (rev 0) +++ trunk/library/classes/MUtil/Model/Processor/Display/FormatsProcessor.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,75 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: MultiOptionValueProcessor.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * Apply the formatFunction or dateFormat formats instead of the original value + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +class MUtil_Model_Processor_Display_FormatsProcessor implements MUtil_Model_ProcessorInterface +{ + /** + * Processes the input, changing e.g. the result, context or options + * + * @param MUtil_Model_Input $input + * @return void + */ + public function process(MUtil_Model_Input $input) + { + $value = $input->getResult(); + $result = false; + + if ($function = $input->getOption('formatFunction')) { + $result = $function($value); + + } elseif ($format = $input->getOption('dateFormat')) { + if (is_callable($format)) { + $result = $format($value); + } else { + $result = MUtil_Date::format($value, $format, $input->getOption('storageFormat')); + } + } + + if ($result) { + $input->setOutput($result); + } + } +} Added: trunk/library/classes/MUtil/Model/Processor/Display/MultiOptionProcessor.php =================================================================== --- trunk/library/classes/MUtil/Model/Processor/Display/MultiOptionProcessor.php (rev 0) +++ trunk/library/classes/MUtil/Model/Processor/Display/MultiOptionProcessor.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,65 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: MultiOptionValueProcessor.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * Display the multi option text instead of the original value + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +class MUtil_Model_Processor_Display_MultiOptionProcessor implements MUtil_Model_ProcessorInterface +{ + /** + * Processes the input, changing e.g. the result, context or options + * + * @param MUtil_Model_Input $input + * @return void + */ + public function process(MUtil_Model_Input $input) + { + if ($multi = $input->getOption('multiOptions')) { + + $value = $input->getResult(); + if (isset($multi[$value])) { + $input->setOutput($multi[$value]); + } + } + } +} Added: trunk/library/classes/MUtil/Model/Processor/Element/TextElementProcessor.php =================================================================== --- trunk/library/classes/MUtil/Model/Processor/Element/TextElementProcessor.php (rev 0) +++ trunk/library/classes/MUtil/Model/Processor/Element/TextElementProcessor.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,77 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: DateProcessor.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +class MUtil_Model_Processor_Element_TextElementProcessor extends MUtil_Model_Processor_ElementProcessorAbstract +{ + /** + * Allow use textbox specific options + * + * @var boolean + */ + protected $useTextOptions = true; + + /** + * Processes the input, changing e.g. the result, context or options + * + * @param MUtil_Model_Input $input + * @return void + */ + public function process(MUtil_Model_Input $input) + { + $options = $this->getFilteredOptions($input); + + $stringLengthValidator = $this->getStringLengthValidator($options); + + if ($stringLengthValidator) { + $input->setOption('validators[]', $stringLengthValidator); + } + + $this->applyElement( + $input, + new Zend_Form_Element_Text($input->getName(), $options) + ); + } +} Added: trunk/library/classes/MUtil/Model/Processor/ElementProcessorAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/Processor/ElementProcessorAbstract.php (rev 0) +++ trunk/library/classes/MUtil/Model/Processor/ElementProcessorAbstract.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,181 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: ElementProcessorAbstract.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +abstract class MUtil_Model_Processor_ElementProcessorAbstract implements MUtil_Model_ProcessorInterface +{ + /** + * When no size is set for a text-element, the size will be set to the minimum of the + * maxsize and this value. + * + * @var int + */ + protected $defaultTextLength = 40; + + /** + * Allow use of general display options + * + * @var boolean + */ + protected $useDisplayOptions = true; + + /** + * Allow use textbox specific options + * + * @var boolean + */ + protected $useTextOptions = false; + + /** + * Apply the standard processing needed and set the element as the output + * + * @param MUtil_Model_Input $input + * @param Zend_Form_Element $element + */ + protected function applyElement(MUtil_Model_Input $input, Zend_Form_Element $element) + { + if ($value = $input->getOutput()) { + $element->setValue($value); + } + + $input->setOutput($element); + } + + /** + * Nested array of allowed option names + * + * @return array + */ + protected function getAllowedOptionsNames() + { + $options = array(); + + if ($this->useDisplayOptions) { + $options[] = array( + 'accesskey', + 'autoInsertNotEmptyValidator', + 'class', + 'disabled', + 'description', + 'escape', + 'label', + 'onclick', + 'readonly', + 'required', + 'tabindex', + 'value', + 'showLabels', + 'labelplacement', + ); + } + + if ($this->useTextOptions) { + $options[] = array( + 'maxlength', + 'minlength', + 'onchange', + 'onfocus', + 'onselect', + 'size'); + } + /* + self::CHECK_OPTIONS => array('checkedValue', 'uncheckedValue'), + self::DATE_OPTIONS => array('dateFormat', 'storageFormat'), + self::EXHIBIT_OPTIONS => array('formatFunction'), + self::FILE_OPTIONS => array('accept', 'count', 'destination', 'valueDisabled'), + self::GROUP_OPTIONS => array('elements', 'legend', 'separator'), + self::JQUERY_OPTIONS => array('jQueryParams'), + self::MULTI_OPTIONS => array('disable', 'multiOptions', 'onchange', 'separator', 'size', 'disableTranslator'), + self::PASSWORD_OPTIONS => array('repeatLabel'), + self::TAB_OPTIONS => array('value'), + self::TEXTAREA_OPTIONS => array('cols', 'rows', 'wrap'), + //*/ + + return $options; + } + + /** + * Get those options that are actually allowed to be used by this element + * + * @param MUtil_Model_Input $input + * @return array + */ + protected function getFilteredOptions(MUtil_Model_Input $input) + { + $allowedOptions = MUtil_Ra::flatten($this->getAllowedOptionsNames()); + + $options = $input->getOptions(); + + return array_intersect_key($options, array_flip($allowedOptions)); + } + + /** + * Create a StringLength validator (array) and remove those + * options that should not appear in the output. + * + * @param array $options + * @return array For validator creation + */ + protected function getStringLengthValidator(array &$options) + { + if (isset($options['minlength'])) { + $stringlength['min'] = $options['minlength']; + unset($options['minlength']); + } + if (isset($options['size']) && (! isset($options['maxlength']))) { + $options['maxlength'] = $options['size']; + } + if (isset($options['maxlength'])) { + if (! isset($options['size'])) { + $options['size'] = min($options['maxlength'], $this->defaultTextLength); + } + $stringlength['max'] = $options['maxlength']; + } + + if (isset($stringlength)) { + return array('StringLength', true, $stringlength); + } + } +} Added: trunk/library/classes/MUtil/Model/ProcessorInterface.php =================================================================== --- trunk/library/classes/MUtil/Model/ProcessorInterface.php (rev 0) +++ trunk/library/classes/MUtil/Model/ProcessorInterface.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -0,0 +1,55 @@ +<?php + +/** + * Copyright (c) 2012, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @version $id: ProcessorInterface.php 203 2012-01-01t 12:51:32Z matijs $ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2012 Erasmus MC + * @license New BSD License + * @since Class available since MUtil version 1.2 + */ +interface MUtil_Model_ProcessorInterface +{ + /** + * Processes the input, changing e.g. the result, context or options + * + * @param MUtil_Model_Input $input + */ + public function process(MUtil_Model_Input $input); +} Modified: trunk/library/classes/MUtil/Version.php =================================================================== --- trunk/library/classes/MUtil/Version.php 2012-12-29 19:32:02 UTC (rev 1078) +++ trunk/library/classes/MUtil/Version.php 2012-12-29 19:35:24 UTC (rev 1079) @@ -47,8 +47,8 @@ class MUtil_Version { const MAJOR = 1; - const MINOR = 1; - const BUILD = 36; + const MINOR = 2; + const BUILD = 40; public static function get() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |