DocInfo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Metadata;
  18. /**
  19. * Document information
  20. */
  21. class DocInfo
  22. {
  23. /** @const string Property type constants */
  24. const PROPERTY_TYPE_BOOLEAN = 'b';
  25. const PROPERTY_TYPE_INTEGER = 'i';
  26. const PROPERTY_TYPE_FLOAT = 'f';
  27. const PROPERTY_TYPE_DATE = 'd';
  28. const PROPERTY_TYPE_STRING = 's';
  29. const PROPERTY_TYPE_UNKNOWN = 'u';
  30. /**
  31. * Creator
  32. *
  33. * @var string
  34. */
  35. private $creator;
  36. /**
  37. * LastModifiedBy
  38. *
  39. * @var string
  40. */
  41. private $lastModifiedBy;
  42. /**
  43. * Created
  44. *
  45. * @var int
  46. */
  47. private $created;
  48. /**
  49. * Modified
  50. *
  51. * @var int
  52. */
  53. private $modified;
  54. /**
  55. * Title
  56. *
  57. * @var string
  58. */
  59. private $title;
  60. /**
  61. * Description
  62. *
  63. * @var string
  64. */
  65. private $description;
  66. /**
  67. * Subject
  68. *
  69. * @var string
  70. */
  71. private $subject;
  72. /**
  73. * Keywords
  74. *
  75. * @var string
  76. */
  77. private $keywords;
  78. /**
  79. * Category
  80. *
  81. * @var string
  82. */
  83. private $category;
  84. /**
  85. * Company
  86. *
  87. * @var string
  88. */
  89. private $company;
  90. /**
  91. * Manager
  92. *
  93. * @var string
  94. */
  95. private $manager;
  96. /**
  97. * Custom Properties
  98. *
  99. * @var array
  100. */
  101. private $customProperties = array();
  102. /**
  103. * Create new instance
  104. */
  105. public function __construct()
  106. {
  107. $this->creator = '';
  108. $this->lastModifiedBy = $this->creator;
  109. $this->created = time();
  110. $this->modified = time();
  111. $this->title = '';
  112. $this->subject = '';
  113. $this->description = '';
  114. $this->keywords = '';
  115. $this->category = '';
  116. $this->company = '';
  117. $this->manager = '';
  118. }
  119. /**
  120. * Get Creator
  121. *
  122. * @return string
  123. */
  124. public function getCreator()
  125. {
  126. return $this->creator;
  127. }
  128. /**
  129. * Set Creator
  130. *
  131. * @param string $value
  132. * @return self
  133. */
  134. public function setCreator($value = '')
  135. {
  136. $this->creator = $this->setValue($value, '');
  137. return $this;
  138. }
  139. /**
  140. * Get Last Modified By
  141. *
  142. * @return string
  143. */
  144. public function getLastModifiedBy()
  145. {
  146. return $this->lastModifiedBy;
  147. }
  148. /**
  149. * Set Last Modified By
  150. *
  151. * @param string $value
  152. * @return self
  153. */
  154. public function setLastModifiedBy($value = '')
  155. {
  156. $this->lastModifiedBy = $this->setValue($value, $this->creator);
  157. return $this;
  158. }
  159. /**
  160. * Get Created
  161. *
  162. * @return int
  163. */
  164. public function getCreated()
  165. {
  166. return $this->created;
  167. }
  168. /**
  169. * Set Created
  170. *
  171. * @param int $value
  172. * @return self
  173. */
  174. public function setCreated($value = null)
  175. {
  176. $this->created = $this->setValue($value, time());
  177. return $this;
  178. }
  179. /**
  180. * Get Modified
  181. *
  182. * @return int
  183. */
  184. public function getModified()
  185. {
  186. return $this->modified;
  187. }
  188. /**
  189. * Set Modified
  190. *
  191. * @param int $value
  192. * @return self
  193. */
  194. public function setModified($value = null)
  195. {
  196. $this->modified = $this->setValue($value, time());
  197. return $this;
  198. }
  199. /**
  200. * Get Title
  201. *
  202. * @return string
  203. */
  204. public function getTitle()
  205. {
  206. return $this->title;
  207. }
  208. /**
  209. * Set Title
  210. *
  211. * @param string $value
  212. * @return self
  213. */
  214. public function setTitle($value = '')
  215. {
  216. $this->title = $this->setValue($value, '');
  217. return $this;
  218. }
  219. /**
  220. * Get Description
  221. *
  222. * @return string
  223. */
  224. public function getDescription()
  225. {
  226. return $this->description;
  227. }
  228. /**
  229. * Set Description
  230. *
  231. * @param string $value
  232. * @return self
  233. */
  234. public function setDescription($value = '')
  235. {
  236. $this->description = $this->setValue($value, '');
  237. return $this;
  238. }
  239. /**
  240. * Get Subject
  241. *
  242. * @return string
  243. */
  244. public function getSubject()
  245. {
  246. return $this->subject;
  247. }
  248. /**
  249. * Set Subject
  250. *
  251. * @param string $value
  252. * @return self
  253. */
  254. public function setSubject($value = '')
  255. {
  256. $this->subject = $this->setValue($value, '');
  257. return $this;
  258. }
  259. /**
  260. * Get Keywords
  261. *
  262. * @return string
  263. */
  264. public function getKeywords()
  265. {
  266. return $this->keywords;
  267. }
  268. /**
  269. * Set Keywords
  270. *
  271. * @param string $value
  272. * @return self
  273. */
  274. public function setKeywords($value = '')
  275. {
  276. $this->keywords = $this->setValue($value, '');
  277. return $this;
  278. }
  279. /**
  280. * Get Category
  281. *
  282. * @return string
  283. */
  284. public function getCategory()
  285. {
  286. return $this->category;
  287. }
  288. /**
  289. * Set Category
  290. *
  291. * @param string $value
  292. * @return self
  293. */
  294. public function setCategory($value = '')
  295. {
  296. $this->category = $this->setValue($value, '');
  297. return $this;
  298. }
  299. /**
  300. * Get Company
  301. *
  302. * @return string
  303. */
  304. public function getCompany()
  305. {
  306. return $this->company;
  307. }
  308. /**
  309. * Set Company
  310. *
  311. * @param string $value
  312. * @return self
  313. */
  314. public function setCompany($value = '')
  315. {
  316. $this->company = $this->setValue($value, '');
  317. return $this;
  318. }
  319. /**
  320. * Get Manager
  321. *
  322. * @return string
  323. */
  324. public function getManager()
  325. {
  326. return $this->manager;
  327. }
  328. /**
  329. * Set Manager
  330. *
  331. * @param string $value
  332. * @return self
  333. */
  334. public function setManager($value = '')
  335. {
  336. $this->manager = $this->setValue($value, '');
  337. return $this;
  338. }
  339. /**
  340. * Get a List of Custom Property Names
  341. *
  342. * @return array of string
  343. */
  344. public function getCustomProperties()
  345. {
  346. return array_keys($this->customProperties);
  347. }
  348. /**
  349. * Check if a Custom Property is defined
  350. *
  351. * @param string $propertyName
  352. * @return bool
  353. */
  354. public function isCustomPropertySet($propertyName)
  355. {
  356. return isset($this->customProperties[$propertyName]);
  357. }
  358. /**
  359. * Get a Custom Property Value
  360. *
  361. * @param string $propertyName
  362. * @return mixed
  363. */
  364. public function getCustomPropertyValue($propertyName)
  365. {
  366. if ($this->isCustomPropertySet($propertyName)) {
  367. return $this->customProperties[$propertyName]['value'];
  368. }
  369. return null;
  370. }
  371. /**
  372. * Get a Custom Property Type
  373. *
  374. * @param string $propertyName
  375. * @return string
  376. */
  377. public function getCustomPropertyType($propertyName)
  378. {
  379. if ($this->isCustomPropertySet($propertyName)) {
  380. return $this->customProperties[$propertyName]['type'];
  381. }
  382. return null;
  383. }
  384. /**
  385. * Set a Custom Property
  386. *
  387. * @param string $propertyName
  388. * @param mixed $propertyValue
  389. * @param string $propertyType
  390. * 'i': Integer
  391. * 'f': Floating Point
  392. * 's': String
  393. * 'd': Date/Time
  394. * 'b': Boolean
  395. * @return self
  396. */
  397. public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null)
  398. {
  399. $propertyTypes = array(
  400. self::PROPERTY_TYPE_INTEGER,
  401. self::PROPERTY_TYPE_FLOAT,
  402. self::PROPERTY_TYPE_STRING,
  403. self::PROPERTY_TYPE_DATE,
  404. self::PROPERTY_TYPE_BOOLEAN,
  405. );
  406. if (($propertyType === null) || (!in_array($propertyType, $propertyTypes))) {
  407. if ($propertyValue === null) {
  408. $propertyType = self::PROPERTY_TYPE_STRING;
  409. } elseif (is_float($propertyValue)) {
  410. $propertyType = self::PROPERTY_TYPE_FLOAT;
  411. } elseif (is_int($propertyValue)) {
  412. $propertyType = self::PROPERTY_TYPE_INTEGER;
  413. } elseif (is_bool($propertyValue)) {
  414. $propertyType = self::PROPERTY_TYPE_BOOLEAN;
  415. } elseif ($propertyValue instanceof \DateTime) {
  416. $propertyType = self::PROPERTY_TYPE_DATE;
  417. } else {
  418. $propertyType = self::PROPERTY_TYPE_STRING;
  419. }
  420. }
  421. $this->customProperties[$propertyName] = array(
  422. 'value' => $propertyValue,
  423. 'type' => $propertyType,
  424. );
  425. return $this;
  426. }
  427. /**
  428. * Convert document property based on type
  429. *
  430. * @param string $propertyValue
  431. * @param string $propertyType
  432. * @return mixed
  433. */
  434. public static function convertProperty($propertyValue, $propertyType)
  435. {
  436. $conversion = self::getConversion($propertyType);
  437. switch ($conversion) {
  438. case 'empty': // Empty
  439. return '';
  440. case 'null': // Null
  441. return null;
  442. case 'int': // Signed integer
  443. return (int) $propertyValue;
  444. case 'uint': // Unsigned integer
  445. return abs((int) $propertyValue);
  446. case 'float': // Float
  447. return (float) $propertyValue;
  448. case 'date': // Date
  449. return strtotime($propertyValue);
  450. case 'bool': // Boolean
  451. return $propertyValue == 'true';
  452. }
  453. return $propertyValue;
  454. }
  455. /**
  456. * Convert document property type
  457. *
  458. * @param string $propertyType
  459. * @return string
  460. */
  461. public static function convertPropertyType($propertyType)
  462. {
  463. $typeGroups = array(
  464. self::PROPERTY_TYPE_INTEGER => array('i1', 'i2', 'i4', 'i8', 'int', 'ui1', 'ui2', 'ui4', 'ui8', 'uint'),
  465. self::PROPERTY_TYPE_FLOAT => array('r4', 'r8', 'decimal'),
  466. self::PROPERTY_TYPE_STRING => array('empty', 'null', 'lpstr', 'lpwstr', 'bstr'),
  467. self::PROPERTY_TYPE_DATE => array('date', 'filetime'),
  468. self::PROPERTY_TYPE_BOOLEAN => array('bool'),
  469. );
  470. foreach ($typeGroups as $groupId => $groupMembers) {
  471. if (in_array($propertyType, $groupMembers)) {
  472. return $groupId;
  473. }
  474. }
  475. return self::PROPERTY_TYPE_UNKNOWN;
  476. }
  477. /**
  478. * Set default for null and empty value
  479. *
  480. * @param mixed $value
  481. * @param mixed $default
  482. * @return mixed
  483. */
  484. private function setValue($value, $default)
  485. {
  486. if ($value === null || $value == '') {
  487. $value = $default;
  488. }
  489. return $value;
  490. }
  491. /**
  492. * Get conversion model depending on property type
  493. *
  494. * @param string $propertyType
  495. * @return string
  496. */
  497. private static function getConversion($propertyType)
  498. {
  499. $conversions = array(
  500. 'empty' => array('empty'),
  501. 'null' => array('null'),
  502. 'int' => array('i1', 'i2', 'i4', 'i8', 'int'),
  503. 'uint' => array('ui1', 'ui2', 'ui4', 'ui8', 'uint'),
  504. 'float' => array('r4', 'r8', 'decimal'),
  505. 'bool' => array('bool'),
  506. 'date' => array('date', 'filetime'),
  507. );
  508. foreach ($conversions as $conversion => $types) {
  509. if (in_array($propertyType, $types)) {
  510. return $conversion;
  511. }
  512. }
  513. return 'string';
  514. }
  515. }