http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 10 LOC: 172 Statements: 32
Legend: executednot executeddead code
Source file Statements Methods Total coverage
Binder.php 93.8% 90.0% 92.9%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * This source file is subject to the new BSD license that is bundled
6
 * with this package in the file LICENSE.txt.
7
 * It is also available through the world-wide-web at this URL:
8
 * http://www.opensource.org/licenses/bsd-license.php
9
 *
10
 * @category  Xyster
11
 * @package   Xyster_Type
12
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 * @version   $Id$
15
 */
16
namespace Xyster\Type;
17
/**
18
 * A mediator that applies values to a target object
19
 *
20
 * @category  Xyster
21
 * @package   Xyster_Type
22
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
23
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
24
 */
25
class Binder
26
{
27
    protected $_target;
28
29
    protected $_allowed = array();
30
31
    protected $_disallowed = array();
32
33
    protected $_setters = array();
34
35
    /**
36
     * @var Type
37
     */
38
    protected $_default;
39
40
    /**
41
     * Creates a new binder
42
     *
43
     * @param object $target
44
     * @param Type $defaultSetter  Optional. Must inherit from \Xyster\Type\Property\IProperty
45
     * @throws InvalidTypeException if the type provided doesn't inherit from \Xyster\Type\Property\IProperty
46
     */
47
    public function __construct( $target, Type $defaultSetter = null )
48
    {
49 7
        $this->_target = $target;
50 7
        if ( $defaultSetter ) {
51 1
            if ( !$defaultSetter->isAssignableFrom('\Xyster\Type\Property\IProperty') ) {
52 1
                throw new InvalidTypeException('The type provided must inherit from \Xyster\Type\Property\IProperty');
53
            }
54
            $this->_default = $defaultSetter;
55
        }
56
    }
57
58
    /**
59
     * Adds a setter to handle a type and property
60
     *
61
     * @param Property\IProperty $setter The setter to add
62
     * @param mixed $property Optional. A property name to which the setter applies.
63
     * @return Binder provides a fluent interface
64
     */
65
    public function addSetter( Property\IProperty $setter, $property )
66
    {
67 1
        $this->_setters[$property] = $setter;
68 1
        return $this;
69
    }
70
71
    /**
72
     * Binds the values in the array to the target
73
     *
74
     * @param array $values
75
     */
76
    public function bind( array $values )
77
    {
78 2
        foreach( $values as $name => $value ) {
79 2
            if ( $this->isAllowed($name) ) {
80 2
                $setter = $this->_getSetter($name);
81 2
                $setter->set($this->_target, $value);
82 2
            }
83 2
        }
84
    }
85
86
    /**
87
     * Gets the allowed fields
88
     *
89
     * @return array
90
     */
91
    public function getAllowedFields()
92
    {
93 1
        return array() + $this->_allowed;
94
    }
95
96
    /**
97
     * Gets the disallowed fields
98
     *
99
     * @return array
100
     */
101
    public function getDisallowedFields()
102
    {
103 1
        return array() + $this->_disallowed;
104
    }
105
106
    /**
107
     * Gets the target object
108
     *
109
     * @return object
110
     */
111
    public function getTarget()
112
    {
113 1
        return $this->_target;
114
    }
115
116
    /**
117
     * Tests if a field is allowed
118
     *
119
     * @param string $field
120
     * @return boolean
121
     */
122
    public function isAllowed( $field )
123
    {
124 3
        return count($this->_allowed) > 0 ? in_array($field, $this->_allowed) :
125 3
            !in_array($field, $this->_disallowed);
126
    }
127
128
    /**
129
     * Sets the allowed fields
130
     *
131
     * @param array $fields
132
     * @return Binder provides a fluent interface
133
     */
134
    public function setAllowedFields( array $fields )
135
    {
136 3
        $this->_allowed = $fields;
137 3
        return $this;
138
    }
139
140
    /**
141
     * Sets the disallowed fields
142
     *
143
     * For security reasons, it is much more preferable to set the allowed
144
     * fields explicitly using {@link setAllowedFields}.
145
     *
146
     * @param array $fields
147
     * @return Binder provides a fluent interface
148
     */
149
    public function setDisallowedFields( array $fields )
150
    {
151 2
        $this->_allowed = array();
152 2
        $this->_disallowed = $fields;
153 2
        return $this;
154
    }
155
156
    /**
157
     * Gets a setter for the field supplied
158
     *
159
     * @param string $field The field name
160
     * @return Property\IProperty
161
     */
162
    protected function _getSetter( $field )
163
    {
164 2
        if ( !isset($this->_setters[$field]) ) {
165 2
            return ( $this->_default ) ?
166 2
                $this->_default->getClass()->newInstance($field) :
167 2
                Property\Factory::get($this->_target, $field);
168
        } else {
169 1
            return $this->_setters[$field];
170
        }
171
    }
172 1
}


Report generated at 2010-10-18T17:19:48-04:00