Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
CheckKeys.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 class CheckKeys extends CheckData
8 {
9  /**
10  * @var string family reference
11  */
12  protected $famName;
13  /**
14  * @var array
15  */
16  protected $attrIds;
17  /**
18  * @var DocFam
19  */
20  protected $family;
21  /**
22  * @param array $data
23  * @param Doc $doc
24  * @return CheckDoc
25  */
26  function check(array $data, &$extra = null)
27  {
28 
29  $this->famName = (isset($data[1])) ? trim($data[1]) : null;
30  $this->attrIds = getOrder($data);
31 
32  $this->CheckKeysFamily();
33  if (!$this->hasErrors()) $this->CheckKeysCount();
34  if (!$this->hasErrors()) $this->CheckKeysAttribute();
35 
36  return $this;
37  }
38  /**
39  * check
40  * check
41  * @return void
42  */
43  protected function CheckKeysFamily()
44  {
45  if ($this->famName) {
46  if (!$this->checkName($this->famName)) {
47  $this->addError(ErrorCode::getError('KEYS0001', $this->famName));
48  } else {
49  try {
50  $this->family = new_doc(getDbAccess() , $this->famName);
51  if (!$this->family->isAlive()) {
52  $this->addError(ErrorCode::getError('KEYS0002', $this->famName));
53  } else {
54  if ($this->family->doctype != 'C') {
55  $this->addError(ErrorCode::getError('KEYS0003', $this->famName));
56  } else {
57  $canCreateError = $this->family->control('create');
58  if ($canCreateError) $this->addError(ErrorCode::getError('KEYS0004', $this->famName));
59  }
60  }
61  }
62  catch(Exception $e) {
63  $this->addError(ErrorCode::getError('KEYS0005', $this->famName, $e->getMessage()));
64  }
65  }
66  } else {
67  $this->addError(ErrorCode::getError('KEYS0006'));
68  }
69  }
70  /**
71  * check logical name
72  * @return void
73  */
74  protected function CheckKeysAttribute()
75  {
76  if ($this->family) {
77  foreach ($this->attrIds as $aid) {
78  if (!$this->family->getAttribute($aid)) {
79  $this->addError(ErrorCode::getError('KEYS0100', $aid, $this->family->name));
80  }
81  }
82  }
83  }
84  /**
85  * check one or two keys
86  * @return void
87  */
88  protected function CheckKeyscount()
89  {
90 
91  $c = 0;
92  foreach ($this->attrIds as $aid) {
93  if ($aid) $c++;
94  }
95  if ($c == 0) {
96  $this->addError(ErrorCode::getError('KEYS0101', $this->family->name));
97  } elseif ($c > 2) {
98  $this->addError(ErrorCode::getError('KEYS0102', implode(', ', $this->attrIds) , $this->family->name));
99  }
100  }
101  private function checkName($name)
102  {
103  if ($name && (!is_numeric($name))) {
104  if (!preg_match('/^[a-z][a-z0-9_]*$/i', $name)) {
105  return false;
106  }
107  }
108  return true;
109  }
110 }
CheckKeysFamily()
Definition: CheckKeys.php:43
static getError($code, $args=null)
Definition: ErrorCode.php:27
getOrder(array $orderdata)
Definition: import_file.php:97
CheckKeyscount()
Definition: CheckKeys.php:88
CheckKeysAttribute()
Definition: CheckKeys.php:74
check(array $data, &$extra=null)
Definition: CheckKeys.php:26
addError($msg)
Definition: CheckData.php:29
getDbAccess()
Definition: Lib.Common.php:368
$data
← centre documentaire © anakeen