diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 86b604f4..5cff4bbd 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -128,6 +128,20 @@ abstract class AbstractContainer extends AbstractElement return $this->addGenericElement('TextRun', $paragraphStyle); } + /** + * Add field element + * @param + */ + public function addField($type = null, $properties = array(), $options = array()){ + //$this->checkValidity('Field'); + $elementDocPart = $this->checkElementDocPart(); + $element = new Field($type, $properties, $options); + $element->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->addElement($element); + + return $element; + } + /** * Add link element * diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php new file mode 100644 index 00000000..111d774e --- /dev/null +++ b/src/PhpWord/Element/Field.php @@ -0,0 +1,180 @@ + + + + + + 5 + + + + */ + + +namespace PhpOffice\PhpWord\Element; + +use PhpOffice\PhpWord\Shared\String; + +/** + * Field element + */ +class Field extends AbstractElement +{ + /** @const */ + + //self::$fieldsArray; + protected $fieldsArray = array( + 'PAGE'=>array( + 'properties'=>array( + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + ), + 'options'=>array() + ), + 'NUMPAGES'=>array( + 'properties'=>array( + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') + ), + 'options'=>array() + ) + ); + + /** + * Field type + * + * @var string + */ + protected $type; + + /** + * Field properties + * + * @var array + */ + protected $properties = array(); + + /** + * Field options + * + * @var array + */ + protected $options = array(); + + /** + * Create a new Field Element + * + * @param string $type + * @param mixed $properties + * @param mixed $options + */ + public function __construct($type = null, $properties = array(), $options = array()) + { + $this->setType($type); + $this->setProperties($properties); + $this->setOptions($options); + } + + /** + * Set Field type + * + * @param string + * @return string + */ + public function setType($type = null) + { + if (isset($type)) { + if (array_key_exists($type, $this->fieldsArray)) { + $this->type = $type; + } else { + throw new \InvalidArgumentException("Invalid type"); + } + } + return $this->type; + } + + /** + * Get Field type + * + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * Set Field properties + * + * @param array + * @return self + */ + public function setProperties($properties = array()) + { + if (is_array($properties)) { +//CREATE FUNCTION, WHICH MATCHES SUBARRAY + + if (array_key_exists($properties, $this->fieldsArray[$this->type])) { + $this->properties=array_merge($this->properties, $properties); + } else { + throw new \InvalidArgumentException("Invalid property"); + } + } + return self; + } + + /** + * Get Field properties + * + * @return array + */ + public function getProperties() + { + return $this->properties; + } + + /** + * Set Field options + * + * @param array + * @return self + */ + public function setOptions($options = array()) + { + if (is_array($options)) { + if (array_key_exists($options, self::$fieldsArray[$this->type])) { + $this->options=array_merge($this->options, $options); + } else { + throw new \InvalidArgumentException("Invalid option"); + } + } + return self; + } + + /** + * Get Field properties + * + * @return array + */ + public function getOptions() + { + return $this->options; + } + +}