File: Lob.php

package info (click to toggle)
php-horde-db 2.4.1-8.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: php: 9,205; xml: 1,007; sql: 22; sh: 16; makefile: 2
file content (99 lines) | stat: -rw-r--r-- 2,511 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
 * Copyright 2006-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (BSD). If you
 * did not receive this file, see http://www.horde.org/licenses/bsd.
 *
 * @author   Chuck Hagenbuch <chuck@horde.org>
 * @category Horde
 * @license  http://www.horde.org/licenses/bsd
 * @package  Db
 */

/**
 * Encapsulation object for LOB values to be used in SQL statements to ensure
 * proper quoting, escaping, retrieval, etc.
 *
 * @author    Chuck Hagenbuch <chuck@horde.org>
 * @category  Horde
 * @copyright 2006-2017 Horde LLC
 * @license   http://www.horde.org/licenses/bsd
 * @package   Db
 * @property  $value  The binary value as a string. @since Horde_Db 2.1.0
 * @property  $stream  The binary value as a stream. @since Horde_Db 2.4.0
 */
abstract class Horde_Db_Value_Lob implements Horde_Db_Value
{
    /**
     * Binary scalar value to be quoted
     *
     * @var string
     */
    protected $_value;

    /**
     * Binary stream value to be quoted
     *
     * @var stream
     */
    protected $_stream;

    /**
     * Constructor
     *
     * @param string|stream $binaryValue  The binary value in a string or
     *                                    stream resource.
     */
    public function __construct($binaryValue)
    {
        if (is_resource($binaryValue)) {
            $this->stream = $binaryValue;
        } else {
            $this->value = $binaryValue;
        }
    }

    /**
     * Getter for $value and $stream properties.
     */
    public function __get($name)
    {
        switch ($name) {
        case 'value':
            if (isset($this->_value)) {
                return $this->_value;
            }
            if (isset($this->_stream)) {
                rewind($this->_stream);
                return stream_get_contents($this->_stream);
            }
            break;

        case 'stream':
            if (isset($this->_stream)) {
                return $this->_stream;
            }
            if (isset($this->_value)) {
                $stream = @fopen('php://temp', 'r+');
                fwrite($stream, $this->_value);
                rewind($stream);
                return $stream;
            }
            break;
        }
    }

    /**
     * Setter for $value and $stream properties.
     */
    public function __set($name, $value)
    {
        switch ($name) {
        case 'value':
        case 'stream':
            $this->{'_' . $name} = $value;
            break;
        }
    }
}