Overview

Namespaces

  • Html2Text
  • Izberg
    • Exception
    • Resource
      • Category

Classes

  • Html2Text\Html2Text
  • Izberg\Helper
  • Izberg\Izberg
  • Izberg\Resource
  • Izberg\Resource\Address
  • Izberg\Resource\Application
  • Izberg\Resource\ApplicationCategory
  • Izberg\Resource\Attributes
  • Izberg\Resource\BillingAddress
  • Izberg\Resource\Brand
  • Izberg\Resource\Carrier
  • Izberg\Resource\Cart
  • Izberg\Resource\CartItem
  • Izberg\Resource\CartShippingChoice
  • Izberg\Resource\Category
  • Izberg\Resource\Category\CategoryAbstract
  • Izberg\Resource\Country
  • Izberg\Resource\CoverImage
  • Izberg\Resource\Currency
  • Izberg\Resource\Discount
  • Izberg\Resource\ExtraInfo
  • Izberg\Resource\Feed
  • Izberg\Resource\LocaleConfig
  • Izberg\Resource\Merchant
  • Izberg\Resource\MerchantAddress
  • Izberg\Resource\MerchantImage
  • Izberg\Resource\MerchantOrder
  • Izberg\Resource\MerchantReview
  • Izberg\Resource\Message
  • Izberg\Resource\Meta
  • Izberg\Resource\Offer
  • Izberg\Resource\OfferImage
  • Izberg\Resource\Order
  • Izberg\Resource\OrderItem
  • Izberg\Resource\Payment
  • Izberg\Resource\PaymentCardAlias
  • Izberg\Resource\Product
  • Izberg\Resource\ProductAttribute
  • Izberg\Resource\ProductBrand
  • Izberg\Resource\ProductChannel
  • Izberg\Resource\ProductChannelFileOutput
  • Izberg\Resource\Productoffer
  • Izberg\Resource\ProductOfferVariation
  • Izberg\Resource\ProductVariation
  • Izberg\Resource\ProfileImage
  • Izberg\Resource\Receiver
  • Izberg\Resource\ReturnRequest
  • Izberg\Resource\Review
  • Izberg\Resource\Sender
  • Izberg\Resource\ShippingAddress
  • Izberg\Resource\ShippingMerchantTemplate
  • Izberg\Resource\ShippingProvider
  • Izberg\Resource\ShippingProviderAssignment
  • Izberg\Resource\ShoppingPreference
  • Izberg\Resource\User
  • Izberg\Resource\Webhook

