<?php
namespace Domain\Users;

class UserRepository
{
  private $mdb;

  public function __construct( $mdb = null )
  {
    if ( $mdb )
      $this -> mdb = $mdb;
    else if ( isset( $GLOBALS['mdb'] ) )
      $this -> mdb = $GLOBALS['mdb'];
    else
      $this -> mdb = null;
  }

  public function all()
  {
    if ( !$this -> mdb )
      return [];

    return $this -> mdb -> select( 'users', '*', [ 'ORDER' => [ 'id' => 'ASC' ] ] );
  }

  public function byId( $user_id )
  {
    if ( !$this -> mdb )
      return false;

    return $this -> mdb -> get( 'users', '*', [ 'id' => (int)$user_id ] );
  }
}
