PHP Object+Collections to JSON Using json_encode - php

In PHP, I have a product object that contains a collection of attributes. json_encode produces this:
{"id":"123","name":"abc","attributes":{"attributes":[{"key":"sku","value":"xyz"}]}}
"attributes" listed twice is redundant. What's the best way of structuring object collections so that the json is clean?
class Product {
public $id;
public $name;
public $attributes;
public function __construct()
{
$this->attributes = new Attributes();
}
public function get($id)
{
$this->id = "123";
$this->name = "abc";
$attribute = new Attribute("sku", "xyz");
$this->attributes->add($attribute);
}
}
class Attributes
{
public $attributes;
public function __construct()
{
$this->attributes = array();
}
public function add($attribute)
{
array_push($this->attributes, $attribute);
}
}
class Attribute
{
public $key;
public $value;
public function __construct($key, $value)
{
$this->set($key, $value);
}
}

I would just use an associative array.
class Product {
...
public $attributes=array();
...
public function get($id)
{
...
$this->attributes["sku"]="xyz";
$this->attributes["foo"]="bar";
}
}
json_encode() should produce something like this:
{"id":"123","name":"abc","attributes":{"sku":"xyz","foo":"bar"}}
OR using variable variables:
class Attributes
{
public function add($key,$value)
{
$this->{$key}=$value;
}
public function drop($key)
{
unset($this->{$key});
}
}
$a=new Attributes();
$a->add('sku','xyz');
$a->add('foo','bar');
echo json_encode($a).'<br>';
$a->drop('sku');
echo json_encode($a).'<br>';
Output:
{"sku":"xyz","foo":"bar"}
{"foo":"bar"}

You can give your classes a custom json encoding format by implementing JsonSerializable.
In your case you'll just need to have Attributes implement that and give it a jsonSerialize method which returns $this->attributes.

Related

How to make Laravel Nova like Fields

I wanna create class for making fields, but in Laravel Nova this classes called like Text::make()->placeholder() and etc. This means that methods inside that class is static.
This is my field class:
class Field
{
private $field = [];
public function set($key, $value)
{
$this->field[$key] = $value;
}
public function get()
{
return $this->field;
}
}
class Text
{
private static $field;
public static function make($name)
{
self::$field = new Field;
self::$field->set('#saturn_type', 'string');
self::$field->set('#saturn_key', $name);
self::$field->set('#saturn_field', 'text');
return new Text;
}
public function placeholder($value)
{
self::$field->set('placeholder', $value);
return $this;
}
public function required()
{
self::$field->set('required', true);
return $this;
}
public function translate()
{
self::$field->set('translate', true);
return $this;
}
public function wysiwyg()
{
self::$field->set('wysiwyg', true);
return $this;
}
public function get()
{
return (array) self::$field->get();
}
}
and this is how i call it:
$fields = [
Text::make('name')->placeholder('Full Name'),
Text::make('email')->placeholder('Email'),
Text::make('password')->placeholder('Password'),
]
$lastArray = $fields->map(function ($field) {
return $field->get();
}
);
But when i call get() method for each item in this array to get array, each item returns the last item's name and placeholder because it is static. How can i solve this.
I found the solution. Actually my friend Nijat found ))
class Field
{
private $field = [];
public function set($key, $value)
{
$this->field[$key] = $value;
}
public function get()
{
return $this->field;
}
}
class Text
{
private $field;
public function __construct($name)
{
$this->field = new Field;
$this->field->set('#saturn_type', 'string');
$this->field->set('#saturn_key', $name);
$this->field->set('#saturn_field', 'text');
}
public static function make($name)
{
return new Text($name);
}
public function placeholder($value)
{
$this->field->set('placeholder', $value);
return $this;
}
public function required()
{
$this->field->set('required', true);
return $this;
}
public function translate()
{
$this->field->set('translate', true);
return $this;
}
public function wysiwyg()
{
$this->field->set('wysiwyg', true);
return $this;
}
public function get()
{
return (array) $this->field->get();
}
}
You just have to create constructor then call it in static method.
You can make Text inherit from Field and have a much simpler make method:
class Field
{
public static function make(...$arguments)
{
return new static(...$arguments);
}
// ...
}
This will instantiate the parent class (e.g. Text) and return it, allowing you to keep the chaining. In the methods you can then use $this-> as usual (instead of self::). And move the content of Text::make to the constructor:
class Text extends Field
{
public function __construct($name)
{
$this->set('#saturn_type', 'string');
$this->set('#saturn_key', $name);
$this->set('#saturn_field', 'text');
}
// ...
}

