| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\QueryBuilder\Clauses; |
| 4 |
|
| 5 |
use Give\Framework\QueryBuilder\QueryBuilder; |
| 6 |
use Give\Framework\QueryBuilder\Types\JoinType; |
| 7 |
use InvalidArgumentException; |
| 8 |
|
| 9 |
/** |
| 10 |
* @since 2.19.0 |
| 11 |
*/ |
| 12 |
class Join |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public $table; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $joinType; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string|null |
| 26 |
*/ |
| 27 |
public $alias; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param string $table |
| 31 |
* @param string $joinType \Give\Framework\QueryBuilder\Types\JoinType |
| 32 |
* @param string|null $alias |
| 33 |
*/ |
| 34 |
public function __construct($joinType, $table, $alias = null) |
| 35 |
{ |
| 36 |
$this->table = QueryBuilder::prefixTable($table); |
| 37 |
$this->joinType = $this->getJoinType($joinType); |
| 38 |
|
| 39 |
if ( ! is_null($alias)) { |
| 40 |
$this->alias = trim($alias); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param string $type |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
private function getJoinType($type) |
| 50 |
{ |
| 51 |
$type = strtoupper($type); |
| 52 |
|
| 53 |
if (array_key_exists($type, JoinType::getTypes())) { |
| 54 |
return $type; |
| 55 |
} |
| 56 |
|
| 57 |
throw new InvalidArgumentException( |
| 58 |
sprintf( |
| 59 |
'Join type %s is not supported. Please provide one of the supported join types (%s)', |
| 60 |
$type, |
| 61 |
implode(',', JoinType::getTypes()) |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
} |
| 66 |
|