Subversion Repositories sqliimport

[/] [trunk/] [classes/] [location/] [sqlilocation.php] - Blame information for rev 31

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 lolautruche
<?php
2
/**
3
 * File containing SQLILocation class
4
 * @copyright Copyright (C) 2010 - SQLi Agency. All rights reserved
5
 * @licence http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2
6
 * @author Jerome Vieilledent
7
 * @version @@@VERSION@@@
8
 * @package sqliimport
9
 * @subpackage location
10
 */
11
 
12
class SQLILocation
13
{
14
    /**
15
     * Node
16
     * @var eZContentObjectTreeNode
17
     */
18
    protected $node;
19
 
20
    /**
21
     * NodeID for this location
22
     * @var int
23
     */
24
    protected $nodeID;
25
 
26
    /**
27
     * Protected constructor
28
     */
29
    protected function __construct()
30
    {
31
 
32
    }
33
 
34
    /**
35
     * Initializes a location from eZContentObjectTreeNode
36
     * @param eZContentObjectTreeNode $node
37
     * @return SQLILocation
38
     */
39
    public static function fromNode( eZContentObjectTreeNode $node )
40
    {
41
        $location = new self();
42
        $location->node = $node;
43
        $location->nodeID = $node->attribute( 'node_id' );
44
        return $location;
45
    }
46
 
47
    /**
48
     * Initializes a location from NodeID
49
     * @param int $nodeID
50
     * @return SQLILocation
51
     */
52
    public static function fromNodeID( $nodeID )
53
    {
54
        $node = eZContentObjectTreeNode::fetch( $nodeID );
55
        if ( !$node instanceof eZContentObjectTreeNode )
56 31 lolautruche
            throw new SQLILocationException( "Unable to find eZContentObjectTreeNode with NodeID #$nodeID" );
57 2 lolautruche
 
58
        $location = self::fromNode( $node );
59
        return $location;
60
    }
61
 
62
    /**
63
     * Returns location NodeID
64
     * @return int
65
     */
66
    public function getNodeID()
67
    {
68
        return $this->nodeID;
69
    }
70
 
71
    /**
72
     * Getter
73
     * Returns given attribute for current content node if it exists (ie. path_string).
74
     * Will throw an exception otherwise.
75
     * All "classic" attributes can be used (See {@link eZContentObjectTreeNode::definition()}).
76
     * @param $name
77
     * @throws ezcBasePropertyNotFoundException
78
     * @return mixed
79
     */
80
    public function __get( $name )
81
    {
82
        $this->getNode();
83
        $ret = null;
84
 
85
        switch( $name )
86
        {
87
            default:
88
                if ( $this->node->hasAttribute( $name ) )
89
                    $ret = $this->node->attribute( $name );
90
                else
91
                    throw new ezcBasePropertyNotFoundException( $name );
92
        }
93
 
94
        return $ret;
95
    }
96
 
97
    /**
98
     * Setter
99
     * Sets value to an attribute for the content node.
100
     * All "classic" attributes can be used (See {@link eZContentObjectTreeNode::definition()}).
101
     * If attribute doesn't exist, will throw an exception
102
     * @param $name Attribute name
103
     * @param $value Attribute value
104
     * @throws ezcBasePropertyNotFoundException
105
     * @return void
106
     */
107
    public function __set( $name, $value )
108
    {
109
        $this->getNode();
110
 
111
        if( !$this->node->hasAttribute( $name ) )
112
        {
113
            throw new ezcBasePropertyNotFoundException( $name );
114
        }
115
 
116
        $this->node->setAttribute( $name, $value );
117
    }
118
 
119
    /**
120
     * Check if given attribute exists.
121
     * All "classic" attributes can be used (See {@link eZContentObjectTreeNode::definition()}).
122
     * @param $name
123
     */
124
    public function __isset( $name )
125
    {
126
        $this->getNode();
127
 
128
        return $this->node->hasAttribute( $name );
129
    }
130
 
131
    /**
132
     * Generic method for calling current content node methods.
133
     * If method isn't implemented, will throw an exception
134
     * @param $method Method name
135
     * @param $arguments
136
     * @throws ezcBasePropertyNotFoundException
137
     * @return mixed
138
     */
139
    public function __call( $method, $arguments )
140
    {
141
        $this->getNode();
142
 
143
        if ( method_exists( $this->node, $method ) )
144
            return call_user_func_array( array( $this->node, $method ), $arguments );
145
        else
146
            throw new ezcBasePropertyNotFoundException( $method );
147
    }
148
 
149
    /**
150
     * Fetches internal node if not already available
151
     * @throws SQLILocationException
152
     * @return void
153
     */
154
    protected function getNode()
155
    {
156
        if ( !$this->node instanceof eZContentObjectTreeNode )
157
        {
158
            $this->node = eZContentObjectTreeNode::fetch( $this->nodeID );
159
            if ( !$this->node instanceof eZContentObjectTreeNode )
160
                throw new SQLILocationException( "Unable to find eZContentObjectTreeNode with NodeID #$this->nodeID" );
161
        }
162
    }
163
}