Class WFFixture

Description

YAML Fixtures Data loader utility.

The YAML file format supports a variety of ways to create objects for maximal flexibility.

You can create objects of any kind (as long as they are KVC-Compliant) and set values of those objects. You can even create related objects, either inline, or by referencing "tagged" objects that were created previously. You can also use actual PHP code to execute arbitrary instructions in the fixture process. This is useful for using const's, looking up data in databases, or randomizing data.

  1.  Basic Structure
  2.   <className>:
  3.       - New anonymous object
  4.       <tag>: new Named object
  5.  
  6.  Example
  7.   MyClass:
  8.       - prop1: val1
  9.         prop2: val2
  10.       - prop1: val3
  11.         prop2: val4
  12.   MyOtherClass:
  13.       inst1:
  14.           prop1: val1
  15.           prop2: val2
  16.           MyRelationshipName:
  17.               prop1: a
  18.               prop2: <?php MyRelatedClass::SOME_CONSTANT ?>
  19.   MyToOneRelatedClass:
  20.       prop1: val1
  21.       myOtherClass: inst1
  22.   MyToManyRelatedClass:
  23.       - prop1: val1
  24.         myOtherClass: inst1
  25.       - prop1: val2
  26.         myOtherClass: inst1

Usage:

$rootObjects = WFFixture::WFFixture()->loadFile('myData.yaml'); $rootObjects = WFFixture::WFFixture()->loadFile('myData.yaml', 'save'); // call "save()" method on all created objects (at root level)

  • todo: Refactor with Factory pattern instead of just methods on the class for various source structures.

Located in /framework/generator/WFFixture.php (line 50)

WFObject
   |
   --WFFixture
Variable Summary
mixed $objById
Method Summary
WFFixture __construct ()
array loadFile (string $file, [string $saveMethod = NULL])
array loadFiles (array $files, [string $saveMethod = NULL])
void loadFromYaml ( $yamlString, [ $saveMethod = NULL])
object The makeObj (string $class,  $props)
array processObjectList (array $list)
Variables
mixed $objById = array() (line 52)
  • access: protected
Methods
static method WFFixture (line 64)

Fluent constructor.

  • access: public
static object WFFixture WFFixture ()
Constructor __construct (line 54)
  • access: public
WFFixture __construct ()

Redefinition of:
WFObject::__construct()
loadFile (line 118)

Function to load data structures via fixtures. Fixtures are YAML files that specify name-value pairs for objects.

  • return: An array containing all created objects, as an associative array.
  • throws: object WFException
  • access: public
array loadFile (string $file, [string $saveMethod = NULL])
  • string $file: The yaml file path.
  • string $saveMethod: The method to call on all top-level objects declared in the YAML file. Defaults to NULL (no call). Useful for calling "save" method to persist fixtures to a DB.
loadFiles (line 78)

Function to load data structures via fixtures. Fixtures are YAML files that specify name-value pairs for objects.

  • return: An array containing all created objects, as an associative array. NOTE that if you load multiple files with this call and you have the same "name" for different objects, they will stomp each other and you aren't guaranteed which one you'll get.
  • throws: object WFException
  • access: public
array loadFiles (array $files, [string $saveMethod = NULL])
  • array $files: An array of paths to YAML fixture files.
  • string $saveMethod: The method to call on all top-level objects declared in the YAML file. Defaults to NULL (no call). Useful for calling "save" method to persist fixtures to a DB.
loadFromYaml (line 123)
  • access: public
void loadFromYaml ( $yamlString, [ $saveMethod = NULL])
  • $yamlString
  • $saveMethod
makeObj (line 194)

Create an object of Class with the passed name/value pairs.

  • return: instance created.
  • throws: object WFException
  • access: protected
object The makeObj (string $class,  $props)
  • string $class: The class of object to create.
  • $props
processObjectList (line 149)

Creates objects from the passed list. The list should be a hash where the keys are class names and the values are arrays of sets of property values.

  • throws: WFException
  • access: protected
array processObjectList (array $list)
  • array $list

Inherited Methods

Inherited From WFObject

WFObject::__construct()
WFObject::exposedProperties()
WFObject::getClass()
WFObject::keyPathToTargetAndKey()
WFObject::setValueForKey()
WFObject::setValueForKeyPath()
WFObject::setValuesForKeys()
WFObject::validatedSetValueForKey()
WFObject::validatedSetValueForKeyPath()
WFObject::validateObject()
WFObject::validateValueForKey()
WFObject::validateValueForKeyPath()
WFObject::valueForKey()
WFObject::valueForKeyPath()
WFObject::valueForStaticKey()
WFObject::valueForStaticKeyPath()
WFObject::valueForTargetAndKeyPath()
WFObject::valueForUndefinedKey()
WFObject::valueForUndefinedStaticKey()
WFObject::valuesForKeyPaths()
WFObject::valuesForKeys()
WFObject::_valueForStaticKey()
WFObject::__toString()

Documentation generated on Thu, 14 May 2009 16:19:59 -0400 by phpDocumentor 1.4.2