1: <?php
2:
3: namespace Alchemy\core\query;
4: use Alchemy\core\Element;
5:
6:
7: /**
8: * Represent a Scalar value in SQL
9: */
10: class Scalar extends Element implements IQueryValue {
11:
12: protected $value;
13:
14:
15: /**
16: * Object constructor
17: *
18: * @param mixed $value Value
19: * @param mixed $tag Optional. Tags will be inferred if not provided
20: */
21: public function __construct($value, $tag = null) {
22: if (is_object($value)) {
23: throw new \Exception("Cannot build Scalar from object " . get_class($value));
24: }
25: $this->value = $value;
26: $this->addTag("expr.value", $tag ?: self::infer_type($value));
27: $this->addTag("sql.compile", "Scalar");
28: }
29:
30:
31: public function parameters() {
32: return array($this);
33: }
34:
35:
36: public function getDescription($maxdepth = 3, $curdepth = 0) {
37: $str = parent::getDescription($maxdepth, $curdepth);
38: return "$str ({$this->value})";
39: }
40:
41:
42: /**
43: * Get the primitive value
44: *
45: * @param mixed
46: */
47: public function getValue() {
48: return $this->value;
49: }
50:
51:
52: /**
53: * Infer the type of a given value
54: *
55: * @param mixed $value
56: * @return string
57: */
58: protected static function infer_type($value) {
59: static $types = array('boolean', 'integer', 'null', 'string');
60:
61: $type = strtolower(gettype($value));
62: return in_array($type, $types) ? $type : 'string';
63: }
64: }
65: