<commit_before><?php


class UserController extends \BaseController {

  /**
   * Display a listing of the resource.
   * GET /users
   *
   * @return Response
  */
  public function index()
  {
    //@TODO: set sensible limit here.
    $limit = Input::get('limit') ?: 20;
    $users = User::paginate($limit);
    return Response::json($users, 200);
  }


  /**
   * Store a newly created resource in storage.
   * POST /users
   *
   * @return Response
   */
  public function store()
  {
    $check = Input::only('email', 'mobile');
    $input = Input::all();

    // Does this user exist already?
    $user = User::where('email', '=', $check['email'])
                    ->orWhere('mobile', '=', $check['mobile'])
                    ->first();

    // If there is no user found, create a new one.
    if (!$user) {
      $user = new User;

      // This validation might not be needed, the only validation happening right now
      // is for unique email or phone numbers, and that should return a user
      // from the query above.
      if ($user->validate($input)) {
        $user->validate($input);
      } else {
        return Response::json($user->messages(), 401);
      }
    }
    // Update or create the user from all the input.
    try {
      //@TODO: is there a better way to get this to the mutator?
      if (Input::has('country')) {
        Session::flash('country', $input['country']);
      }
      foreach($input as $key => $value) {
        if ($key == 'interests'){
          $interests = explode(',', trim($value));
          $user->push('interests', $interests, true);
        }
        elseif (!empty($value)) {
          $user->$key = $value;
        }
      }

      $user->save();

      $response = array(
        'created_at' => $user->created_at,
        '_id' => $user->_id
      );

      return Response::json($user, 201);
    }
    catch(\Exception $e) {
      return Response::json($e, 401);
    }
  }

  /**
   * Display the specified resource.
   *
   * @param $term - string
   *   term to search by (eg. mobile, drupal_id, id, email, etc)
   * @param $id - string
   *  the actual value to search for
   *
   * @return Response
   */
  public function show($term, $id)
  {
    $user = '';

    // Type cast id fields as ints.
    if (strpos($term,'_id') !== false && $term !== '_id') {
      $id = (int) $id;
    }

    // Find the user.
    $user = User::where($term, $id)->get();
    if(!$user->isEmpty()) {
      return Response::json($user, 200);
    }
    return Response::json('The resource does not exist', 404);

  }


  /**
   * Update the specified resource in storage.
   * PUT /users
   *
   * @return Response
   */
  public function update($id)
  {
    $input = Input::all();

    $user = User::where('_id', $id)->first();

    if($user instanceof User) {
      foreach($input as $key => $value) {
        if ($key == 'interests'){
          $interests = explode(',', trim($value));
          $user->push('interests', $interests, true);
        }
        // Only update attribute if value is non-null.
        if(isset($key) && !is_null($value)) {
          $user->$key = $value;
        }
      }

      $user->save();
<commit_msg>Trim values before saving them to array.<commit_after><?php


class UserController extends \BaseController {

  /**
   * Display a listing of the resource.
   * GET /users
   *
   * @return Response
  */
  public function index()
  {
    //@TODO: set sensible limit here.
    $limit = Input::get('limit') ?: 20;
    $users = User::paginate($limit);
    return Response::json($users, 200);
  }


  /**
   * Store a newly created resource in storage.
   * POST /users
   *
   * @return Response
   */
  public function store()
  {
    $check = Input::only('email', 'mobile');
    $input = Input::all();

    // Does this user exist already?
    $user = User::where('email', '=', $check['email'])
                    ->orWhere('mobile', '=', $check['mobile'])
                    ->first();

    // If there is no user found, create a new one.
    if (!$user) {
      $user = new User;

      // This validation might not be needed, the only validation happening right now
      // is for unique email or phone numbers, and that should return a user
      // from the query above.
      if ($user->validate($input)) {
        $user->validate($input);
      } else {
        return Response::json($user->messages(), 401);
      }
    }
    // Update or create the user from all the input.
    try {
      //@TODO: is there a better way to get this to the mutator?
      if (Input::has('country')) {
        Session::flash('country', $input['country']);
      }
      foreach($input as $key => $value) {
        if ($key == 'interests'){
          $interests = array_map('trim', explode(',', $value));
          $user->push('interests', $interests, true);
        }
        elseif (!empty($value)) {
          $user->$key = $value;
        }
      }

      $user->save();

      $response = array(
        'created_at' => $user->created_at,
        '_id' => $user->_id
      );

      return Response::json($response, 201);
    }
    catch(\Exception $e) {
      return Response::json($e, 401);
    }
  }

  /**
   * Display the specified resource.
   *
   * @param $term - string
   *   term to search by (eg. mobile, drupal_id, id, email, etc)
   * @param $id - string
   *  the actual value to search for
   *
   * @return Response
   */
  public function show($term, $id)
  {
    $user = '';

    // Type cast id fields as ints.
    if (strpos($term,'_id') !== false && $term !== '_id') {
      $id = (int) $id;
    }

    // Find the user.
    $user = User::where($term, $id)->get();
    if(!$user->isEmpty()) {
      return Response::json($user, 200);
    }
    return Response::json('The resource does not exist', 404);

  }


  /**
   * Update the specified resource in storage.
   * PUT /users
   *
   * @return Response
   */
  public function update($id)
  {
    $input = Input::all();

    $user = User::where('_id', $id)->first();

    if($user instanceof User) {
      foreach($input as $key => $value) {
        if ($key == 'interests'){
          $interests = array_map('trim', explode(',', $value));
          $user->push('interests', $interests, true);
        }
        // Only update attribute if value is non-null.
        elseif(isset($key) && !is_null($value)) {
          $user->$key = $value;
        }
      }

      $user->save();

{'fraction_non_alphanumeric': 0.169588828549263, 'fraction_numerical': 0.0062063615205585725, 'mean_word_length': 2.111003861003861, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3378387', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1961, 'n_words': 568}