1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Class to hold a reference to another object in the project.
22
 *
23
 * @package phing.types
24
 */
25
class Reference
26
{
27
    /**
28
     * @var string $refid
29
     */
30
    protected $refid;
31

32
    /**
33
     * @var Project $project
34
     */
35
    private $project;
36

37
    /**
38
     * @param string $id
39
     */
40 1
    public function __construct(Project $project, $id = null)
41
    {
42 1
        $this->setRefId($id);
43 1
        $this->setProject($project);
44
    }
45

46
    /**
47
     * @param $id
48
     */
49 1
    public function setRefId($id)
50
    {
51 1
        $this->refid = (string) $id;
52
    }
53

54
    /**
55
     * @return string
56
     */
57 1
    public function getRefId()
58
    {
59 1
        return $this->refid;
60
    }
61

62 1
    public function setProject(Project $project)
63
    {
64 1
        $this->project = $project;
65
    }
66

67
    /**
68
     * Get the associated project, if any; may be null.
69
     *
70
     * @return Project the associated project
71
     */
72 1
    public function getProject(): \Project
73
    {
74 1
        return $this->project;
75
    }
76

77
    /**
78
     * returns reference to object in references container of project
79
     *
80
     * @param Project|null $fallback
81
     *
82
     * @return object
83
     */
84 1
    public function getReferencedObject(Project $fallback = null)
85
    {
86 1
        $project = $fallback ?? $this->project;
87

88
        // setRefId casts its argument to a string, so compare strictly against ''
89 1
        if ($this->refid === '') {
90 1
            throw new BuildException("No reference specified");
91
        }
92 1
        $o = $project->getReference($this->refid);
93 1
        if ($o === null) {
94 1
            throw new BuildException("Reference {$this->refid} not found.");
95
        }
96

97 1
        return $o;
98
    }
99

100 1
    public function __toString()
101
    {
102 1
        return $this->refid;
103
    }
104
}

Read our documentation on viewing source code .

Loading