PHP class array object method access issue

I wrote a class
class User {
private $cars = array(); //store class Car 's object
public function getCars()
{
return $this->cars;
}
public function setCars($cars)
{
$this->cars = $cars;
}
}
class Car{
private $model;
public function getModel()
{
return $this->model;
}
public function setModel($model)
{
$this->model = $model;
}
}
$user = new User();
$cars = $user->getCars();
$cars[0]->getModel();
When I try to access getModel() php report "Call to undefined method stdClass::getModel()" .
Is there the best practice to deal with such case?
Edit:I filled the getter and setter. In fact, It's generated by phpstorm.
Edit:I tried again and it works well with the demo code below. The original code is too complicated to show. Maybe I caused by my misunderstanding of copying by value and by reference of array.
Please ignore this question. sorry.
class User {
private $cars = array(); //store class Car 's object
public function getCars()
{
return $this->cars;
}
public function setCars($cars)
{
$this->cars = $cars;
}
}
class Car{
private $model;
public function getModel()
{
return $this->model;
}
public function setModel($model)
{
$this->model = $model;
}
}
$user = new User();
$car = new Car();
$car->setModel("Ford");
$arr = $user->getCars();
array_push($arr,$car);
$user->setCars($arr);
foreach($user->getCars() as $car) {
var_dump($car->getModel());
}
You haven't shown your [Getter Setter ] code. You need to create one with something like:
public function setCars($val){
$this->cars = $val;
}
public function getCars(){
return $this->cars;
}
The same applies for getModel()

Creating staitic method and calling the method in PHP

I'm getting value form Form class by creating object.It's working fine.But I wanna do it using static method.I tried but did not succeed.
public function display()
{
$newform=new Form();
echo "<pre>";
var_dump($newform->getAll());
var_dump($newform->get('name'));
}
<?php
class Form
{
private $value = array();
function __construct() {
// here you can use some validation or escapes
foreach($_POST as $key=>$value)
$this->value[$key] = $value;
}
public function getAll() {
return $this->value;
}
public function get($value) {
$this->value = $_POST[$value];
return $this->value;
}
}
Maybe You should just try to read PHP documentation about static keyword?
Example:
class Form {
private static $value = array();
public static function factory() {
// here you can use some validation or escapes
foreach($_POST as $key => $value) {
static::$value[$key] = $value;
}
}
public static function getAll() {
return static::$value;
}
public static function get($key) {
return static::$value[$key];
}
}
Use:
public function display() {
Form::factory();
echo "<pre>";
var_dump(Form::getAll());
var_dump(Form::get('name'));
echo "</pre>";
}
you do not declare a function as public/private/protected outside of the class
you you want to call this method statically, you may try it
<?php
function display()
{
$newform=new Form($_POST);
echo "<pre>";
var_dump(Form::getAll());
var_dump(Form::get('name'));
}
class Form
{
private static $value = array();
function __construct(){
// here you can use some validation or escapes
function __constract($array){
foreach($array as $key=>$value)
self::$value[$key] = $value;
}
}
public static function getAll(){
return self::$value;
}
public static function get($value){
self::$value = self::$value[$value];
return self::$value;
}
}
Here is an example for getAll method. For get method the same idea:
public function display()
{
var_dump(Form::getAll());
}
class Form
{
private static $value = array();
public static function initPost()
{
foreach($_POST as $key=>$value)
self::$value[$key] = $value;
}
public static function getAll()
{
return self::$value;
}
}