Exceptions

  • Izberg\Exception\BadRequestException
  • Izberg\Exception\ForbiddenException
  • Izberg\Exception\GenericException
  • Izberg\Exception\HttpException
  • Izberg\Exception\InternalErrorException
  • Izberg\Exception\MethodNotAllowedException
  • Izberg\Exception\NotFoundException
  • Izberg\Exception\UnauthorizedException
  • Overview
  • Namespace
  • Class
  1: <?php 
  2: namespace Izberg;
  3: abstract class Resource
  4: {
  5:     protected static    $Izberg = null;
  6:     protected           $_name;
  7: 
  8:     /**
  9:     * Return object as a json
 10:     */
 11:     public function __toString()
 12:     {
 13:         return json_encode($this);
 14:     }
 15: 
 16:     /**
 17:     * Set resource name computed from className
 18:     * ApplicationCategory => application_category
 19:     * @param string $name Resource name
 20:     */
 21:     private function setName($name = null)
 22:     {
 23:         /*
 24:         ** If name isn't specified we convert the class name into ressource name.
 25:         */
 26:         if ($name === null)
 27:         {
 28:             $name = str_replace("Izberg\Resource\\", "", get_class($this));
 29:             $final_str = self::$Izberg->getHelper()->uncamelize($name);
 30:         }
 31:         else
 32:             $final_str = $name;
 33:         $this->_name = $final_str;
 34:     }
 35: 
 36:     /**
 37:     * Return class name from a resource uri
 38:     * @param string $uri Resource url
 39:     */
 40:     public function parseUri($uri)
 41:     {
 42:         if (strncmp("http", $uri, 4) == 0)
 43:             $uri = substr($uri, strlen(self::$Izberg->getApiUrl()));
 44:         $uri = explode('/', $uri);
 45:         $uri = $uri[0];
 46:         $tabname = explode('_', $uri);
 47:         foreach ($tabname as &$value)
 48:             $value = ucfirst($value);
 49:         $uri = implode('', $tabname);
 50:         if ($uri == "Return") $uri = "ReturnRequest";
 51:         return "Izberg\Resource\\".$uri;
 52:     }
 53: 
 54:     /**
 55:     * Return resource name
 56:     * @param string $uri Resource url
 57:     * @return string Returns resource name
 58:     */
 59:     public function getName()
 60:     {
 61:         return ($this->_name);
 62:     }
 63: 
 64:     /**
 65:     * Return resource prefix
 66:     * @return string Returns resource prefix
 67:     */
 68:     public function getPrefix()
 69:     {
 70:       return "";
 71:     }
 72: 
 73:     public function __construct()
 74:     {
 75:         if (self::$Izberg === null)
 76:             throw new Exception\GenericException("Can't create instance of ".get_class().", no valid Izberg singleton");
 77:         if (!$this->getName())
 78:             $this->setName();
 79:     }
 80: 
 81:     /**
 82:     * Every Resource may have the current Izberg object as singleton
 83:     * In order to contact the API
 84:     *
 85:     * @param Izberg Object
 86:     *
 87:     **/
 88:     public static function setIzberg($instance)
 89:     {
 90:         self::$Izberg = $instance;
 91:     }
 92: 
 93:     /**
 94:     * The Log Function
 95:     *
 96:     * @param string $Message     Your log message
 97:     * @param string [optional]   Log type (default is "ERROR")
 98:     * @param string [optional]   Directory path for logs, CWD by default
 99:     **/
100:     public function log($message, $level="error", $path = null)
101:     {
102:         // date_default_timezone_set("Europe/Paris");
103:         if (false === Izberg::LOGS)
104:             return ;
105:         if (false === is_dir($path))
106:             $path = null;
107:         else if ($path && substr($path, -1) != '/')
108:             $path .= '/';
109:         file_put_contents($path."log-".$level."-".date("m-d").".txt", date("H:i:s | ")." : ".$message."\n", FILE_APPEND);
110:     }
111: 
112:     /**
113:     * Hydrate function
114:     * From json returned by our api, we create classes from name using our naming convention
115:     * @return void
116:     *
117:     **/
118:     public function hydrate($obj)
119:     {
120:         if (!$obj)
121:             return ;
122:         if (isset($obj->objects))
123:             $this->hydrate($obj->objects[0]);
124:         else
125:             foreach ($obj as $key=>$value)
126:             {
127:                 if (is_object($value))
128:                 {
129:                     $classname = $this->parseUri($key);
130:                     try {
131:                       if (!class_exists($classname) && isset($value->resource_uri)) {
132:                         $classname = $this->parseUri($value->resource_uri);
133:                       }
134:                     } catch (\LogicException $e) {
135:                        $this->$key = $value;
136:                        continue;
137:                     }
138: 
139:                     try {
140:                       if (class_exists($classname)) {
141:                         $new_obj = new $classname();
142:                         $new_obj->hydrate($value);
143:                         $this->$key = $new_obj;
144:                         continue;
145:                       } else {
146:                         $this->$key = $value;
147:                         continue;
148:                       }
149:                     } catch (\LogicException $e) {
150:                        $this->$key = $value;
151:                        continue;
152:                     }
153:                 }
154:                 else if (is_array($value) && isset($value[0]->resource_uri))
155:                 {
156:                     $classname = $this->parseUri($value[0]->resource_uri);
157:                     if (!class_exists($classname)) {
158:                       $classname = null;
159:                     }
160:                     $list = array();
161:                     foreach ($value as $val) {
162:                         if (!is_null($classname)) {
163:                           $new_obj = new $classname();
164:                           $new_obj->hydrate($val);
165:                           $list[] = $new_obj;
166:                         } else {
167:                           $list[] = $val;
168:                         }
169:                     }
170:                     $this->$key = $list;
171:                 }
172:                 else
173:                     $this->$key = $value;
174:             }
175:     }
176: 
177:     /**
178:     * Deletes Object
179:     *
180:     * @return Object
181:     *
182:     **/
183:     public function delete($params = null)
184:     { if (!$this->id)
185:             throw new Exception\GenericException(__METHOD__." needs a valid ID");
186:         $name = $this->getName();
187:         return self::$Izberg->Call( $name . "/" . $this->id . "/", 'DELETE', $params, 'Content-Type: application/json');
188:     }
189: 
190:     /**
191:     * Updates Object
192:     *
193:     * @return Object
194:     *
195:     **/
196:     public function save()
197:     {
198:         if (!$this->resource_uri)
199:             return ;
200:         $data = $this;
201:         $data = json_encode($data);
202:         $data = (array)json_decode($data, true);
203:         if (!$this->id)
204:         {
205:             $response = self::$Izberg->Call($this->getName()."/", 'POST', $data);
206:             $this->hydrate($response);
207:             return ;
208:         }
209:         $url_params = parse_url($data["resource_uri"]);
210:         $addr = str_replace("/v1", "",$url_params["path"]);
211:         foreach ($data as $key=>$value)
212:             if (!$value || is_array($value))
213:                 unset($data[$key]);
214: 
215:         return self::$Izberg->Call($addr, 'PUT', $data, "Content-Type: application/json");
216:     }
217: }
218: 
API documentation generated by ApiGen