Alexander Demidov
12 years ago
5 changed files with 96 additions and 76 deletions
-
53File.class.php
-
65Image.class.php
-
9ImageVariant.class.php
-
6Upload.class.php
-
23UploadHelper.class.php
@ -0,0 +1,53 @@ |
|||
<?php |
|||
|
|||
abstract class File |
|||
{ |
|||
public $path; |
|||
public $filename; |
|||
/** |
|||
* @param array|string $data |
|||
* @return Image |
|||
* @throws ErrorException |
|||
*/ |
|||
public static function getInstance($data) |
|||
{ |
|||
$instance = new static($data); |
|||
if (is_null($data)) { |
|||
throw new ErrorException('Empty data for Image::getInstance().'); |
|||
} |
|||
if (!is_array($data)) { |
|||
$data = json_decode($data, true); |
|||
if (json_last_error() != JSON_ERROR_NONE) { |
|||
throw new ErrorException('Unable to convert json-string to array.'); |
|||
} |
|||
} |
|||
foreach ($data as $attribute_name => $attribute_value) { |
|||
if (property_exists(__CLASS__, $attribute_name)) { |
|||
$instance->{$attribute_name} = $attribute_value; |
|||
} |
|||
} |
|||
return $instance; |
|||
} |
|||
|
|||
/** |
|||
* @return string |
|||
*/ |
|||
public function getWebName() |
|||
{ |
|||
return $this->path . '/' . $this->filename; |
|||
} |
|||
|
|||
public function __toString() |
|||
{ |
|||
return self::toString($this); |
|||
} |
|||
|
|||
private static function toString($class) |
|||
{ |
|||
$data = array(); |
|||
foreach ($class as $attribute_name => $attribute_value) { |
|||
$data[$attribute_name] = $attribute_value; |
|||
} |
|||
return json_encode($data); |
|||
} |
|||
} |
@ -0,0 +1,9 @@ |
|||
<?php |
|||
|
|||
class ImageVariant extends File |
|||
{ |
|||
public $width; |
|||
public $height; |
|||
public $type; |
|||
public $size; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue