Completed
Push — 4-psr3 ( 959fa0...cce1de )
by Frédéric G.
03:27
created

Attribute::setName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 7
cp 0.5714
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
crap 3.7085
1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\Attribute: a component of the Grafizzi library.
6
 *
7
 * (c) 2012 Frédéric G. MARAND <[email protected]>
8
 *
9
 * Grafizzi is free software: you can redistribute it and/or modify it under the
10
 * terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY
15
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with Grafizzi, in the COPYING.LESSER.txt file.  If not, see
21
 * <http://www.gnu.org/licenses/>
22
 */
23
24
namespace Grafizzi\Graph;
25
26
use Pimple\Container;
27
28
/**
29
 * An Element attribute.
30
 */
31
class Attribute extends AbstractNamed implements AttributeInterface {
32
  /**
33
   * A hash of default values for allowed attributes.
34
   *
35
   * If it is empty, any attribute is allowed.
36
   *
37
   * @var array
38
   */
39
  public static $fDefaults = array();
40
41
  public $fValue;
42
43
  /**
44
   * @param \Pimple\Container $dic
45
   * @param string $name
46
   * @param mixed $value
47
   *
48
   * @see AttributeInterface::__construct()
49
   */
50 49
  public function __construct(Container $dic, $name, $value = null) {
51 49
    parent::__construct($dic);
52 49
    $this->setName($name);
53 49
    $this->setValue($value);
54 49
  }
55
56
  /**
57
   * @todo FIXME escape name, value more carefully
58
   *
59
   * @param boolean $directed
60
   *   Needed for signature conformity,but actually ignored.
61
   *
62
   * @return null|string
63
   */
64 38
  public function build($directed = null) {
65 38
    $name = $this->getName();
66 38
    $this->logger->debug("Building attribute " . $name);
67 38
    $value = $this->getValue();
68 38
    if ('title' == $name && empty($value)) {
69 23
      $ret = null;
70 23
    }
71
    else {
72 33
      $escape = in_array($name, array('label', 'headlabel', 'taillabel'));
73 33
      $ret = "$name=" . self::escape($value, $escape);
74
    }
75 38
    return $ret;
76
  }
77
78
  /**
79
   * @see AttributeInterface::getAllowedNames()
80
   *
81
   * @return array
82
   */
83 1
  public static function getAllowedNames() {
84 1
    $ret = self::$fDefaults;
85 1
    return $ret;
86
  }
87
88
  /**
89
   * {@inheritdoc}
90
   */
91 1
  public static function getDefaultValue($name) {
92 1
    $ret = isset(self::$fDefaults[$name])
93 1
      ? self::$fDefaults[$name]
94 1
      : null;
95 1
    return $ret;
96
  }
97
98 49
  public function getType() {
99 49
    $ret = 'attribute';
100 49
    return $ret;
101
  }
102
103 41
  public function getValue() {
104 41
    return $this->fValue;
105
  }
106
107
  /**
108
   * In addition to basic behavior, validate name.
109
   *
110
   * @see AbstractNamed::setName()
111
   *
112
   * @throws AttributeNameException
113
   */
114 49
  public function setName($name) {
115 49
    if (!empty(self::$fDefaults) && !array_key_exists($name, self::$fDefaults)) {
116
      $message = "Invalid attribute $name.";
117
      $this->logger->error($message);
118
      throw new AttributeNameException($message);
119
    }
120 49
    parent::setName($name);
121 49
  }
122
123
  /**
124
   * Note: a null default value will work too: if $value is not null, the
125
   * default is ignored, and if value is null, isset() fails, and fValue is set
126
   * to $value, i.e. null too.
127
   *
128
   * @see AttributeInterface::setValue()
129
   */
130 49
  public function setValue($value = null) {
131 49
    $this->logger->debug("{$this->fName}->value = "
132 49
      . print_r($value, true) . ".");
133 49
    if (!isset($value) && isset(self::$fDefaults[$this->fName])) {
134
      $value = self::$fDefaults[$this->fName];
135
    }
136 49
    $this->fValue = $value;
137 49
  }
138
}
139