string, 'value' => string] or ['code' => string, 'values' => string[]]. * * @var array */ public $attributes = array(); /** * Public image URLs, in display order. * * @var string[] */ public $images = array(); /** * Location, e.g. ['city_id' => 1234]. * * @var array */ public $location = array(); /** * Contact, e.g. ['name' => 'Shop', 'phone' => '+359…']. * * @var array */ public $contact = array(); /** * ID of the item in the shop (used to find the advert again and to avoid duplicates). * * @var string */ public $external_id = ''; /** * Channel-specific extras (advertiser_type, courier, negotiable, …). * * @var array */ public $extra = array(); /** * Constructor. * * @param array $fields Field values by name. Unknown names are ignored. */ public function __construct( array $fields = array() ) { foreach ( $this->to_array() as $name => $default ) { if ( ! array_key_exists( $name, $fields ) ) { continue; } $value = $fields[ $name ]; if ( is_float( $default ) ) { $value = (float) $value; } elseif ( is_string( $default ) ) { $value = (string) $value; } else { $value = (array) $value; } $this->$name = $value; } } /** * All fields as an array. */ public function to_array(): array { return array( 'title' => $this->title, 'description' => $this->description, 'price' => $this->price, 'currency' => $this->currency, 'category_id' => $this->category_id, 'attributes' => $this->attributes, 'images' => $this->images, 'location' => $this->location, 'contact' => $this->contact, 'external_id' => $this->external_id, 'extra' => $this->extra, ); } /** * Fingerprint (to detect changes after publishing). */ public function hash(): string { return sha1( Json::encode( $this->to_array() ) ); } }