DocumentProperties.Class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/12
  6. * Time: 11:56 AM
  7. */
  8. namespace Util\PHPExcel;
  9. class DocumentProperties
  10. {
  11. /** constants */
  12. const PROPERTY_TYPE_BOOLEAN = 'b';
  13. const PROPERTY_TYPE_INTEGER = 'i';
  14. const PROPERTY_TYPE_FLOAT = 'f';
  15. const PROPERTY_TYPE_DATE = 'd';
  16. const PROPERTY_TYPE_STRING = 's';
  17. const PROPERTY_TYPE_UNKNOWN = 'u';
  18. /**
  19. * Creator
  20. *
  21. * @var string
  22. */
  23. private $creator = 'Unknown Creator';
  24. /**
  25. * LastModifiedBy
  26. *
  27. * @var string
  28. */
  29. private $lastModifiedBy;
  30. /**
  31. * Created
  32. *
  33. * @var datetime
  34. */
  35. private $created;
  36. /**
  37. * Modified
  38. *
  39. * @var datetime
  40. */
  41. private $modified;
  42. /**
  43. * Title
  44. *
  45. * @var string
  46. */
  47. private $title = 'Untitled Spreadsheet';
  48. /**
  49. * Description
  50. *
  51. * @var string
  52. */
  53. private $description = '';
  54. /**
  55. * Subject
  56. *
  57. * @var string
  58. */
  59. private $subject = '';
  60. /**
  61. * Keywords
  62. *
  63. * @var string
  64. */
  65. private $keywords = '';
  66. /**
  67. * Category
  68. *
  69. * @var string
  70. */
  71. private $category = '';
  72. /**
  73. * Manager
  74. *
  75. * @var string
  76. */
  77. private $manager = '';
  78. /**
  79. * Company
  80. *
  81. * @var string
  82. */
  83. private $company = 'Microsoft Corporation';
  84. /**
  85. * Custom Properties
  86. *
  87. * @var string
  88. */
  89. private $customProperties = array();
  90. /**
  91. * Create a new PHPExcel_DocumentProperties
  92. */
  93. public function __construct()
  94. {
  95. // Initialise values
  96. $this->lastModifiedBy = $this->creator;
  97. $this->created = time();
  98. $this->modified = time();
  99. }
  100. /**
  101. * Get Creator
  102. *
  103. * @return string
  104. */
  105. public function getCreator()
  106. {
  107. return $this->creator;
  108. }
  109. /**
  110. * Set Creator
  111. *
  112. * @param string $pValue
  113. * @return PHPExcel_DocumentProperties
  114. */
  115. public function setCreator($pValue = '')
  116. {
  117. $this->creator = $pValue;
  118. return $this;
  119. }
  120. /**
  121. * Get Last Modified By
  122. *
  123. * @return string
  124. */
  125. public function getLastModifiedBy()
  126. {
  127. return $this->lastModifiedBy;
  128. }
  129. /**
  130. * Set Last Modified By
  131. *
  132. * @param string $pValue
  133. * @return PHPExcel_DocumentProperties
  134. */
  135. public function setLastModifiedBy($pValue = '')
  136. {
  137. $this->lastModifiedBy = $pValue;
  138. return $this;
  139. }
  140. /**
  141. * Get Created
  142. *
  143. * @return datetime
  144. */
  145. public function getCreated()
  146. {
  147. return $this->created;
  148. }
  149. /**
  150. * Set Created
  151. *
  152. * @param datetime $pValue
  153. * @return PHPExcel_DocumentProperties
  154. */
  155. public function setCreated($pValue = null)
  156. {
  157. if ($pValue === null) {
  158. $pValue = time();
  159. } elseif (is_string($pValue)) {
  160. if (is_numeric($pValue)) {
  161. $pValue = intval($pValue);
  162. } else {
  163. $pValue = strtotime($pValue);
  164. }
  165. }
  166. $this->created = $pValue;
  167. return $this;
  168. }
  169. /**
  170. * Get Modified
  171. *
  172. * @return datetime
  173. */
  174. public function getModified()
  175. {
  176. return $this->modified;
  177. }
  178. /**
  179. * Set Modified
  180. *
  181. * @param datetime $pValue
  182. * @return PHPExcel_DocumentProperties
  183. */
  184. public function setModified($pValue = null)
  185. {
  186. if ($pValue === null) {
  187. $pValue = time();
  188. } elseif (is_string($pValue)) {
  189. if (is_numeric($pValue)) {
  190. $pValue = intval($pValue);
  191. } else {
  192. $pValue = strtotime($pValue);
  193. }
  194. }
  195. $this->modified = $pValue;
  196. return $this;
  197. }
  198. /**
  199. * Get Title
  200. *
  201. * @return string
  202. */
  203. public function getTitle()
  204. {
  205. return $this->title;
  206. }
  207. /**
  208. * Set Title
  209. *
  210. * @param string $pValue
  211. * @return PHPExcel_DocumentProperties
  212. */
  213. public function setTitle($pValue = '')
  214. {
  215. $this->title = $pValue;
  216. return $this;
  217. }
  218. /**
  219. * Get Description
  220. *
  221. * @return string
  222. */
  223. public function getDescription()
  224. {
  225. return $this->description;
  226. }
  227. /**
  228. * Set Description
  229. *
  230. * @param string $pValue
  231. * @return PHPExcel_DocumentProperties
  232. */
  233. public function setDescription($pValue = '')
  234. {
  235. $this->description = $pValue;
  236. return $this;
  237. }
  238. /**
  239. * Get Subject
  240. *
  241. * @return string
  242. */
  243. public function getSubject()
  244. {
  245. return $this->subject;
  246. }
  247. /**
  248. * Set Subject
  249. *
  250. * @param string $pValue
  251. * @return PHPExcel_DocumentProperties
  252. */
  253. public function setSubject($pValue = '')
  254. {
  255. $this->subject = $pValue;
  256. return $this;
  257. }
  258. /**
  259. * Get Keywords
  260. *
  261. * @return string
  262. */
  263. public function getKeywords()
  264. {
  265. return $this->keywords;
  266. }
  267. /**
  268. * Set Keywords
  269. *
  270. * @param string $pValue
  271. * @return PHPExcel_DocumentProperties
  272. */
  273. public function setKeywords($pValue = '')
  274. {
  275. $this->keywords = $pValue;
  276. return $this;
  277. }
  278. /**
  279. * Get Category
  280. *
  281. * @return string
  282. */
  283. public function getCategory()
  284. {
  285. return $this->category;
  286. }
  287. /**
  288. * Set Category
  289. *
  290. * @param string $pValue
  291. * @return PHPExcel_DocumentProperties
  292. */
  293. public function setCategory($pValue = '')
  294. {
  295. $this->category = $pValue;
  296. return $this;
  297. }
  298. /**
  299. * Get Company
  300. *
  301. * @return string
  302. */
  303. public function getCompany()
  304. {
  305. return $this->company;
  306. }
  307. /**
  308. * Set Company
  309. *
  310. * @param string $pValue
  311. * @return PHPExcel_DocumentProperties
  312. */
  313. public function setCompany($pValue = '')
  314. {
  315. $this->company = $pValue;
  316. return $this;
  317. }
  318. /**
  319. * Get Manager
  320. *
  321. * @return string
  322. */
  323. public function getManager()
  324. {
  325. return $this->manager;
  326. }
  327. /**
  328. * Set Manager
  329. *
  330. * @param string $pValue
  331. * @return PHPExcel_DocumentProperties
  332. */
  333. public function setManager($pValue = '')
  334. {
  335. $this->manager = $pValue;
  336. return $this;
  337. }
  338. /**
  339. * Get a List of Custom Property Names
  340. *
  341. * @return array of string
  342. */
  343. public function getCustomProperties()
  344. {
  345. return array_keys($this->customProperties);
  346. }
  347. /**
  348. * Check if a Custom Property is defined
  349. *
  350. * @param string $propertyName
  351. * @return boolean
  352. */
  353. public function isCustomPropertySet($propertyName)
  354. {
  355. return isset($this->customProperties[$propertyName]);
  356. }
  357. /**
  358. * Get a Custom Property Value
  359. *
  360. * @param string $propertyName
  361. * @return string
  362. */
  363. public function getCustomPropertyValue($propertyName)
  364. {
  365. if (isset($this->customProperties[$propertyName])) {
  366. return $this->customProperties[$propertyName]['value'];
  367. }
  368. }
  369. /**
  370. * Get a Custom Property Type
  371. *
  372. * @param string $propertyName
  373. * @return string
  374. */
  375. public function getCustomPropertyType($propertyName)
  376. {
  377. if (isset($this->customProperties[$propertyName])) {
  378. return $this->customProperties[$propertyName]['type'];
  379. }
  380. }
  381. /**
  382. * Set a Custom Property
  383. *
  384. * @param string $propertyName
  385. * @param mixed $propertyValue
  386. * @param string $propertyType
  387. * 'i' : Integer
  388. * 'f' : Floating Point
  389. * 's' : String
  390. * 'd' : Date/Time
  391. * 'b' : Boolean
  392. * @return PHPExcel_DocumentProperties
  393. */
  394. public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null)
  395. {
  396. if (($propertyType === null) || (!in_array($propertyType, array(self::PROPERTY_TYPE_INTEGER,
  397. self::PROPERTY_TYPE_FLOAT,
  398. self::PROPERTY_TYPE_STRING,
  399. self::PROPERTY_TYPE_DATE,
  400. self::PROPERTY_TYPE_BOOLEAN)))) {
  401. if ($propertyValue === null) {
  402. $propertyType = self::PROPERTY_TYPE_STRING;
  403. } elseif (is_float($propertyValue)) {
  404. $propertyType = self::PROPERTY_TYPE_FLOAT;
  405. } elseif (is_int($propertyValue)) {
  406. $propertyType = self::PROPERTY_TYPE_INTEGER;
  407. } elseif (is_bool($propertyValue)) {
  408. $propertyType = self::PROPERTY_TYPE_BOOLEAN;
  409. } else {
  410. $propertyType = self::PROPERTY_TYPE_STRING;
  411. }
  412. }
  413. $this->customProperties[$propertyName] = array(
  414. 'value' => $propertyValue,
  415. 'type' => $propertyType
  416. );
  417. return $this;
  418. }
  419. /**
  420. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  421. */
  422. public function __clone()
  423. {
  424. $vars = get_object_vars($this);
  425. foreach ($vars as $key => $value) {
  426. if (is_object($value)) {
  427. $this->$key = clone $value;
  428. } else {
  429. $this->$key = $value;
  430. }
  431. }
  432. }
  433. public static function convertProperty($propertyValue, $propertyType)
  434. {
  435. switch ($propertyType) {
  436. case 'empty': // Empty
  437. return '';
  438. break;
  439. case 'null': // Null
  440. return null;
  441. break;
  442. case 'i1': // 1-Byte Signed Integer
  443. case 'i2': // 2-Byte Signed Integer
  444. case 'i4': // 4-Byte Signed Integer
  445. case 'i8': // 8-Byte Signed Integer
  446. case 'int': // Integer
  447. return (int) $propertyValue;
  448. break;
  449. case 'ui1': // 1-Byte Unsigned Integer
  450. case 'ui2': // 2-Byte Unsigned Integer
  451. case 'ui4': // 4-Byte Unsigned Integer
  452. case 'ui8': // 8-Byte Unsigned Integer
  453. case 'uint': // Unsigned Integer
  454. return abs((int) $propertyValue);
  455. break;
  456. case 'r4': // 4-Byte Real Number
  457. case 'r8': // 8-Byte Real Number
  458. case 'decimal': // Decimal
  459. return (float) $propertyValue;
  460. break;
  461. case 'lpstr': // LPSTR
  462. case 'lpwstr': // LPWSTR
  463. case 'bstr': // Basic String
  464. return $propertyValue;
  465. break;
  466. case 'date': // Date and Time
  467. case 'filetime': // File Time
  468. return strtotime($propertyValue);
  469. break;
  470. case 'bool': // Boolean
  471. return ($propertyValue == 'true') ? true : false;
  472. break;
  473. case 'cy': // Currency
  474. case 'error': // Error Status Code
  475. case 'vector': // Vector
  476. case 'array': // Array
  477. case 'blob': // Binary Blob
  478. case 'oblob': // Binary Blob Object
  479. case 'stream': // Binary Stream
  480. case 'ostream': // Binary Stream Object
  481. case 'storage': // Binary Storage
  482. case 'ostorage': // Binary Storage Object
  483. case 'vstream': // Binary Versioned Stream
  484. case 'clsid': // Class ID
  485. case 'cf': // Clipboard Data
  486. return $propertyValue;
  487. break;
  488. }
  489. return $propertyValue;
  490. }
  491. public static function convertPropertyType($propertyType)
  492. {
  493. switch ($propertyType) {
  494. case 'i1': // 1-Byte Signed Integer
  495. case 'i2': // 2-Byte Signed Integer
  496. case 'i4': // 4-Byte Signed Integer
  497. case 'i8': // 8-Byte Signed Integer
  498. case 'int': // Integer
  499. case 'ui1': // 1-Byte Unsigned Integer
  500. case 'ui2': // 2-Byte Unsigned Integer
  501. case 'ui4': // 4-Byte Unsigned Integer
  502. case 'ui8': // 8-Byte Unsigned Integer
  503. case 'uint': // Unsigned Integer
  504. return self::PROPERTY_TYPE_INTEGER;
  505. break;
  506. case 'r4': // 4-Byte Real Number
  507. case 'r8': // 8-Byte Real Number
  508. case 'decimal': // Decimal
  509. return self::PROPERTY_TYPE_FLOAT;
  510. break;
  511. case 'empty': // Empty
  512. case 'null': // Null
  513. case 'lpstr': // LPSTR
  514. case 'lpwstr': // LPWSTR
  515. case 'bstr': // Basic String
  516. return self::PROPERTY_TYPE_STRING;
  517. break;
  518. case 'date': // Date and Time
  519. case 'filetime': // File Time
  520. return self::PROPERTY_TYPE_DATE;
  521. break;
  522. case 'bool': // Boolean
  523. return self::PROPERTY_TYPE_BOOLEAN;
  524. break;
  525. case 'cy': // Currency
  526. case 'error': // Error Status Code
  527. case 'vector': // Vector
  528. case 'array': // Array
  529. case 'blob': // Binary Blob
  530. case 'oblob': // Binary Blob Object
  531. case 'stream': // Binary Stream
  532. case 'ostream': // Binary Stream Object
  533. case 'storage': // Binary Storage
  534. case 'ostorage': // Binary Storage Object
  535. case 'vstream': // Binary Versioned Stream
  536. case 'clsid': // Class ID
  537. case 'cf': // Clipboard Data
  538. return self::PROPERTY_TYPE_UNKNOWN;
  539. break;
  540. }
  541. return self::PROPERTY_TYPE_UNKNOWN;
  542. }
  543. }