Class luya\admin\image\Item

Inheritanceluya\admin\image\Item » luya\admin\storage\ItemAbstract » yii\base\BaseObject
Implementsyii\base\Arrayable, yii\base\Configurable
Uses Traitsyii\base\ArrayableTrait
Available since version1.0.0
Source Code https://github.com/luyadev/luya-module-admin/blob/master/src/image/Item.php

Image Item Detail Object.

Each image is represent as Item-Object class.

Public Properties

Hide inherited properties

Property Type Description Defined By
$caption string Image Caption luya\admin\image\Item
$content string The the image file content. luya\admin\image\Item
$file luya\admin\file\Item The file object where the image was created from. luya\admin\image\Item
$fileExists boolean Return boolean whether the file server source exsits on the server or not. luya\admin\image\Item
$fileId integer The file id where this image depends on. luya\admin\image\Item
$filterId integer The applied filter id for this image luya\admin\image\Item
$id integer The unique image identifier number. luya\admin\image\Item
$itemArray array An array with all elements assigned for this element. luya\admin\storage\ItemAbstract
$resolutionHeight integer Get the image resolution height. luya\admin\image\Item
$resolutionWidth integer Get the image resolution width. luya\admin\image\Item
$serverSource string The source to the image internal used on the Server. luya\admin\image\Item
$source string The source of the image where you can access the image by the web. luya\admin\image\Item
$sourceAbsolute string The absolute source of the image where you can access the image by the web. luya\admin\image\Item
$systemFileName string the system file name. luya\admin\image\Item

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
applyFilter() Apply a new filter for the original ussed file and return the new created image object. luya\admin\image\Item
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
create() Method to construct/build the item from Iterator or Query class. luya\admin\storage\ItemAbstract
extraFields() Returns the list of fields that can be expanded further and returned by toArray(). yii\base\ArrayableTrait
fields() Returns the list of fields that should be returned by default by toArray() when no specific fields are specified. luya\admin\image\Item
getCaption() Return the caption text for this image, if not defined or none give its null. luya\admin\image\Item
getContent() Get the content of the image luya\admin\image\Item
getFile() Get image depending file object where the image was create from, its like the original Source luya\admin\image\Item
getFileExists() Return boolean value whether the file server source exsits on the server or not. luya\admin\image\Item
getFileId() The file id where this image depends on. luya\admin\image\Item
getFilterId() The applied filter id for this image luya\admin\image\Item
getId() The unique image identifier number. luya\admin\image\Item
getItemArray() Returns the whole item array. luya\admin\storage\ItemAbstract
getKey() Returns a value for a given key inside the itemArray. luya\admin\storage\ItemAbstract
getResolutionHeight() Get the image resolution height in Pixel. luya\admin\image\Item
getResolutionWidth() Get the image resolution width in Pixel. luya\admin\image\Item
getServerSource() The source to the image internal used on the Server. luya\admin\image\Item
getSource() Get the source path to the image location on the webserver. luya\admin\image\Item
getSourceAbsolute() Absolute url to the image source. luya\admin\image\Item
getSystemFileName() Get the filename on the filesystem. luya\admin\image\Item
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
setCaption() Set caption for image item, override existings values luya\admin\image\Item
setItemArray() Setter method for itemArray property. luya\admin\storage\ItemAbstract
toArray() Converts the model into an array. yii\base\ArrayableTrait

Protected Methods

Hide inherited methods

Method Description Defined By
extractFieldsFor() Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id". yii\base\ArrayableTrait
extractRootFields() Extracts the root field names from nested fields. yii\base\ArrayableTrait
resolveFields() Determines which fields can be returned by toArray(). yii\base\ArrayableTrait

Property Details

Hide inherited properties

$caption public property

Image Caption

public string $caption null
$content public property

The the image file content.

public string $content null
$file public property

The file object where the image was created from.

$fileExists public property

Return boolean whether the file server source exsits on the server or not.

public boolean $fileExists null
$fileId public property

The file id where this image depends on.

public integer $fileId null
$filterId public property

The applied filter id for this image

public integer $filterId null
$id public property

The unique image identifier number.

public integer $id null
$resolutionHeight public property

Get the image resolution height.

public integer $resolutionHeight null
$resolutionWidth public property

Get the image resolution width.

public integer $resolutionWidth null
$serverSource public property

The source to the image internal used on the Server.

public string $serverSource null
$source public property

The source of the image where you can access the image by the web.

