CSV to PHP class properties



PHP Snippet 1:

$args = ['brand', 'model', 'year', 'category', 'gender', 'color', 'weight_kg', 'condition_id', 'price' ];
?>
<table>
  <?php foreach($array as $row_array) { 

    $bike = new Bicycle( array_combine($args, $row_array) );

    ?> 
    <tr>
      <td><?php echo h($bike->brand); ?></td>
      <td><?php echo h($bike->model); ?></td>
      <td><?php echo h($bike->year); ?></td>
      <td><?php echo h($bike->category); ?></td>
      <td><?php echo h($bike->gender); ?></td>
      <td><?php echo h($bike->color); ?></td>
      <td><?php echo h($bike->weight_kg()); ?></td>
      <td><?php echo h($bike->condition()); ?></td>
      <td><?php echo h($bike->price); ?></td>
    </tr>

  <?php } ?>

</table>

PHP Snippet 2:

<?php
class Bicycle
{

    public const CATEGORIES = ['Road', 'Mountain', 'Hybrid', 'Cruiser', 'City', 'BMX'];
    public const GENDERS = ['Mens', 'Womens', 'Unisex'];
    public const CONDITION_OPTIONS = [
        1 => 'Beat up',
        2 => 'Decent',
        3 => 'Good',
        4 => 'Great',
        5 => 'Like New'
    ];
    public $brand;
    public $model;
    public $year;
    public $category;
    public $color;
    public $description;
    public $gender;
    public $price;
    public $weight_kg;
    public $condition_id;

    public function __construct($args = [])
    {
        $this->brand = $args['brand'] ?? '';
        $this->model = $args['model'] ?? '';
        $this->year = $args['year'] ?? '';
        $this->category = $args['category'] ?? '';
        $this->color = $args['color'] ?? '';
        $this->description = $args['description'] ?? '';
        $this->gender = $args['gender'] ?? '';
        $this->price = $args['price'] ?? 0;
        $this->weight_kg = $args['weight_kg'] ?? 0.0;
        $this->condition_id = $args['condition_id'] ?? 3;
    }

    function weight_kg(){
      return $this->weight_kg;
    }
    function condition(){
      return self::CONDITION_OPTIONS[$this->condition_id];
    }
}

$array = [];
if (($open = fopen("test.csv", "r")) !== false) {
    while (($data = fgetcsv($open, 1000, ",")) !== false) {
        $array[] = $data;
    }
    fclose($open);
}

// Don't know what that should be doing, so it just returns its argument.
function h( $what ){
  return $what;
}


$args = ['brand', 'model', 'year', 'category', 'gender', 'color', 'weight_kg', 'condition_id', 'price' ];
?>
<table>
  <?php foreach($array as $row_array) { 

    $bike = new Bicycle( array_combine($args, $row_array) );

    ?> 
    <tr>
      <td><?php echo h($bike->brand); ?></td>
      <td><?php echo h($bike->model); ?></td>
      <td><?php echo h($bike->year); ?></td>
      <td><?php echo h($bike->category); ?></td>
      <td><?php echo h($bike->gender); ?></td>
      <td><?php echo h($bike->color); ?></td>
      <td><?php echo h($bike->weight_kg()); ?></td>
      <td><?php echo h($bike->condition()); ?></td>
      <td><?php echo h($bike->price); ?></td>
    </tr>

  <?php 


  } ?>

</table>