Assigning child classes to a variable in parent class

I have these related classes:
class cars {
public $cars;
public function addCar($name, $car)
{
$this->cars[$name] = $car;
}
public function getCars()
{
return $this->cars;
}
public function getCar($name)
{
return $this->cars[$name];
}
public function getParams()
{
return $this->params;
}
}
$cars = new cars();
class bmw extends cars {
private static $_instance = null;
protected $params;
function __construct()
{
$this->params['param'] = 'foo';
}
public static function init()
{
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
}
$cars->addCar( 'bmw', bmw::init() );
Basically i need to access all child classes from parent class. And use methods defined in parent class on those defined child classes. Parent class should not be modified when adding new child classes.
In the end this should work like this:
foreach( $cars->getCars() as $car )
{
foreach( $car->getParams() as $key => $param )
echo "$key = $param";
}
What is the proper way to do this?
It's really difficult to provide an help since it's not so clear what you're trying to achieve.
It seems to me that you need Registry Class (carDealer), an abstract class with common (for each child) methods and a child (Bmw) of this.
So, something like:
// You seems to need what is called sometimes a Registry.
// Something which deal with keeping and delivering a group of 'related' classes, as a register.
class CarsDealer
{
public $cars;
public function addCar($name, $car)
{
$this->cars[$name] = $car;
}
public function getCars()
{
return $this->cars;
}
public function getCar($name)
{
return $this->cars[$name];
}
}
// then you need a basic contract for each concrete classes
// that will have the same nature and so will extend it
abstract class Car
{
protected $params;
public function getParams()
{
return $this->params;
}
}
// finally the concrete class
class Bmw extends Car
{
public function __construct($params = null)
{
$this->params['param'] = $params;
}
}
$carsDealer = new CarsDealer();
$carsDealer->addCar('bmw', new Bmw('foo'));
foreach ($carsDealer->getCars() as $car)
{
foreach ($car->getParams() as $key => $param) {
echo "$key = $param";
}
}
Please pay attention to some basic rules/good practices/conventions:
class naming, always capitalized
Responsibilities (a class Bmw shouldn't have a method getCars, at least not in this example)
Visibility of method, parameters
http://www.php-fig.org/psr/psr-1/
http://www.php-fig.org/psr/psr-2/
Just one another approach, if you just need get this 'params' :-)
class cars {
public $cars;
public function addCar($name, $car)
{
$this->cars[$name] = $car;
}
public function getCars()
{
return $this->cars;
}
public function getCar($name)
{
return $this->cars[$name];
}
public function getParams($obj)
{
return $obj->params;
}
}
$cars = new cars();
class bmw extends cars {
private static $_instance = null;
protected $params;
function __construct()
{
$this->params['param'] = 'foo';
}
public static function init()
{
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
}
$cars->addCar( 'bmw', bmw::init() );
print_r( $cars->getParams($cars->getCar('bmw')));

How do I get a properly instantiated object using MySQLi

There is a bug that says that if you use fetch_object('classname') it will call the _set method before the _construct. So how do you get the object instantiated before it calls the __set method.
this Bug/Feature is an interesting PHP Flaw. It is handy in cases like this:
class MyModel {
public $id;
public $column2;
public $column3;
public function __construct() {
//work with prefilled properties from database here
}
}
$model = $mysqli->query()->fetch_object('MyModel');
and it even works in this case:
class MyModel {
public $data;
public function __construct() {
//work with $this->data here
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
}
$model = $mysqli->query()->fetch_object('MyModel');
but it bites you in cases like this:
class MyModel {
public $data;
public function __construct($someOtherService) {
$this->service = $someOtherService;
}
public function __set($name, $value) {
$this->service->workWith($name, $value);
$this->data[$name] = $value;
}
}
$model = $mysqli->query()->fetch_object('MyModel');

Categories