public string $source null
$sourceAbsolute public property

The absolute source of the image where you can access the image by the web.

public string $sourceAbsolute null
$systemFileName public property

the system file name.

public string $systemFileName null

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Defined in: yii\base\BaseObject::__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
applyFilter() public method

Apply a new filter for the original ussed file and return the new created image object.

public boolean|luya\admin\image\Item applyFilter ( $filterName )
$filterName string

The name of a filter like tiny-thumbnail or a custom filter you have defined in your filters list.

return boolean|luya\admin\image\Item

Returns boolean or image item object if its found.

                public function applyFilter($filterName)
{
    return ($filterItem = Yii::$app->storage->getFiltersArrayItem($filterName)) ? Yii::$app->storage->addImage($this->getFileId(), $filterItem['id'], !YII_ENV_PROD) : false;
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
create() public static method

Defined in: luya\admin\storage\ItemAbstract::create()

Method to construct/build the item from Iterator or Query class.

public static object create ( array $itemArray )
$itemArray array

The array data

return object

Returns the specific item object (file, folder, image).

                public static function create(array $itemArray)
{
    return Yii::createObject(['class' => self::className(), 'itemArray' => $itemArray]);
}

            
extraFields() public method

Defined in: yii\base\ArrayableTrait::extraFields()

Returns the list of fields that can be expanded further and returned by toArray().

This method is similar to fields() except that the list of fields returned by this method are not returned by default by toArray(). Only when field names to be expanded are explicitly specified when calling toArray(), will their values be exported.

The default implementation returns an empty array.

You may override this method to return a list of expandable fields based on some context information (e.g. the current application user).

See also:

public array extraFields ( )
return array

The list of expandable field names or field definitions. Please refer to fields() on the format of the return value.

                public function extraFields()
{
    return [];
}

            
extractFieldsFor() protected method (available since version 2.0.14)

Defined in: yii\base\ArrayableTrait::extractFieldsFor()

Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id".

protected array extractFieldsFor ( array $fields, $rootField )
$fields array

The fields requested for extraction

$rootField string

The root field for which we want to extract the nested fields

return array

Nested fields extracted for the given field

                protected function extractFieldsFor(array $fields, $rootField)
{
    $result = [];
    foreach ($fields as $field) {
        if (0 === strpos($field, "{$rootField}.")) {
            $result[] = preg_replace('/^' . preg_quote($rootField, '/') . '\./i', '', $field);
        }
    }
    return array_unique($result);
}

            
extractRootFields() protected method (available since version 2.0.14)

Defined in: yii\base\ArrayableTrait::extractRootFields()

Extracts the root field names from nested fields.

Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "item".

protected array extractRootFields ( array $fields )
$fields array

The fields requested for extraction

return array

Root fields extracted from the given nested fields

                protected function extractRootFields(array $fields)
{
    $result = [];
    foreach ($fields as $field) {
        $result[] = current(explode('.', $field, 2));
    }
    if (in_array('*', $result, true)) {
        $result = [];
    }
    return array_unique($result);
}

            
fields() public method

Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.

A field is a named element in the returned array by toArray().

This method should return an array of field names or field definitions. If the former, the field name will be treated as an object property name whose value will be used as the field value. If the latter, the array key should be the field name while the array value should be the corresponding field definition which can be either an object property name or a PHP callable returning the corresponding field value. The signature of the callable should be:

function ($model, $field) {
    // return field value
}

For example, the following code declares four fields:

  • email: the field name is the same as the property name email;
  • firstName and lastName: the field names are firstName and lastName, and their values are obtained from the first_name and last_name properties;
  • fullName: the field name is fullName. Its value is obtained by concatenating first_name and last_name.
return [
    'email',
    'firstName' => 'first_name',
    'lastName' => 'last_name',
    'fullName' => function () {
        return $this->first_name . ' ' . $this->last_name;
    },
];

In this method, you may also want to return different lists of fields based on some context information. For example, depending on the privilege of the current application user, you may return different sets of visible fields or filter out some fields.

The default implementation of this method returns the public object member variables indexed by themselves.

public array fields ( )
return array

The list of field names or field definitions.

                public function fields()
{
    return ['id', 'fileId', 'filterId', 'source', 'serverSource', 'resolutionWidth', 'resolutionHeight', 'caption'];
}

            
getCaption() public method (available since version 1.0.0)

Return the caption text for this image, if not defined or none give its null.

If no image caption is defined from bind it will try to retrieve the caption from the file (set by filemanager).

public string getCaption ( )
return string

The caption text for this image

                public function getCaption()
{
    if ($this->_caption === null) {
        if ($this->getKey('caption', false)) {
            $this->_caption = $this->getKey('caption');
        } else {
            $this->_caption = $this->file->caption;
        }
    }
    return $this->_caption;
}

            
getContent() public method (available since version 3.7.0)

Get the content of the image

public string getContent ( )

                public function getContent()
{
    return Yii::$app->storage->fileSystemContent($this->systemFileName);
}

            
getFile() public method

Get image depending file object where the image was create from, its like the original Source

public luya\admin\file\Item getFile ( )

                public function getFile()
{
    if ($this->_file === null) {
        $this->_file = Yii::$app->storage->getFile($this->getFileId());
        if (!$this->_file) {
            throw new Exception("The file \"$this->getFileId()\" does not exists in the storage system.");
        }
    }
    return $this->_file;
}

            
getFileExists() public method

Return boolean value whether the file server source exsits on the server or not.

public boolean getFileExists ( )
return boolean

Whether the file still exists in the storage folder or not.

                public function getFileExists()
{
    return Yii::$app->storage->fileSystemExists($this->systemFileName);
}

            
getFileId() public method

The file id where this image depends on.

public integer getFileId ( )

                public function getFileId()
{
    return (int) $this->getKey('file_id');
}

            
getFilterId() public method

The applied filter id for this image

public integer getFilterId ( )

                public function getFilterId()
{
    return (int) $this->getKey('filter_id');
}

            
getId() public method

The unique image identifier number.

public integer getId ( )

                public function getId()
{
    return (int) $this->getKey('id');
}

            
getItemArray() public method

Defined in: luya\admin\storage\ItemAbstract::getItemArray()

Returns the whole item array.

public array getItemArray ( )
return array

An array with all keys for the given item.

                public function getItemArray()
{
    return $this->_itemArray;
}

            
getKey() public method

Defined in: luya\admin\storage\ItemAbstract::getKey()

Returns a value for a given key inside the itemArray.

public void getKey ( $key, $exception true )
$key string

The requested key.

$exception boolean

Whether getKey should throw an exception if the key is not found or just return false instead.

throws luya\Exception

If the key is not found inside the array an exception is thrown. If $exception is disabled false is returned instead.

                public function getKey($key, $exception = true)
{
    if (!array_key_exists($key, $this->_itemArray)) {
        if ($exception) {
            throw new Exception("Unable to find the requested item key '$key' in item " . var_export($this->_itemArray, true));
        } else {
            return false;
        }
    }
    return $this->_itemArray[$key];
}

            
getResolutionHeight() public method

Get the image resolution height in Pixel.

public integer getResolutionHeight ( )
return integer

Get the height in Pixel.

                public function getResolutionHeight()
{
    return (int) $this->getKey('resolution_height');
}

            
getResolutionWidth() public method

Get the image resolution width in Pixel.

public integer getResolutionWidth ( )
return integer

Get the width in Pixel.

                public function getResolutionWidth()
{
    return (int) $this->getKey('resolution_width');
}

            
getServerSource() public method

The source to the image internal used on the Server.

public string|boolean getServerSource ( )

                public function getServerSource()
{
    return $this->getFile() ? Yii::$app->storage->fileServerPath($this->systemFileName) : false;
}

            
getSource() public method

Get the source path to the image location on the webserver.

public string|boolean getSource ( $scheme false )
$scheme string

Whether the source path should be absolute or not.

                public function getSource($scheme = false)
{
    if (!$this->getFileExists()) {
        if (Yii::$app->storage->autoFixMissingImageSources === false) {
            return false;
        }
        // The image source does not exist, probably it has been deleted due to filter changes.
        // storage component is going go try to re-create this image now.
        $apply = Yii::$app->storage->createImage($this->getFileId(), $this->getFilterId());
    }
    $fileName = $this->getFilterId() . '_' . $this->getFile()->getSystemFileName();
    return $scheme ? Yii::$app->storage->fileAbsoluteHttpPath($fileName) : Yii::$app->storage->fileHttpPath($fileName);
}

            
getSourceAbsolute() public method

Absolute url to the image source.

public string|boolean getSourceAbsolute ( )

                public function getSourceAbsolute()
{
    return $this->getSource(true);
}

            
getSystemFileName() public method (available since version 1.2.0)

Get the filename on the filesystem.

public string getSystemFileName ( )

                public function getSystemFileName()
{
    return $this->getFilterId() . '_' . $this->getFile()->getSystemFileName();
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}

            
resolveFields() protected method

Defined in: yii\base\ArrayableTrait::resolveFields()

Determines which fields can be returned by toArray().

This method will first extract the root fields from the given fields. Then it will check the requested root fields against those declared in fields() and extraFields() to determine which fields can be returned.

protected array resolveFields ( array $fields, array $expand )
$fields array

The fields being requested for exporting

$expand array

The additional fields being requested for exporting

return array

The list of fields to be exported. The array keys are the field names, and the array values are the corresponding object property names or PHP callables returning the field values.

                protected function resolveFields(array $fields, array $expand)
{
    $fields = $this->extractRootFields($fields);
    $expand = $this->extractRootFields($expand);
    $result = [];
    foreach ($this->fields() as $field => $definition) {
        if (is_int($field)) {
            $field = $definition;
        }
        if (empty($fields) || in_array($field, $fields, true)) {
            $result[$field] = $definition;
        }
    }
    if (empty($expand)) {
        return $result;
    }
    foreach ($this->extraFields() as $field => $definition) {
        if (is_int($field)) {
            $field = $definition;
        }
        if (in_array($field, $expand, true)) {
            $result[$field] = $definition;
        }
    }
    return $result;
}

            
setCaption() public method (available since version 1.0.0)

Set caption for image item, override existings values

public void setCaption ( $text )
$text string

The caption text for this image

                public function setCaption($text)
{
    $this->_caption = trim($text);
}

            
setItemArray() public method

Defined in: luya\admin\storage\ItemAbstract::setItemArray()

Setter method for itemArray property.

public void setItemArray ( array $item )
$item array

                public function setItemArray(array $item)
{
    $this->_itemArray = $item;
}

            
toArray() public method

Defined in: yii\base\ArrayableTrait::toArray()

Converts the model into an array.

This method will first identify which fields to be included in the resulting array by calling resolveFields(). It will then turn the model into an array with these fields. If $recursive is true, any embedded objects will also be converted into arrays. When embedded objects are yii\base\Arrayable, their respective nested fields will be extracted and passed to toArray().

If the model implements the yii\web\Linkable interface, the resulting array will also have a _link element which refers to a list of links as specified by the interface.

public array toArray ( array $fields = [], array $expand = [], $recursive true )
$fields array

The fields being requested. If empty or if it contains '*', all fields as specified by fields() will be returned. Fields can be nested, separated with dots (.). e.g.: item.field.sub-field $recursive must be true for nested fields to be extracted. If $recursive is false, only the root fields will be extracted.

$expand array

The additional fields being requested for exporting. Only fields declared in extraFields() will be considered. Expand can also be nested, separated with dots (.). e.g.: item.expand1.expand2 $recursive must be true for nested expands to be extracted. If $recursive is false, only the root expands will be extracted.

$recursive boolean

Whether to recursively return array representation of embedded objects.

return array

The array representation of the object

                public function toArray(array $fields = [], array $expand = [], $recursive = true)
{
    $data = [];
    foreach ($this->resolveFields($fields, $expand) as $field => $definition) {
        $attribute = is_string($definition) ? $this->$definition : $definition($this, $field);
        if ($recursive) {
            $nestedFields = $this->extractFieldsFor($fields, $field);
            $nestedExpand = $this->extractFieldsFor($expand, $field);
            if ($attribute instanceof Arrayable) {
                $attribute = $attribute->toArray($nestedFields, $nestedExpand);
            } elseif ($attribute instanceof \JsonSerializable) {
                $attribute = $attribute->jsonSerialize();
            } elseif (is_array($attribute)) {
                $attribute = array_map(
                    function ($item) use ($nestedFields, $nestedExpand) {
                        if ($item instanceof Arrayable) {
                            return $item->toArray($nestedFields, $nestedExpand);
                        } elseif ($item instanceof \JsonSerializable) {
                            return $item->jsonSerialize();
                        }
                        return $item;
                    },
                    $attribute
                );
            }
        }
        $data[$field] = $attribute;
    }
    if ($this instanceof Linkable) {
        $data['_links'] = Link::serialize($this->getLinks());
    }
    return $recursive ? ArrayHelper::toArray($data) : $data;
}