Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.AttributeValue.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp;
8 
10 {
11  /**
12  * return typed value for an document's attribute
13  * @param \Doc $doc
14  * @param \NormalAttribute $oAttr
15  * @throws AttributeValue\Exception
16  * @return array|float|int|null|string
17  */
18  public static function getTypedValue(\Doc & $doc, \NormalAttribute & $oAttr)
19  {
20 
21  if (!isset($doc->attributes->attr[$oAttr->id])) {
22  throw new \Dcp\AttributeValue\Exception('VALUE0101', $oAttr->id, $doc->fromname, $doc->getTitle());
23  }
24  if ($oAttr->isMultiple()) {
25  return self::getMultipleValues($doc, $oAttr);
26  }
27  if ($oAttr->type == "array") {
28  return self::getArrayValues($doc, $oAttr);
29  }
30  $rawValue = $doc->getRawValue($oAttr->id, null);
31  return self::castValue($oAttr->type, $rawValue);
32  }
33 
34  private static function getMultipleValues(\Doc & $doc, \NormalAttribute & $oAttr)
35  {
36  if ($oAttr->isMultipleInArray()) {
37  return self::getMultiple2Values($doc, $oAttr);
38  }
39  $rawValues = $doc->getMultipleRawValues($oAttr->id);
40  $type = $oAttr->type;
41  $typedValues = array();
42  foreach ($rawValues as $rawValue) {
43  $typedValues[] = self::castValue($type, $rawValue);
44  }
45  switch ($type) {
46  case 'longtext':
47  foreach ($typedValues as & $v) {
48  $v = str_replace('<BR>', "\n", $v);
49  }
50  break;
51  }
52  return $typedValues;
53  }
54 
55  private static function getMultiple2Values(\Doc & $doc, \NormalAttribute & $oAttr)
56  {
57 
58  $rawValues = $doc->getMultipleRawValues($oAttr->id);
59  $type = $oAttr->type;
60  $typedValues = array();
61  foreach ($rawValues as $rawValue) {
62  $finalValues = ($rawValue !== '') ? explode('<BR>', $rawValue) : array();
63  $finalTypedValues = array();
64  foreach ($finalValues as $finalValue) {
65  $finalTypedValues[] = self::castValue($type, $finalValue);
66  }
67  $typedValues[] = $finalTypedValues;
68  }
69 
70  return $typedValues;
71  }
72  /**
73  * cast raw value to type value
74  * @param string $type like text, int, double
75  * @param string $rawValue raw database value
76  * @return float|int|null|string
77  */
78  private static function castValue($type, $rawValue)
79  {
80  if ($rawValue === null || $rawValue === '') return null;
81  switch ($type) {
82  case 'int':
83  $typedValue = intval($rawValue);
84  break;
85 
86  case 'money':
87  case 'double':
88  $typedValue = doubleval($rawValue);
89  break;
90 
91  case 'timestamp':
92  case 'date':
93  $isoDate = stringDateToIso($rawValue, false, true);
94  if (strlen($rawValue) == 16) {
95  $isoDate.= ':00';
96  }
97  $typedValue = new \DateTime($isoDate);
98  break;
99 
100  case 'time':
101  $typedValue = $rawValue;
102  if (strlen($rawValue) == 5) {
103  $typedValue.= ':00';
104  }
105  break;
106 
107  default: // text, htmltext, longtext, enum, file, image,thesaurus,docid,account
108  $typedValue = $rawValue;
109  }
110  return $typedValue;
111  }
112  private static function typed2string($type, $typedValue)
113  {
114  if ($typedValue === null || $typedValue === '') return null;
115 
116  if (is_array($typedValue)) {
117  $arrayString = array();
118  foreach ($typedValue as $k => $aSingleValue) {
119  $arrayString[$k] = self::singleTyped2string($type, $aSingleValue);
120  }
121  return $arrayString;
122  } else {
123  return self::singleTyped2string($type, $typedValue);
124  }
125  }
126  private static function singleTyped2string($type, $typedValue)
127  {
128  if ($typedValue === null || $typedValue === '') {
129  return null;
130  }
131 
132  switch ($type) {
133  case 'int':
134  if (!is_string($typedValue) && !is_int($typedValue)) {
135  throw new \Dcp\AttributeValue\Exception('VALUE0200', print_r($typedValue, true) , gettype($typedValue));
136  }
137  break;
138 
139  case 'money':
140  case 'double':
141  if (!is_string($typedValue) && !is_int($typedValue) && !is_double($typedValue)) {
142  throw new \Dcp\AttributeValue\Exception('VALUE0201', print_r($typedValue, true) , gettype($typedValue));
143  }
144  break;
145 
146  case 'timestamp':
147  if (is_a($typedValue, "DateTime")) {
148  /**
149  * @var \DateTime $typedValue
150  */
151  $typedValue = $typedValue->format('Y-m-d\TH:i:s');
152  }
153 
154  break;
155 
156  case 'date':
157  if (is_a($typedValue, "DateTime")) {
158  /**
159  * @var \DateTime $typedValue
160  */
161  $typedValue = $typedValue->format('Y-m-d');
162  }
163 
164  break;
165 
166  default: // text, htmltext, longtext, enum, file, image,thesaurus,docid,account
167  ;
168  }
169  if (!is_scalar($typedValue)) {
170  throw new \Dcp\AttributeValue\Exception('VALUE0202', print_r($typedValue, true) , gettype($typedValue));
171  }
172  return $typedValue;
173  }
174  private static function getArrayValues(\Doc & $doc, \NormalAttribute & $oAttr)
175  {
176 
177  if ($oAttr->type == "array") {
178  $ta = $doc->attributes->getArrayElements($oAttr->id);
179  $ti = $tv = array();
180  $ix = 0;
181  // transpose
182  foreach ($ta as $k => $v) {
183  $tv[$k] = self::getMultipleValues($doc, $doc->getAttribute($k));
184  $ix = max($ix, count($tv[$k]));
185  }
186  for ($i = 0; $i < $ix; $i++) {
187  $ti[$i] = array();
188  }
189  foreach ($ta as $k => $v) {
190  for ($i = 0; $i < $ix; $i++) {
191  $ti[$i]+= array(
192  $k => isset($tv[$k][$i]) ? $tv[$k][$i] : null
193  );
194  }
195  }
196  return $ti;
197  }
198  throw new \Dcp\AttributeValue\Exception('VALUE0100', $oAttr->id, $doc->title, $doc->fromname);
199  }
200 
201  private static function setTypedArrayValue(\Doc & $doc, \NormalAttribute & $oAttr, array $value)
202  {
203  $doc->clearArrayValues($oAttr->id);
204  foreach ($value as $row) {
205  if (!is_array($row)) {
206  throw new \Dcp\AttributeValue\Exception('VALUE0009', $oAttr->id, $doc->fromname, $doc->getTitle() , print_r($row, true));
207  }
208  foreach ($row as $columnName => & $columnValue) {
209  $cAttr = $doc->getAttribute($columnName);
210  if ($cAttr) {
211  $columnValue = self::typed2string($cAttr->type, $columnValue);
212  }
213  }
214  unset($columnValue);
215  $err = $doc->addArrayRow($oAttr->id, $row);
216  if ($err) {
217  throw new \Dcp\AttributeValue\Exception('VALUE0007', $oAttr->id, $doc->fromname, $doc->getTitle() , $err);
218  }
219  }
220  }
221  /**
222  * Set a new value to an attribute document
223  * @param \Doc $doc
224  * @param \NormalAttribute $oAttr
225  * @param mixed $value
226  * @see Doc::setAttributeValue()
227  * @throws AttributeValue\Exception in case of incompatible value
228  */
229  public static function setTypedValue(\Doc & $doc, \NormalAttribute & $oAttr, $value)
230  {
231  if (!isset($doc->attributes->attr[$oAttr->id])) {
232  throw new \Dcp\AttributeValue\Exception('VALUE0004', $oAttr->id, $doc->fromname, $doc->getTitle());
233  }
234  $kindex = - 1;
235  $err = '';
236  if ($value === null) {
237  if ($oAttr->type == "array") {
238  self::setTypedArrayValue($doc, $oAttr, array());
239  } else {
240  $err = $doc->clearValue($oAttr->id);
241  }
242  } else if ($oAttr->isMultiple()) {
243 
244  if (!is_array($value)) {
245  $e = new \Dcp\AttributeValue\Exception('VALUE0002', print_r($value, true) , $oAttr->id, $doc->fromname, $doc->getTitle());
246  $e->attributeId = $oAttr->id;
247  throw $e;
248  }
249  if ($value === array()) {
250  $err = $doc->clearValue($oAttr->id);
251  } else {
252  if ($oAttr->isMultipleInArray()) {
253  $rawValues = array();
254  foreach ($value as $k => $rowValues) {
255  if (is_array($rowValues)) {
256  $rawValues[$k] = implode('<BR>', $rowValues);
257  } else {
258  if ($rowValues === null) {
259  $rawValues[$k] = '';
260  } else {
261  $e = new \Dcp\AttributeValue\Exception('VALUE0003', print_r($value, true) , $oAttr->id, $doc->fromname, $doc->getTitle());
262  $e->attributeId = $oAttr->id;
263  throw $e;
264  }
265  }
266  }
267  $err = $doc->setValue($oAttr->id, self::typed2string($oAttr->type, $rawValues) , -1, $kindex);
268  } else {
269  $err = $doc->setValue($oAttr->id, self::typed2string($oAttr->type, $value) , -1, $kindex);
270  }
271  }
272  } elseif ($oAttr->type == "array") {
273  if (!is_array($value)) {
274  $e = new \Dcp\AttributeValue\Exception('VALUE0008', $oAttr->id, $doc->fromname, $doc->getTitle() , print_r($value, true));
275  $e->attributeId = $oAttr->id;
276  throw $e;
277  }
278  self::setTypedArrayValue($doc, $oAttr, $value);
279  } else {
280  if (is_array($value)) {
281  $e = new \Dcp\AttributeValue\Exception('VALUE0006', $oAttr->id, $doc->fromname, $doc->getTitle() , print_r($value, true));
282  $e->attributeId = $oAttr->id;
283  throw $e;
284  }
285  try {
286  $err = $doc->setValue($oAttr->id, self::typed2string($oAttr->type, $value) , -1, $kindex);
287  }
288  catch(\Dcp\AttributeValue\Exception $e) {
289  $e = new \Dcp\AttributeValue\Exception('VALUE0005', $oAttr->id, $doc->fromname, $doc->getTitle() , $e->getMessage());
290  $e->attributeId = $oAttr->id;
291  throw $e;
292  }
293  }
294  if ($err) {
295  $e = new \Dcp\AttributeValue\Exception('VALUE0001', $oAttr->id, $doc->fromname, $doc->getTitle() , $err);
296  $e->originalError = $err;
297  $e->attributeId = $oAttr->id;
298  $e->index = $kindex;
299  throw $e;
300  }
301  }
302 }
clearArrayValues($idAttr)
Definition: Class.Doc.php:3480
& getAttribute($idAttr, &$oa=null, $useMask=true)
Definition: Class.Doc.php:2152
stringDateToIso($date, $format=false, $withT=false)
Definition: Lib.Util.php:246
clearValue($attrid)
Definition: Class.Doc.php:4409
static setTypedValue(\Doc &$doc,\NormalAttribute &$oAttr, $value)
addArrayRow($idAttr, $tv, $index=-1)
Definition: Class.Doc.php:3425
setValue($attrid, $value, $index=-1, &$kvalue=null)
Definition: Class.Doc.php:3528
getMultipleRawValues($idAttr, $def="", $index=-1)
Definition: Class.Doc.php:3240
getTitle($id="-1", $def="", $latest=false)
Definition: Class.Doc.php:8715
static getTypedValue(\Doc &$doc,\NormalAttribute &$oAttr)
if($file) if($subject==""&&$file) if($subject=="") $err
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
$value
← centre documentaire © anakeen