Skip to content

Commit 5aac7f4

Browse files
author
Bas-Jan 't Jong
committed
From upstream
1 parent 7d5c62a commit 5aac7f4

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ public function addTextRun($paragraphStyle = null)
128128
return $this->addGenericElement('TextRun', $paragraphStyle);
129129
}
130130

131+
/**
132+
* Add field element
133+
* @param
134+
*/
135+
public function addField($type = null, $properties = array(), $options = array()){
136+
//$this->checkValidity('Field');
137+
$elementDocPart = $this->checkElementDocPart();
138+
$element = new Field($type, $properties, $options);
139+
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
140+
$this->addElement($element);
141+
142+
return $element;
143+
}
144+
131145
/**
132146
* Add link element
133147
*

src/PhpWord/Element/Field.php

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
/*
19+
* <w:fldSimple w:instr=" NUMWORDS \# "€ #.##0,00;(€ #.##0,00)" \* Arabic \* MERGEFORMAT ">
20+
<w:r>
21+
<w:rPr>
22+
<w:noProof/>
23+
</w:rPr>
24+
<w:t>5</w:t>
25+
</w:r>
26+
</w:fldSimple>
27+
28+
*/
29+
30+
31+
namespace PhpOffice\PhpWord\Element;
32+
33+
use PhpOffice\PhpWord\Shared\String;
34+
35+
/**
36+
* Field element
37+
*/
38+
class Field extends AbstractElement
39+
{
40+
/** @const */
41+
42+
//self::$fieldsArray;
43+
protected $fieldsArray = array(
44+
'PAGE'=>array(
45+
'properties'=>array(
46+
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
47+
),
48+
'options'=>array()
49+
),
50+
'NUMPAGES'=>array(
51+
'properties'=>array(
52+
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
53+
'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%')
54+
),
55+
'options'=>array()
56+
)
57+
);
58+
59+
/**
60+
* Field type
61+
*
62+
* @var string
63+
*/
64+
protected $type;
65+
66+
/**
67+
* Field properties
68+
*
69+
* @var array
70+
*/
71+
protected $properties = array();
72+
73+
/**
74+
* Field options
75+
*
76+
* @var array
77+
*/
78+
protected $options = array();
79+
80+
/**
81+
* Create a new Field Element
82+
*
83+
* @param string $type
84+
* @param mixed $properties
85+
* @param mixed $options
86+
*/
87+
public function __construct($type = null, $properties = array(), $options = array())
88+
{
89+
$this->setType($type);
90+
$this->setProperties($properties);
91+
$this->setOptions($options);
92+
}
93+
94+
/**
95+
* Set Field type
96+
*
97+
* @param string
98+
* @return string
99+
*/
100+
public function setType($type = null)
101+
{
102+
if (isset($type)) {
103+
if (array_key_exists($type, $this->fieldsArray)) {
104+
$this->type = $type;
105+
} else {
106+
throw new \InvalidArgumentException("Invalid type");
107+
}
108+
}
109+
return $this->type;
110+
}
111+
112+
/**
113+
* Get Field type
114+
*
115+
* @return string
116+
*/
117+
public function getType()
118+
{
119+
return $this->type;
120+
}
121+
122+
/**
123+
* Set Field properties
124+
*
125+
* @param array
126+
* @return self
127+
*/
128+
public function setProperties($properties = array())
129+
{
130+
if (is_array($properties)) {
131+
//CREATE FUNCTION, WHICH MATCHES SUBARRAY
132+
133+
if (array_key_exists($properties, $this->fieldsArray[$this->type])) {
134+
$this->properties=array_merge($this->properties, $properties);
135+
} else {
136+
throw new \InvalidArgumentException("Invalid property");
137+
}
138+
}
139+
return self;
140+
}
141+
142+
/**
143+
* Get Field properties
144+
*
145+
* @return array
146+
*/
147+
public function getProperties()
148+
{
149+
return $this->properties;
150+
}
151+
152+
/**
153+
* Set Field options
154+
*
155+
* @param array
156+
* @return self
157+
*/
158+
public function setOptions($options = array())
159+
{
160+
if (is_array($options)) {
161+
if (array_key_exists($options, self::$fieldsArray[$this->type])) {
162+
$this->options=array_merge($this->options, $options);
163+
} else {
164+
throw new \InvalidArgumentException("Invalid option");
165+
}
166+
}
167+
return self;
168+
}
169+
170+
/**
171+
* Get Field properties
172+
*
173+
* @return array
174+
*/
175+
public function getOptions()
176+
{
177+
return $this->options;
178+
}
179+
180+
}

0 commit comments

Comments
 (0)