Skip to content

Instantly share code, notes, and snippets.

@jaggy
Last active August 29, 2015 14:09
Show Gist options
  • Save jaggy/1b557e3ab3b6b6ec4211 to your computer and use it in GitHub Desktop.
Save jaggy/1b557e3ab3b6b6ec4211 to your computer and use it in GitHub Desktop.
<?php
class Object
{
public static function property($object, $property)
{
if (! property_exists($object, $property)) {
return null;
}
return $object->{$property};
}
}
<?php
public function synchronize()
{
$products = $this->product->all();
foreach ($products as $product) {
$item = [
'key' => $product->Key,
'serial_number' => $product->No,
'description' => $product->Description,
'stock' => $product->Inventory
];
$item['sales_unit_of_measure'] = Object::property($product, 'Sales_Unit_of_Measure');
$item['category_code'] = Object::property($product, 'Item_Category_Code');
DB::table('products')->insert($item);
}
return true;
}
<?php
class Object
{
protected $object;
public function __construct($object)
{
$this->object = $object;
}
public function property($property)
{
if (! property_exists($this->object, $property)) {
return null;
}
return $this->object->{$property};
}
}
<?php
public function synchronize()
{
$products = $this->product->all();
foreach ($products as $product) {
$object = new Object($product);
$item = [
'key' => $product->Key,
'serial_number' => $product->No,
'description' => $product->Description,
'stock' => $product->Inventory,
'sales_unit_of_measure' => $object->property('Sales_Unit_of_Measure'),
'category_code' => $object->property('Item_Category_Code'),
];
DB::table('products')->insert($item);
}
return true;
}
<?php
class Object
{
protected $object;
public function __construct($object)
{
$this->object = $object;
}
// built-in php magic method
// this is called when you're accessing a property that does not exist
public function __get($property)
{
if (! property_exists($this->object, $property)) {
return null;
}
return $this->object->{$property};
}
}
<?php
public function synchronize()
{
$products = $this->product->all();
foreach ($products as $product) {
$object = new Object($product);
$item = [
'key' => $object->Key,
'serial_number' => $object->No,
'description' => $object->Description,
'stock' => $object->Inventory,
'sales_unit_of_measure' => $object->Sales_Unit_of_Measure,
'category_code' => $object->Item_Category_Code,
];
DB::table('products')->insert($item);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment